2015-04-03 11 views
23

Mam kod do utworzenia i NSAlert w Objective-C, ale chciałbym teraz utworzyć go w Swift.Utwórz NSAlert z Swift

Ten alert służy do potwierdzenia, że ​​użytkownik chce usunąć dokument.

Chciałbym przycisk "usuń", aby uruchomić funkcję kasowania i "anuluj", aby odrzucić alert.

Jak mogę to napisać w Swift?

Dzięki

NSAlert *alert = [[[NSAlert alloc] init] autorelease]; 
    [alert addButtonWithTitle:@"Delete"]; 
    [alert addButtonWithTitle:@"Cancel"]; 
    [alert setMessageText:@"Delete the document?"]; 
    [alert setInformativeText:@"Are you sure you would like to delete the document?"]; 
    [alert setAlertStyle:NSWarningAlertStyle]; 
    [alert beginSheetModalForWindow:[self window] modalDelegate:self didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:) contextInfo:nil]; 
+0

może warto rozważyć, że 'beginSheetModal (dla: completionHandler:)' * nie * jest przestarzałe, w rzeczywistości może to być bardziej pożądany sposób obsługi modalnego okna dialogowego (w bloku). Byłoby to bliższe starej drodze z 'didEndSelector' i nie zatrzyma całej aplikacji. – Patru

Odpowiedz

78

beginSheetModalForWindow:modalDelegate jest przestarzałe w OS X 10.10 Yosemite.

Swift 2

func dialogOKCancel(question: String, text: String) -> Bool { 
    let alert: NSAlert = NSAlert() 
    alert.messageText = question 
    alert.informativeText = text 
    alert.alertStyle = NSAlertStyle.WarningAlertStyle 
    alert.addButtonWithTitle("OK") 
    alert.addButtonWithTitle("Cancel") 
    let res = alert.runModal() 
    if res == NSAlertFirstButtonReturn { 
     return true 
    } 
    return false 
} 

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

ta zwraca true lub false według wyboru użytkownika.

NSAlertFirstButtonReturn reprezentuje pierwszy przycisk dodany do okna dialogowego, tutaj "OK".

Swift 3

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 
} 

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

Swift 4

Mamy teraz używać stałe teksty alertu w stylu i wybór przycisku.

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

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

Upewnij się, że "importuj AppKit" (przynajmniej w wersji 3) – Claude

+1

@Claude Jeśli robisz alert, oznacza to, że robisz aplikację Cocoa i oznacza to, że importujesz kakao, które już importuje AppKit. – Moritz

+0

Prawdopodobnie nie powinienem wywoływać alarmu z klasy innej niż VC; ale chciałem po prostu wyświetlić komunikat o błędzie biedaka. Ta klasa wykorzystania nie importowała niczego poza fundamentem, więc potrzebowałem importowania (przynajmniej uczynił XCode szczęśliwym). – Claude

13

myślę, to może pracować dla Ciebie ...

let a = NSAlert() 
    a.messageText = "Delete the document?" 
    a.informativeText = "Are you sure you would like to delete the document?" 
    a.addButtonWithTitle("Delete") 
    a.addButtonWithTitle("Cancel") 
    a.alertStyle = NSAlertStyle.WarningAlertStyle 

    a.beginSheetModalForWindow(self.view.window!, completionHandler: { (modalResponse) -> Void in 
     if modalResponse == NSAlertFirstButtonReturn { 
      print("Document deleted") 
     } 
    })