Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create action when I click on a button than UIAlertcontroller

i Create two buttons of UIAlertcontroller:

One Button - "OpenCamera"
Two button - "OpenGallery"

I just can not understand how I create action when I click on one of them.

- (IBAction)takePic:(id)sender {


UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil
                                                               message:nil
                                                        preferredStyle:UIAlertControllerStyleActionSheet]; // 1
UIAlertAction *openCamrea = [UIAlertAction actionWithTitle:@"open camrea"
                                                      style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
                                                      }];
UIAlertAction *openGallery = [UIAlertAction actionWithTitle:@"open gallery"
                                                       style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
                                                       }];

[alert addAction:openCamrea];
[alert addAction:openGallery];

[self presentViewController:alert animated:YES completion:nil];
}
like image 316
mimpami Avatar asked Mar 17 '26 08:03

mimpami


1 Answers

The handler is the block to be executed on the selection of the item.

UIAlertAction *openGallery = [UIAlertAction
    actionWithTitle:@"open gallery"
    style:UIAlertActionStyleDefault
    handler:^(UIAlertAction * action) {
        // Code to run when the open gallery option is pressed.
    }];

BTW I think the long unbroken lines in the question really don't help as they effectively hide the key parameter.

like image 178
Joseph Lord Avatar answered Mar 18 '26 20:03

Joseph Lord



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!