在不使用segue的情况下点击OK Alert Button后导航到另一个View

时间:2016-06-13 12:33:06

标签: ios objective-c uialertview uialertaction

单击确定按钮我想要将用户重定向到另一个控制器而不使用segue。

   // here is my alert

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

有任何想法请分享。

4 个答案:

答案 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)

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

答案 2 :(得分:0)

尝试以下代码,

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

答案 3 :(得分:0)

实施alertview委托:

@interface firstviewcontroller : UIViewController<UIAlertViewDelegate>{
}

设置你的代码:delegate = self in 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];
}