隐藏UIAlertView中在客观-c

问题描述:

我有两个方法出现和消失UIAlertView中隐藏UIAlertView中在客观-c

- (void)showAlert { 

      UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@"My Alert" 
                   message:@"Do you want to continue?" 
                  delegate:self 
                cancelButtonTitle:nil 
                otherButtonTitles:@"No", @"Yes", nil]; 
      [myAlert show]; 

     } 

// dismiss uialert 

     -(void)dismiss:(UIAlertView*)alert{ 
         [alert dismissWithClickedButtonIndex:0 animated:YES]; 
          } 

我有,当我想打电话给我辞退方法的问题,我不知道该怎么myAlert传递给解除方法以隐藏UIAlertView。

[self dismiss: // how to pas myAlert]; 
+0

你想自动解雇alertview没有选择是或否?请简洁明了 – Arun

+1

UIAlertView中已被弃用。请参阅[本答案](http://*.com/questions/4463806/adding-a-simple-uialertview/28383410#28383410)了解最新的示例。 – FreeNickname

您需要创建全球UIAlertView中对象。

YourController.h

@property (strong, nonatomic) UIAlertView *myAlertView; 

YourController.m

-(void)showAlert 
{ 
myAlertView = nil; 
myAlertView = [[UIAlertView alloc] initWithTitle:@"My Alert" 
                 message:@"Do you want to continue?" 
                delegate:self 
              cancelButtonTitle:nil 
              otherButtonTitles:@"No", @"Yes", nil]; 
    [myAlert show]; 
} 

-(void)dismiss 
{ 
     [myAlertView dismissWithClickedButtonIndex:0 animated:YES]; 
} 
+0

非常感谢@Nirmalsinh。 – Steven

UIAlertView中不赞成使用UIAlertController

使用语法如下:

 UIAlertController* alert = [UIAlertController 
               alertControllerWithTitle:@"SUCCESS" 
               message:@"Profile picture updated successfuly." 
//automatically 2 sec alert will disappear            preferredStyle:UIAlertControllerStyleAlert]; 
        [self performSelector:@selector(abc:) withObject:alert afterDelay:2]; 

        UIAlertAction* ok = [UIAlertAction 
             actionWithTitle:@"OK" 
             style:UIAlertActionStyleDefault 
             handler:^(UIAlertAction * action) 
             { 
             }]; 

        [alert addAction:ok]; 
        [self presentViewController:alert animated:YES completion:nil]; 

当u要解雇乌尔UIAlertController

-(void)abc:(UIAlertController*)x{ 
    [x dismissViewControllerAnimated:YES completion:nil]; 
} 

@st即使你不需要创建任何解雇警报的方法。但我想你也检查下面的链接,因为UIAlertview已被弃用,你可以使用UIAlertcontroller而不是它。 链接: UIAlertView first deprecated IOS 9

+1

苹果允许在OS 9之后。所以现在我们可以再次使用它。 – Nirmalsinh

+0

这太棒了! thx @Nirmalsinh来更新我。 –