0

单击确定按钮时,我希望在不使用 segue 的情况下将用户重定向到另一个控制器。

   // here is my alert

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

有什么想法请分享。

4

4 回答 4

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];
于 2016-06-13T12:38:32.153 回答
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];
于 2016-06-13T12:39:58.903 回答
0

试试下面的代码,

 UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"DestinationController"];
 [self.navigationController pushViewController:controller animated:YES];
于 2016-06-13T12:40:14.947 回答
0

implement alertview delegate:

@interface firstviewcontroller : UIViewController<UIAlertViewDelegate>{
}

set your code: delegate= self in alertview;

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

Delegate method for 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];
}
于 2016-06-13T12:43:06.210 回答