2013-08-24 17 views

Odpowiedz

30

Możesz użyć NSAlert w kakao. To jest to samo, co UIAlertView w ios. można pop-up alert o tym

NSAlert *alert = [NSAlert alertWithMessageText:@"Alert" defaultButton:@"Ok" alternateButton:@"Cancel" otherButton:nil informativeTextWithFormat:@"Alert pop up displayed"]; 
[alert runModal]; 

EDIT:

Jest to najnowsza metoda stosowana jak wyżej metody jest przestarzałe teraz.

NSAlert *alert = [[NSAlert alloc] init]; 
[alert setMessageText:@"Message text."]; 
[alert setInformativeText:@"Informative text."]; 
[alert addButtonWithTitle:@"Cancel"]; 
[alert addButtonWithTitle:@"Ok"]; 
[alert runModal]; 
+0

Ta metoda jest przestarzała. Cytaty z dokumentacji Apple - "Przestarzałe, zamiast tego przydzielaj i inicjuj obiekt NSAlert i ustaw odpowiednio jego atrybuty". –

+1

Tak Ta metoda jest teraz przestarzała. Ale nadal możesz z tego skorzystać. W każdym razie edytuję moją odpowiedź na nową metodę wyświetlania alertu. – Surjeet

5

Istnieje sprytnie nazwana klasa NSAlert, która może wyświetlać okno dialogowe lub arkusz do przedstawienia alertu.

6

Swift 3,0

let alert = NSAlert.init() 
alert.messageText = "Hello world" 
alert.informativeText = "Information text" 
alert.addButton(withTitle: "OK") 
alert.addButton(withTitle: "Cancel") 
alert.runModal() 
1

Swift 3,0 Przykład:

Wyjaśnienie:

func showCloseAlert(completion : (Bool)->Void) { 
     let alert = NSAlert() 
     alert.messageText = "Warning!" 
     alert.informativeText = "Nothing will be saved!" 
     alert.alertStyle = NSAlertStyle.warning 
     alert.addButton(withTitle: "OK") 
     alert.addButton(withTitle: "Cancel") 
     completion(alert.runModal() == NSAlertFirstButtonReturn) 
} 

Zastosowanie:

showCloseAlert { answer in 
     if answer == true{ 
      self.dismissViewController(self) 
     } 
    } 
0

można użyć tej metody w Swift

func dialogOKCancel(question: String, text: String) -> Bool 
     { 
      let alert = NSAlert() 
      alert.messageText = question 
      alert.informativeText = text 
      alert.alertStyle = NSAlertStyle.warning 
      alert.addButton(withTitle: "OK") 
      alert.addButton(withTitle: "Cancel") 
      return alert.runModal() == NSAlertFirstButtonReturn 
     } 

a następnie wywołać go w ten sposób

let answer = dialogOKCancel(question: "Ok?", text: "Choose your answer.") 

odpowiedź będzie prawda czy fałsz na wybranie „OK” lub „Anuluj”, odpowiednio.

Powiązane problemy