PDA

View Full Version : Switch views problem




dogwong
May 19, 2010, 06:26 AM
I follow this viedo (http://www.youtube.com/watch?v=zu31fCIwt1E) and make a simular apps
but it throw a error:
"expected expression before 'ThirdViewController'"
on the line
"[self presentModalViewController:ThirdViewController animated:YES];"

I don't know what it mean....
is it because the simulator version? (i use 3 but the video use 2)
and how to switch view if this way is wrong

Please help....
and sorry for my bad English
thanks



TiberiusXavier
May 19, 2010, 09:22 AM
Can you post your code?

dejo
May 19, 2010, 10:09 AM
[self presentModalViewController:ThirdViewController animated:YES];
Remember that Objective-C is case-sensitive and that ThirdViewController is a class name not an instance variable name.

dogwong
May 19, 2010, 10:19 PM
(project name is SWF)

here is the code:

SWFAppDelegate.h

#import <UIKit/UIKit.h>

@class SWFViewController;

@interface SWFAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
SWFViewController *viewController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet SWFViewController *viewController;

@end



SWFAppDelegate.m

#import "SWFAppDelegate.h"
#import "SWFViewController.h"

@implementation SWFAppDelegate

@synthesize window;
@synthesize viewController;


- (void)applicationDidFinishLaunching:(UIApplication *)application {

// Override point for customization after app launch
[window addSubview:viewController.view];
[window makeKeyAndVisible];
}


- (void)dealloc {
[viewController release];
[window release];
[super dealloc];
}


@end


SWFViewController.h

#import <UIKit/UIKit.h>
#import "SecondViewController.h"

@interface SWFViewController : UIViewController {
IBOutlet UIButton *about;
IBOutlet SecondViewController *SecondViewController;
}

- (IBAction)showAbout;

@end


SWFViewController.m
#import "SWFViewController.h"


@implementation SWFViewController



/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
}
return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
*/



// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}


- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}


- (void)dealloc {
[super dealloc];
}

- (IBAction)showAbout {
[self presentModalViewController:SecondViewController animated:YES];
// this line have error
}

@end


SecondViewController.h
#import <UIKit/UIKit.h>
#import "ThirdViewController.h"


@interface SecondViewController : UIViewController {

IBOutlet ThirdViewController *ThirdViewController;

}

- (IBAction)gotoThirdView;

- (IBAction)gotoBack;

@end

SecondViewController.h

#import <UIKit/UIKit.h>
#import "ThirdViewController.h"


@interface SecondViewController : UIViewController {

IBOutlet ThirdViewController *ThirdViewController;

}

- (IBAction)gotoThirdView;

- (IBAction)gotoBack;

@end

SecondViewController.m

#import "SecondViewController.h"


@implementation SecondViewController

- (IBAction)gotoThirdView{
[self presentModalViewController:ThirdViewController animated:YES];
}

- (IBAction)goBack{
[self dismissModalViewControllerAnimated:YES];
}
/*
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
}
return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}


- (void)dealloc {
[super dealloc];
}


@end warning: incomplete implementation of class 'SecondViewController'




ThirdViewController.h
#import <UIKit/UIKit.h>


@interface ThirdViewController : UIViewController {



}

- (IBAction)goBack;

@end

ThirdViewController.m

#import "ThirdViewController.h"


@implementation ThirdViewController

- (IBAction)goBack{
[self dismissModalViewControllerAnimated:YES];
}

/*
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
}
return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}


- (void)dealloc {
[super dealloc];
}


@end


SWFViewController.xib

a button: title: about
connection: Touch up inside - file's owner showAbout

SecondView.xib

a label, text: Second
a button, title: go to third
connection: Touch up inside - file's owner gotoThirdView
a button, title: back
connection: Touch up inside - file's owner gotoBack

ThirdView.xib
a label, text: Third
a button, title: Back
connection: Touch up inside - file's owner gotoBack


Thanks

Svinja
May 20, 2010, 04:20 AM
Hi,
as dejo said...this part of the code is wrong:

- (IBAction)gotoThirdView
{
[self presentModalViewController:ThirdViewController animated:YES];
}

ThirdViewController is a class, you must use instance of that class in order for this to work...for example


- (IBAction)gotoThirdView
{
ThirdViewController *thirdViewController=[[ThirdViewController alloc] init];
[self presentModalViewController:thirdViewController animated:YES];
[thirdViewController release];
}

notice the capital letters...

dogwong
May 21, 2010, 12:27 AM
Hi,
as dejo said...this part of the code is wrong:

- (IBAction)gotoThirdView
{
[self presentModalViewController:ThirdViewController animated:YES];
}

ThirdViewController is a class, you must use instance of that class in order for this to work...for example


- (IBAction)gotoThirdView
{
ThirdViewController *thirdViewController=[[ThirdViewController alloc] init];
[self presentModalViewController:thirdViewController animated:YES];
[thirdViewController release];
}

notice the capital letters...

Build and Run succeeded.

But when i try that, it is normal if I switch between first and second view, and it crash when I switch to third view, what's wrong?
and is that any way to find what make the apps carsh?
Thanks