2011-04-20 82 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