Reputation: 13267
I'm showing an NSAlert with a custom view and three buttons. The custom view has two text fields and it allows the user to log in.
In the Mac App Store, an NSAlert with a similar design comes up. When the user clicks the sign in button, the NSAlert does not dismiss (until the credentials are verified). How does Apple keep the alert up?
Upvotes: 4
Views: 3373
Reputation: 188
Swift 4 style
let alert = NSAlert()
alert.alertStyle = .informational
alert.messageText = "Y/N?"
alert.addButton(withTitle: "Y")
alert.addButton(withTitle: "N")
guard let window = view.window else { return } // for NSViewController
alert.beginSheetModal(for: window) { res in
if res == .alertFirstButtonReturn {
// "Y" action
} else {
// "N" action
}
}
Upvotes: -1
Reputation: 43472
Get the NSButton
you want to behave differently. Change its target and action. (To call through to the original target/action, preserve them before you change them.)
NSAlert *alert = ...;
NSButton *button = [[alert buttons] objectAtIndex:...];
id oldTarget = [button target];
SEL oldAction = [button action];
[button setTarget:self];
[button setAction:@selector(verifyCredentials:)];
Alternatively, you may want to build the alert as a custom window controller and XIB (which is how Apple did it in the case of the App Store.) In that case, you have fine-grained control over button behaviour.
Upvotes: 11