Search code examples
objective-ccocoawindow

Objective-c action before closing the window


How can i assign an action for close button in my window in objective-c? For example, user clicks on close button and then program asks: "Do you really want to close the window?"


Solution

  • If you want to add message before closing view in Application you need to implement UIAlertView in it, don't forget to add < UIAlertViewDelegate> in .h file

    code in action button pressed;

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message" 
                                                    message:@"Do you really want to close the window?" 
                                                    delegate:self 
                                                    cancelButtonTitle:@"OK" 
                                                    otherButtonTitles:nil];
    [alert show];
    

    If you want to do something when the button is clicked, implement this delegate method:

    - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
            // the user clicked OK
            if (buttonIndex == 0) {
                // Write Close Code here.....
    
       }
    }