0

I have a UIActionSheet and I have added the following - UIPickerView and 2 BarButtonItems. I am trying to close the action sheet when someone clicks the Cancel Button (it is one of the bar button items).

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles: nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];

CGRect pickerFrame = CGRectMake(0,40,0,0);
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;

[actionSheet addSubview:pickerView];
UIToolbar *tools = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 40)];
tools.barStyle = UIBarStyleBlackOpaque;
[actionSheet addSubview:tools];

UIBarButtonItem *doneButton=[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(btnActinDoneClicked)];
doneButton.imageInsets=UIEdgeInsetsMake(200, 6, 50, 25);
UIBarButtonItem *CancelButton=[[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(btnActinCancelClicked)]; //this is where the problem is

NSArray *array = [[NSArray alloc]initWithObjects:CancelButton, doneButton, nil];
[tools setItems:array];

[actionSheet showFromTabBar:self.tabBarController.tabBar];
[actionSheet setBounds:CGRectMake(0, 0, 320, 485)];

I have added another method to dismiss the action sheet -

-(IBAction)btnActinCancelClicked:(id)sender{
    [sender dismissWithClickedButtonIndex:0 animated:YES];
}

The problem is that the app crashes when the BarButton is clicked. I think the problem is caused where the CancelButton is declared and it is unable to send a message to the btnActinCancelClicked method, but I can't figure out how to fix it.

Thanks

4

2 回答 2

1

It's not the sender you want to dismiss. It's the ActionSheet. You'll need a reference for that and change your code for something like:

- (IBAction)btnActinCancelClicked:(id)sender{
    [self.actionSheet dismissWithClickedButtonIndex:0 animated:YES];
}
于 2012-08-28T19:21:24.290 回答
1

Your method is this;

-(IBAction)btnActinCancelClicked:(id)sender

So you need to add it like this (because it receives a parameter);

@selector(btnActinCancelClicked:)

So for example, if a method had three parameters you'd call it @selector(myMethod:andX:andY:)

Also, to access your UIActionSheet (not entirely sure this will work);

UIActionSheet *acSheet = sender.superView;

//Check if it is the ActionSheet here then dismiss it

[acSheet dismissWithClickedButtonIndex:0 animated:YES];
于 2012-08-28T19:23:06.527 回答