how to dismiss action sheet

2019-02-21 15:05发布

i used this code to show uipicker in uiactionsheet but when i click close button i want to remove action sheet from view. so what should be the code for removing actionSheet form view.

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];

[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];

CGRect pickerFrame = CGRectMake(0, 40, 0, 0);

pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;

[actionSheet addSubview:pickerView];
[pickerView release];

UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Close"]];
closeButton.momentary = YES; 
closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor blackColor];
[closeButton addTarget:self action:@selector(dismissActionSheet:) forControlEvents:UIControlEventValueChanged];
[actionSheet addSubview:closeButton];
[closeButton release];

[actionSheet showInView:self.view];//[UIApplication mainWindow]];

[actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
 return false;
}

3条回答
我欲成王,谁敢阻挡
2楼-- · 2019-02-21 15:07

Adding this method worked for me:

    -(void) dismissActionSheet:(id)sender {
        UIActionSheet *actionSheet =  (UIActionSheet *)[(UIView *)sender superview];
        [actionSheet dismissWithClickedButtonIndex:0 animated:YES];
    }
查看更多
Bombasti
3楼-- · 2019-02-21 15:18

action sheet scope problem.

used [actionSheet dismissWithClickedButtonIndex:0 animated:YES];

查看更多
贼婆χ
4楼-- · 2019-02-21 15:19

All you need to do is dismiss the ActionSheet, which you could do with dismissWithClickedButtonIndex:animated:

查看更多
登录 后发表回答