Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Don't dismiss NSAlert when button is clicked

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?

like image 761
Jack Humphries Avatar asked Jan 16 '23 22:01

Jack Humphries


1 Answers

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.

like image 113
Jonathan Grynspan Avatar answered Jan 21 '23 11:01

Jonathan Grynspan