2016-06-13 23 views
0

點擊確定按鈕我想ot重定向用戶到另一個控制器不使用segue。導航到另一個視圖後OK按鈕不點擊使用segue

// here is my alert 

    [[[UIAlertView alloc] initWithTitle:nil message:@"Profile details updated successfully." delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Ok", nil] show]; 

有什麼想法請分享。

+0

'UIAlertView'已棄用,以'UIAlertController'這是很多在添加操作的按鈕更靈活的替換。您應該使用它。 – Fogmeister

+0

你試過我的回答嗎? –

回答

4

你可以試試這個代碼,它爲你工作

UIAlertController * alert= [UIAlertController 
           alertControllerWithTitle:@"" 
           message:@"Profile details updated successfully." 
           preferredStyle:UIAlertControllerStyleAlert]; 

    UIAlertAction* ok = [UIAlertAction 
          actionWithTitle:@"OK" 
          style:UIAlertActionStyleDefault 
          handler:^(UIAlertAction * action) 
          { 
           //put your navigation code here 
           // *** THIS IS WHERE YOU NAVIGATE TO LOGIN 
           [self presentViewController:alert animated:YES completion:nil]; 
          }]; 


UIAlertAction* cancel = [UIAlertAction 
          actionWithTitle:@"Cancel" 
          style:UIAlertActionStyleDefault 
          handler:^(UIAlertAction * action) 
          { 
           //Put code for cancel here 

          }]; 

    [alert addAction:ok]; 
    [alert addAction:cancel]; 
+1

這是唯一正確的答案,其他人發佈了不推薦使用的方法..但這仍然是錯誤的,請參閱我的編輯回答 –

+0

同意。這是唯一正確的答案。 (使用@MSU_Bulldog編輯)。 – Fogmeister

2
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    if(buttonIndex == 0) //**Here is your ok button index** 
{ 
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; 
    LoginViewController *controller=[storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"]; 
    [self.navigationController pushViewController:controller animated:YES]; 
} 
} 

如上方法是IOS 8棄用,對於那些需要使用alertcontroller代替alertview如下

UIAlertController * alertController= [UIAlertController 
          alertControllerWithTitle:@"App name" 
          message:@"Update success" 
          preferredStyle:UIAlertControllerStyleAlert]; 

UIAlertAction *cancelAction = [UIAlertAction 
     actionWithTitle:NSLocalizedString(@"Cancel", @"Cancel action") 
        style:UIAlertActionStyleCancel 
       handler:^(UIAlertAction *action) 
       { 
        NSLog(@"Cancel action"); 
       }]; 

UIAlertAction *okAction = [UIAlertAction 
     actionWithTitle:NSLocalizedString(@"OK", @"OK action") 
        style:UIAlertActionStyleDefault 
       handler:^(UIAlertAction *action) 
       { 
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; 
        LoginViewController *controller=[storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"]; 
        [self.navigationController pushViewController:controller animated:YES]; 
       }]; 

[alertController addAction:cancelAction]; 
[alertController addAction:okAction]; 
[self presentViewController:alertController animated:YES completion:nil]; 
0

嘗試下面的代碼,

UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"DestinationController"]; 
[self.navigationController pushViewController:controller animated:YES]; 
0

實施alertview委託:

@interface firstviewcontroller : UIViewController<UIAlertViewDelegate>{ 
} 

設置你的代碼:委託=自我的alertview;

-[[[UIAlertView alloc] initWithTitle:nil message:@"Profile details updated successfully." delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Ok", nil] show]; 

爲alertview委託方法:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; 
    ViewController *controller=[storyboard instantiateViewControllerWithIdentifier:@"ViewController"]; 
    [self.navigationController pushViewController:controller animated:YES]; 
} 
+0

謝謝你的答案其實我的錯誤是我沒有設置委託= self @bhadresh – vicky

相關問題