2011-04-20 66 views
11

我用这个代码在uiactionsheet中显示uipicker,但是当我点击关闭按钮时,我想从视图中删除操作表。所以删除actionSheet表单视图的代码应该是什么。如何解雇行动表

- (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; 
} 
+0

我已经使用[actionsheet removeFromSuperview] ;但它不起作用。 – 2011-04-20 06:39:09

+0

请参阅http://stackoverflow.com/questions/1551587/how-to-dismiss-uiactionsheet-automatically – 2011-04-20 06:46:26

回答

11

所有你需要做的就是驳回ActionSheet,您可以用dismissWithClickedButtonIndex:animated:

+0

嘿,已经想通了自己:P – ttarik 2011-04-20 06:53:57

+0

感谢您的好帮手 – 2011-04-20 07:07:51

+0

斯威夫特怎么样? – dylan 2016-10-25 14:09:16

4

操作表范围问题。

使用[actionSheet dismissWithClickedButtonIndex:0 animated:YES];

+0

你在哪里使用?我试图创建一个dismissActionSheet方法,并将[actionSheet dismissWith ...]放入其中,但它似乎没有工作。 – 2012-01-02 13:54:45

7

添加此方法为我工作做的:

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

我的发件人是一个UIBarButtonItem,它被添加到一个可变数组中,该数组被添加到UIToolBar中,并作为子视图添加到操作表中。如何访问dismiss方法中的操作表? – marciokoko 2013-07-27 03:22:45