Wednesday, May 29, 2013

UIActionSheet dismissWithClickedButtonIndex animated example in Objective C (iOS).

UIActionSheet dismissWithClickedButtonIndex animated

Dismisses the action sheet immediately using an optional animation.

- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated

Parameters of [UIActionSheet dismissWithClickedButtonIndex animated]
buttonIndex
The index of the button that was clicked. Button indices start at 0.
animated
Specify YES to animate the dismissal of the action sheet or NO to remove the action sheet without an animation.

Discussion of [UIActionSheet dismissWithClickedButtonIndex animated]
You can use this method to dismiss the action sheet programmatically as needed. The action sheet also calls this method itself in response to the user tapping one of the buttons in the action sheet.

In iOS 4.0, you may want to call this method whenever your application moves to the background. An action sheet is not dismissed automatically when an application moves to the background. This behavior differs from previous versions of the operating system, where they were canceled automatically when the application was terminated. Dismissing the action sheet gives your application a chance to save changes or abort the operation and perform any necessary cleanup in case your application is terminated later.

UIActionSheet dismissWithClickedButtonIndex animated example.
-(void)dismissActionSheet {
   [sheet dismissWithClickedButtonIndex:0 animated:YES];
}

Example of [UIActionSheet dismissWithClickedButtonIndex animated].
-(void)dismissSheet{
    if (self.actionSheet){
        [self.actionSheet dismissWithClickedButtonIndex:0 animated:NO];
    }
}

-(void)viewDidLoad{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dismissSheet) name:UIApplicationWillResignActiveNotification object:nil];
        // Your other setup code
}

UIActionSheet dismissWithClickedButtonIndex animated example.
    -(void) dismissActionSheet:(id)sender {
        UIActionSheet *actionSheet =  (UIActionSheet *)[(UIView *)sender superview];
        [actionSheet dismissWithClickedButtonIndex:0 animated:YES];
    }

End of UIActionSheet dismissWithClickedButtonIndex animated example article.