2015-10-05 13 views
15

UAlertView jest przestarzałe w systemie iOS 9 i nowszych wersjach. Jaka byłaby alternatywa?Alternatywa dla UIAlertView dla iOS 9?

UIAlertView *new = [[UIAlertView alloc] initWithTitle:@"Success" message:@"Your InApp Purchases were successfully restored" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
[new show]; 
+1

jakiegoś powodu po prostu nie wygląda na dokumentach do 'UIAlertView' (który dokładnie mówi, co robić) lub przeszukać przed opublikowaniem tego pytania? Przed wysłaniem postaraj się znaleźć odpowiedź. – rmaddy

+0

jeśli nie znajdziesz odpowiedzi w swoim dokumencie, lepiej przenieś swój projekt na iOS 8.: P –

Odpowiedz

45

Można użyć tego kodu, aby zastąpić powiadomienie widok:

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert];   
[alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]]; 
[self presentViewController:alertController animated:YES completion:nil]; 

Jeśli potrzebujesz wiele czynności można użyć:

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert]; 
[alertController addAction:[UIAlertAction actionWithTitle:@"Button 1" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { 
    // action 1 
}]]; 
[alertController addAction:[UIAlertAction actionWithTitle:@"Button 2" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { 
    // action 2 
}]]; 
[alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { 
    [self dismissViewControllerAnimated:YES completion:nil]; 
}]];   
[self presentViewController:alertController animated:YES completion:nil]; 
+1

To zajmuje tyle kodu, aby zaimplementować ... Co oni myślą? Możliwość obsłużenia odpowiedzi bez delegata jest jednak miłym dodatkiem. –

+0

@Kundapra Hudga, dlaczego zdecydowałeś się użyć dispatch_async dla [self presentViewController: alertController animated: YES completion: nil]; ? – Adela

+0

Używasz kontrolera UIAlertController, zobacz Swift AlertController: https://iosdevcenters.blogspot.com/2016/03/uialertcontroller-in-swift.html –

1
UIAlertController * alert= [UIAlertController 
            alertControllerWithTitle:@"My Title" 
            message:@"Enter User Credentials" 
            preferredStyle:UIAlertControllerStyleAlert]; 

    [self presentViewController:alert animated:YES completion:nil]; 
9

uzyskać szczegółowe informacje często łącznie z sugestią zastąpienie -clicking na symbol, który wyświetla deklarację klasy/metody.

W przypadku UIAlertView widać

"UIAlertView jest przestarzała. Używaj UIAlertController z preferredStyle z UIAlertControllerStyleAlert zamiast"

1

muszę użyć "UIAlertController" na iOS 8 i późniejszych. Zobaczmy:

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Success" message:@"Your InApp Purchases were successfully restored" preferredStyle:UIAlertControllerStyleAlert]; 

i dodać przyciski:

UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){ 
      //do something when click button 
}]; 

Pamiętaj:

[alertController addAction:okAction]; 

Następnie pokazać go:

[self presentViewController:alertController animated:YES completion:nill]; 

Jeśli chcesz pokazać actionsheep, ty zmień

"preferredStyle:UIAlertControllerStyleActionSheet" 
2
UIAlertController * alert= [UIAlertController 
          alertControllerWithTitle:@"Info" 
          message:@"You are using UIAlertController" 
           preferredStyle:UIAlertControllerStyleAlert]; 

    UIAlertAction* ok = [UIAlertAction 
        actionWithTitle:@"OK" 
        style:UIAlertActionStyleDefault 
        handler:^(UIAlertAction * action) 
        { 
         [alert dismissViewControllerAnimated:YES completion:nil]; 

        }]; 
    UIAlertAction* cancel = [UIAlertAction 
         actionWithTitle:@"Cancel" 
         style:UIAlertActionStyleDefault 
         handler:^(UIAlertAction * action) 
         { 
          [alert dismissViewControllerAnimated:YES completion:nil]; 
         }]; 
[alert addAction:ok]; 
[alert addAction:cancel]; 

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