Ashish Agarwal
Ashish Agarwal

Reputation: 14925

Switching views when pressing button

I am trying to load a new view when I click a button in the app. The error I am getting is -

View Switcher[6867:207] -[UIView pushViewController:animated:]: unrecognized selector sent to instance 0x6010660

and the source code snippet is -

-(IBAction) blueButtonPressed:(id)sender{

if(self.yellowViewController == nil){
    YellowViewController *yellowController = [[YellowViewController alloc] initWithNibName:@"YellowView" bundle:nil];
    self.yellowViewController = yellowController;
    //[yellowController release];

    //[self.view addSubview:yellowController.view];
//[self.view pushViewController:self.yellowViewController];
    [self.navigationController pushViewController:self.yellowViewController];
[yellowController release];
}
//[self.navigationController pushViewController:self.yellowViewController animated:YES];
}

Here is the header file I am using -

#import <UIKit/UIKit.h>

@class BlueViewController;
@class YellowViewController;


@interface BlueViewController : UIViewController {  
YellowViewController *yellowViewController;
BlueViewController *blueViewController; 
}
-(IBAction)blueButtonPressed:(id)sender;

@property(retain) YellowViewController *yellowViewController;
@property(retain, nonatomic) BlueViewController *blueViewController;
@end

The link to the xcode project is - https://rapidshare.com/files/2403429896/View_Switcher.zip

Upvotes: 0

Views: 503

Answers (3)

Vincent Bernier
Vincent Bernier

Reputation: 8664

pushViewController is a UINavigationController method.

[self.navigationController pushViewController:self.yellowViewController animated:YES];

I'm assuming you are in a UIViewController subclass


Could you post an Xcode project I need to see this.


Ok, I've look into your code, the problem is that you don't have any navigationController. Your app is structured like a view base application not like a navigation base application.
The result is that your self.navigationController == nil, that is why that call is ignored.

In your application delegate you need some code looking like this

UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:switchViewController];
navCon.navigationBarHidden = YES;
self.window.rootViewController = navCon;
[window makeKeyAndVisible];

In the applicationFinised...


In your code, when you click on your switch in the toolbar, it's "working" because your using this code :

[blueViewController.view removeFromSuperview];
[self.view insertSubview:yellowViewController.view atIndex:0];

And there is no navigationController in that process.

Upvotes: 3

Volodymyr B.
Volodymyr B.

Reputation: 3441

UIView does not have pushViewController:animated: as a method.

You can push only in UINavigationController

Upvotes: 1

Wesley
Wesley

Reputation: 2200

You should use [self.view addSubview:yellowController.view];

Best to have a navigation controller where you push and pop controllers to or from.

Upvotes: 1

Related Questions