2013-07-03 84 views
0

我在我的應用程序中有一個uialertview,有兩個按鈕,第一個是取消按鈕,第二個是好的。當我按下OK按鈕時,我想要轉到具有故事板標識符「RootView」的視圖控制器,但它不起作用。 我把下面的代碼放在appdelegate.m方法中。UIAlertView按鈕到另一個視圖控制器

- (void)showAlarm:(NSString *)text { 
     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"show alarm" 
                  message:text delegate:self 
                cancelButtonTitle:@"cancel" 
                otherButtonTitles:@"ok", nil]; 


     [alertView show]; 
    } 

    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ 

     if (buttonIndex == 0){ 
      NSLog(@"cancel"); 

     }else if (buttonIndex == 1){ 
      NSLog(@"ok"); 
      UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:[NSBundle mainBundle]]; 


      UINavigationController *navigationController = [storyboard instantiateViewControllerWithIdentifier:@"navigationController"]; 


      [navigationController presentViewController:[storyboard instantiateViewControllerWithIdentifier:@"RootView"] animated:YES completion:nil]; 


     } 
    } 
+0

是否日誌打印出「OK」或不? – Venkat

+0

是的,它打印,它的工作原理,但導航到另一個視圖控制器的操作不起作用 – CarinaM

回答

1

請參閱我的示例代碼工作對我來說

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    // Override point for customization after application launch. 
    [self showAlert]; 
    return YES; 
} 
-(void)showAlert 
{ 
    UIAlertView *alertForCall = [[UIAlertView alloc]initWithTitle:@"" message:@"time to choose" delegate:self cancelButtonTitle:@"YES" otherButtonTitles:@"NO", nil]; 
    [alertForCall show]; 
} 

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    if (buttonIndex == 1) 
    { 
     self.viewController = [[NewsTestViewController alloc] initWithNibName:@"NewsTestViewController" bundle:nil]; 
     UINavigationController *testNavi = [[UINavigationController alloc]initWithRootViewController:self.viewController]; 
     self.window.rootViewController = testNavi; 
     [self.window makeKeyAndVisible]; 

    } 
} 
+0

非常感謝你,我感謝你的幫助,它現在適用於我:) – CarinaM

+0

我相信你應該使用'didDismissWithButtonIndex'而不是'clickedButtonAtIndex'。前者在* UIAlertView從屏幕層次結構中移除後調用*,而後者在UIAlertView仍在屏幕上時發生。在屏幕上,UIAlertView改變了應用程序的性質;這意味着當試圖將視圖推送到導航控制器時會發生不好的事情。 –

3

你想推新的視圖控制器或模態?假設最後,也許這可以幫助你:

RootView *viewController = [storyboard instantiateViewControllerWithIdentifier:@"RootView"]; 
UINavigationController *navigationController = [UINavigationController alloc] initWithRootViewController:viewController]; 
[self presentViewController:navigationController animated:YES completion:nil]; 
+1

我打算添加自己的答案,但@leoformaggio基本上釘了它。 爲了進一步擴展答案,看看你的代碼,你正在創建一個導航控制器和視圖控制器。你是對的,因爲你把視圖控制器放在導航控制器中,但你忘了將導航控制器添加到視圖堆棧。沒有將導航控制器添加到堆棧,將不會發生任何事情。 – feliun

相關問題