2017-05-03 48 views
0

我有兩個方法出現和消失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://stackoverflow.com/questions/4463806/adding-a-simple-uialertview/28383410#28383410)瞭解最新的示例。 – FreeNickname

回答

1

您需要創建全球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

1

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]; 
} 
0

@st即使你不需要創建任何解僱警報的方法。但我想你也檢查下面的鏈接,因爲UIAlertview已被棄用,你可以使用UIAlertcontroller而不是它。 鏈接: UIAlertView first deprecated IOS 9

+1

蘋果允許在OS 9之後。所以現在我們可以再次使用它。 – Nirmalsinh

+0

這太棒了! thx @Nirmalsinh來更新我。 –