I have the following code in a ViewController in my OS X app:
NSAlert *alert = [NSAlert new];
alert.messageText = @"Connection error";
alert.informativeText = @"You do not appear to be connected to the internet";
[alert addButtonWithTitle:@"Third button"];
[alert addButtonWithTitle:@"Second button"];
[alert addButtonWithTitle:@"Ok"];
[alert beginSheetModalForWindow:[[self view] window] completionHandler:^(NSInteger result) {
NSLog(@"Success");
}];
// [alert runModal];
When this code executes nothing happens. If I comment out the beginSheetModalForWindow line, and uncomment [alert runModal], then the alert is displayed as expected.
What am I doing wrong here that it doesn't display as a sheet?
I imagine you are trying to show the NSAlert
too early (while the window is setting up) try adding a perfromselector with a delay to see if this is the case
[self performSelector:@selector(delayed) withObject:nil afterDelay:1.0];
-(void)delayed {
NSAlert *alert = [NSAlert new];
alert.messageText = @"Connection error";
alert.informativeText = @"You do not appear to be connected to the internet";
[alert addButtonWithTitle:@"Third button"];
[alert addButtonWithTitle:@"Second button"];
[alert addButtonWithTitle:@"Ok"];
[alert beginSheetModalForWindow:[self.view window] completionHandler:^(NSInteger result) {
NSLog(@"Success");
}];
}
If so, try showing it once the window has loaded, for example in
- (void)windowDidLoad {
//code here
}