I want to seek confirmation with the user that he wants to close the only window and with that the whole app
So far I have this:
- (BOOL)windowWillClose:(id)sender{
NSAlert *alert = [[NSAlert alloc] init];
[alert addButtonWithTitle:#"Yes"];
[alert addButtonWithTitle:#"No"];
[alert setMessageText:#"Are you sure you want to quit?"];
[alert setInformativeText:#"Quiting will stop the machine, please make sure it is back to its origin."];
[alert setAlertStyle:NSWarningAlertStyle];
[alert setShowsSuppressionButton:YES];
NSInteger result = [alert runModal];
if ( result == NSAlertFirstButtonReturn ) {
return YES;
} else {
return NO;
}
}
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender {
return YES;
}
The window and application close before there is a allert, if I remove applicationShouldTerminateAfterLastWindowClosed my window closes, nothing happens. but when I switched it up and put the allert in applicationShouldTerminateAfterLastWindowClosed the allert worked but by than my window is already closed.
I also tried it with windowShouldClose but that didn't work either.
Any ideas on what I am doing wrong here?
Add a delegate that implements the
- (BOOL)windowShouldClose:(id)sender;
delegate method, see here: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/NSWindowDelegate_Protocol/Reference/Reference.html
Related
I have the following code which expects to show alert as sheet in AppDelegate.m.
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender {
if ([self.socket.inputStream streamStatus] == 2) {
NSStoryboard *storyBoard = [NSStoryboard storyboardWithName:#"Main" bundle:nil];
NSWindowController *mainWindowController = [storyBoard instantiateControllerWithIdentifier:#"MainWindow"];
NSAlert *alert = [[NSAlert alloc] init];
[alert addButtonWithTitle:#"OK"];
[alert setMessageText:NSLocalizedString(#"Warning", #"Warning")];
[alert setInformativeText:NSLocalizedString(#"Disconnect before quit this app!!", #"Disconnet before quit")];
[alert beginSheetModalForWindow:mainWindowController.window completionHandler:^(NSModalResponse returnCode) {
}];
return NO;
} else {
return YES;
}
}
But unfortunately, the result alert does not shown as sheet. Like the screenshot.
I can't understand why. And would like to know how can I show alert as sheet. Please help me!!
It worked for me. I don't know much about.
May be it is because of the part beginSheetModalForWindow:
In your question, it seems that beginSheetModalForWindow:mainWindowController.window
I changed it from mainWindowController.window to self.view.window and it worked, as it is given below:
[alert beginSheetModalForWindow:self.view.window completionHandler:^(NSModalResponse returnCode)
{
....
}];
May be it will help i think.
I'm looking to use a UIActionSheet kind of like a contextual message box (with no action buttons at all and just a label in the bubble with an arrow pointing at something). Since there are no actions the user can take, I would like it to not require a tap to dismiss, but I can't see any way (such as a passthroughViews property) to allow this.
It's probably not designed for this, but it does happen to be handy for it.
This is some example code of how to show an UIAlertView and dismiss it automatically.
Show it:
- (void)show
{
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:#"title"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:nil];
alert.tag = tag;
[alert show];
}
Dismiss it:
- (void)dismiss
{
for (UIWindow* w in [UIApplication sharedApplication].windows)
for (NSObject* o in w.subviews)
if ([o isKindOfClass:[UIAlertView class]]) {
UIAlertView *alert = (UIAlertView*) o;
if (alert.tag == tag)
[alert dismissWithClickedButtonIndex:[(UIAlertView*)o cancelButtonIndex] animated:YES];
}
}
Yo can call the dismiss method after a couple of seconds:
[self performSelector:#selector(dismiss) withObject:nil afterDelay:1.0 inModes:nil];
I want to show a box and proceed with my code based on user's input. UIAlertView does not work as the application does not wait for the input. So is there another type of widget that can wait for user input and then pass control to the app ?
I know there are duplicates here but they are not very helpful as I seem to be missing some pieces (completely new to objective c...)
-(void)function1{
...
// Add UIAlertView *alert; to .h file
alert = [[UIAlertView alloc] initWithTitle:#"" message:#"" delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
[alert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(alertView == alert && buttonIndex == 1){ // If yes
[self function2];
}
}
-(void)function2{
...
//Continue your task
}
you want a MODAL alert -- just use a UIAlertView and wait for its delegate before proceeding.
- a {
alert.delegate = self;
[alert show];
}
-alertView:(id)a didDismissWithButtonIndex:(int)i {
[self proceed];
}
- proceed {
... after alert ...
}
hi guys I have a method called manageui which display a waiting view for a while and when the times out it's display UIAlertView which display a message for try again
My problem is that i can't hide the UIAlertView before calling manageui called
here is my code :
-(void)mangeui
{
double Currenttime=0;
double ptime=Currenttime+5000;
NSLog(#"fire /n");
do
{
//add condition for found session
if (Currenttime<ptime)
{
NSLog(#"inside if");
[spinner setHidden:NO];
[alert setHidden:YES];
}
else
{
alert = [[UIAlertView alloc] initWithTitle:#"Oops:("
message:#"No device found \n Make sure bluetooth is activated and the devices are within range."
delegate:self
cancelButtonTitle:#"Tap to retry"
otherButtonTitles:nil];
[spinner setHidden:YES];
[alert show];
}
Currenttime+=1;
} while (Currenttime < ptime+1 &&[_matchmakingClient availableServerCount]==0);
}
the delegate for alertview is :
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
[alertView dismissWithClickedButtonIndex:1 animated:true];
[spinner setHidden:NO];
alertView.hidden=YES;
[self mangeui];
}
Try this:
[alertView dismissWithClickedButtonIndex:0 animated:YES];
Well I think you didn't understand the concept of delegates
here in docs it says alertView:clickedButtonAtIndex:
The receiver is automatically dismissed after this method is invoked.
yeah there is no need to declare separately to dismiss the alert view.The method is called whenever a button in alertview is pressed and the alertview disappears
Does somebody see something wrong with this? I have an UIAltertView but get an EXC_BAD_ACCESS when I click any of the two buttons:
UIAlertView *alert = [[UIAlertView alloc] init];
[alert setTitle:#"First Sync"];
[alert setMessage:#"The App is going to do its first synchronisation. This might take a few moment..."];
[alert setDelegate:self];
[alert addButtonWithTitle:#"OK"];
[alert addButtonWithTitle:#"Cancel"];
[alert show];
[alert release];
and to catch the response:
#pragma mark UIAlertView
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex != 0)
{
NSLog(#"TEST1");
return;
}
NSLog(#"TEST2");
}
It must be something simple...
You'll have to use the designated initializer:
initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles:
As hinted above, the problem was that the UIAlert was generated in a thread. By using an NSCondition and firing the Alert into a background thread I got the worker thread to wait while the background thread waits for a response from the user. Once the response comes it signals the worker thread to continue (as the required data is stored in the database at that point).