2011-11-06 148 views
0

我試圖在點擊應用程序中的按鈕時加載新視圖。 我得到的錯誤是 -按下按鈕時切換視圖

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

和源代碼片斷 -

-(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]; 
} 

這裏是我使用的頭文件 -

#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 

鏈接到的Xcode項目是 - https://rapidshare.com/files/2403429896/View_Switcher.zip

+0

如果您僅取消註釋if語句之外的方法調用,會發生什麼情況? –

+0

而你的財產yellowViewController設置爲(保留)? –

+0

最終結果是一樣的(在模擬器中沒有任何反應),但我確實得到了一個新的警告 - 不兼容的Objective-C類型結構YellowViewController *',期望'結構UIViewController *'當傳遞參數1'pushViewController:animated :'來自不同的Objective-C類型 –

回答

3

pushViewController是一個UINavigationController方法。

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

我假設你是在一個UIViewController子類


你可以發佈的Xcode項目,我需要看到這一點。


好的,我查看了你的代碼,問題是你沒有任何navigationController。您的應用的結構類似於基本應用,不像導航基礎應用。
結果是,你的self.navigationController ==零,這就是爲什麼這個調用被忽略。

在您的應用程序委託你需要一些代碼看起來像這樣

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

在applicationFinised ...


在你的代碼,當你點擊你的交換機上的工具欄,它的「工作」,因爲您使用此代碼:

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

而且沒有導航控制器在那個過程中。

+0

是的,我在UIViewController子類,但是當我使用self.navigation控制器,我不能移動到下一個視圖。 –

+0

奇怪,當你使用self.navigationController時,你會得到一個錯誤或什麼? –

+0

不,沒有錯誤消息。它只是忽略點擊 –

1

UIView沒有pushViewController:animated:作爲一種方法。

可以推只能在UINavigationController的

+0

嗨,我刪除了動畫:是的,但仍然是相同的錯誤信息 –

+1

因爲整個pushViewController方法用於導航控制器 – Wesley

1

您應該使用[self.view addSubview:yellowController.view];

最好有一個導航控制器,你push和pop控制器或。