user1022511
user1022511

Reputation: 45

Linking UIActionSheet buttons with a current IBAction

I have got created a UIActionsheet programatically however I can't figure out how to link it to an existing IBAction.

    -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {
    self.label.text = @"Destructive Button Clicked";
          }}

This is my code where i click on a button and I want it to link to the following IBAction that i have already got.

    - (IBAction)doButton;

So, how would i go about linking this? Any help would be greatly appreciated. Thanks

Upvotes: 1

Views: 491

Answers (2)

shoughton123
shoughton123

Reputation: 4251

The best way to do it is to add a UIActionSheetDelegate to your class and call the action sheet in your IBAction.

.h file

@interface ViewController : UIViewController <UIActionSheetDelegate>

-(IBAction)doButton;

@end

.m file

-(IBAction)doButton {

UIActionSheet *doSheet = [[UIActionSheet alloc] initWithTitle:@"Your title" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Button 0", @"Button 1", nil];

[doSheet showInView:self.view];
}

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex: (NSInteger)buttonIndex {
       if (buttonIndex == 0) {
             //Do Something
       }
       if(buttonIndex == 1) {
            //Do Something else
       }
}

Upvotes: 0

James
James

Reputation: 2376

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex: (NSInteger)buttonIndex {
if (buttonIndex == 0) {
self.label.text = @"Destructive Button Clicked";
[self doButton];
}

}

Upvotes: 1

Related Questions