2012-08-24 65 views
3

我是一個新手iOS開發人員,最近開發了幾個Android應用程序,但我不熟悉iOS的行話。讓我解釋我的問題。我想使用兩種不同的UIViewController。我爲兩個控制器創建了.h.m文件。我的計劃是在第一個視圖控制器出現在屏幕上五秒鐘後,將第二視圖控制器推到第一視圖控制器的頂部。我的意思是第一個視圖控制器就像啓動畫面或類似的東西。推送ViewController給NSInvalidArgumentException

這是我的貢獻。在第一視圖控制器,I所定義(其中之一當然實現)這兩種方法:

-(void) pushSecondController { 
    SecondViewController *secondController = [[SecondViewController alloc] 
               initWithNibName: nil 
               bundle: NULL]; 
    [self.navigationController pushViewController: secondController animated: YES]; 
} 

-(void)viewDidAppear:(BOOL)animated { 
    [super viewDidAppear:animated]; 
    [self performSelector: @selector(pushViewController:animated:) 
       withObject: nil 
       afterDelay: 5.0f]; 
} 

和所述第二視圖控制器看起來像這樣:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.view.backgroundColor = [UIColor whiteColor]; 
    self.title = @"Second Controller"; 
} 

我已經改變僅viewDidLoad方法。當我運行模擬器時,第一個視圖控制器運行良好,等待5秒鐘並墜毀。輸出看起來像:

2012-08-24 10:46:34.104 NavApplication[20355:f803] -[ViewController pushViewController:]: unrecognized selector sent to instance 0x6e7f780 
2012-08-24 10:46:34.107 NavApplication[20355:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ViewController pushViewController:]: unrecognized selector sent to instance 0x6e7f780' 

讓我問一個問題:我知道有methodNamemethodName:之間的差異。任何人都可以解釋有什麼區別?

任何幫助,將不勝感激。

UPDATE:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    // Override point for customization after application launch. 
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; 
    self.window.rootViewController = self.viewController; 
    self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController]; 
    [self.window makeKeyAndVisible]; 
    [self.window addSubview: self.navigationController.view]; 
    return YES; 
} 
+0

你自己設置的self.navigationController?你可以發佈你的應用委託didFinishLaunchingWithOptions方法嗎? – CarlJ

+0

更新了我的問題,我可以看到,我設置了'navigationController'。 –

回答

3

變化@selector(pushViewController:animated:)@selector(pushSecondController)

+0

首先,謝謝。我已經改變,並得到這個錯誤:''NSInvalidArgumentException',原因:' - [ViewController pushViewController]:無法識別的選擇器發送到實例'。 –

+0

我猜你已將它改爲'@selector(pushViewController)'而不是'@selector(pushSecondController)'? – Ander

+0

是的,你是對的。我錯過了那個虛假的錯字。再次感謝你,現在它正在工作。 –

3

正如上面提到的,你可能想改變你的performSelector命令:因爲你想它來調用你的pushSecondController方法

[self performSelector: @selector(pushSecondController) 
      withObject: nil 
      afterDelay: 5.0f]; 

,而不是pushViewController:animated:

關於第二個問題:methodNamemethodName:之間的差別在於,在:methodName:結束意味着這種方法需要一個參數。所以,你可以有以下幾種方法:

- (void)listItems 
{ 
    // ... 
} 

- (void)insertItem:(NSDictionary *)item 
{ 
    // ... 
} 

時,順便提到他們進入@selector,爲你只是做@selector(listItems)第一種方法,因爲它不帶任何參數,對於後者,你會怎麼做@selector(insertItem:),因爲它需要一個參數。

UPDATE

只看見自己的applicationDidLaunch代碼。您可能需要重新安排事項,以便將ViewController添加到UINavigationController,然後將UINavigationController設置爲窗口的rootViewController。像這樣:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    // Override point for customization after application launch. 

    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; 
    self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController]; 

    self.window.rootViewController = self.navigationController; 

    [self.window makeKeyAndVisible]; 
    return YES; 
} 
+0

詹姆斯,很好的回答。現在我接近這個廣泛。但是很抱歉,我已經接受了一個答案,並且我沒有足夠的代表進行upvoting。無論什麼時候,我都會立刻起來。 –

+0

謝謝!我認爲你可以不接受一個答案並接受另一個答案? –

0

請改變你的didFinishLaunchingWithOptions方法:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
    { 
     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
     // Override point for customization after application launch. 
     self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; 
     self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController]; 

      self.window.rootViewController = self.navigationController; 

      [self.window makeKeyAndVisible]; 

      return YES; 
    } 
相關問題