2011-05-21 121 views
2

我有一个UIActionSheet来选择在我的应用程序中使用的时间。选择时间工作正常,但我无法找到一种方法来隐藏操作表单击“完成”按钮时。查找家长UIActionSheet

我的代码是:

-(void) showTimespanPicker{ 
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"Preparation Time", "Preparation Time") 
    delegate:self         cancelButtonTitle:nil//@"Cancel" 
destructiveButtonTitle:nil 
otherButtonTitles:nil]; 

    [actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent]; 
    actionSheet.tag = PREPTIME_PICKER; 

    CGRect pickerFrame = CGRectMake(0, 40, 0, 0); 
    prepTimePicker = [[UIDatePicker alloc] initWithFrame:pickerFrame]; 
    prepTimePicker.datePickerMode = UIDatePickerModeCountDownTimer; 
    prepTimePicker.countDownDuration = (NSTimeInterval)[recipe.prepTime doubleValue]; 

    [actionSheet addSubview:prepTimePicker]; 
    [prepTimePicker release]; 

    // create toolbar with "Done" button 
    UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 40)]; 
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(prepTimeSelected)]; 
    NSArray *barButtons = [NSArray arrayWithObject:barButtonItem]; 
    [toolbar setItems:barButtons]; 
    [actionSheet addSubview:toolbar]; 

    MyAppAppDelegate *delegate = [[UIApplication sharedApplication] delegate]; 
    [actionSheet showInView:delegate.window]; 
    [actionSheet setBounds:CGRectMake(0, 0, 320, 485)]; 
} 
// fires when the done button is clicked 
-(void)prepTimeSelected { 
    recipe.prepTime = (NSInteger)prepTimePicker.countDownDuration; 
    [self updatePickerValues]; 
    [[prepTimePicker.superview].setHidden:YES]; 
} 

在prepTimeSelected最后一行隐藏的UIDatePicker,但不是actionsheet。我想我需要某种递归方式来查找按钮的父项,或者为ActionSheet创建一个类变量/属性,但这种方式感觉不对。

+0

你有没有得到这个解决方案?我有同样的问题。 – 2011-11-03 18:28:01

回答

0
UIActionSheet *menuProperty;  

@property(nonatomic,retain) UIActionSheet *menuArea; 

然后您需要在您的.m文件

menuArea = [[UIActionSheet alloc] initWithTitle:nil delegate:self 
             cancelButtonTitle:@"Done" 
            destructiveButtonTitle:nil 
             otherButtonTitles:nil]; 


// Add the picker 
pickerArea = [[UIPickerView alloc] initWithFrame:CGRectMake(0,185,0,0)]; 

pickerArea.delegate = self; 
pickerArea.showsSelectionIndicator = YES; // note this is default to NO 

[menuArea addSubview:pickerArea]; 
[menuArea showInView:self.view]; 
[menuArea setBounds:CGRectMake(0,0,320, 600)]; 
+0

我意识到我可以做到这一点,我正在寻求一种方法来做到这一点,而无需为动作表添加属性。 – Echilon 2011-05-21 15:17:34

0
UIView *aView = prepTimePicker; 
do { 
    aView = aView.superView; 
} while ((aView != nil) && [aView isMemberOfClass:[UIActionSheet class]]); 
[aView dismissWithClickedButtonIndex:0 animated:YES]; 

这会帮助你找到UIActionSheet对象以下更改。

0
[[ [[[UIApplication sharedApplication] delegate] window] 
    viewWithTag: PREPTIME_PICKER] dismissWithClickedButtonIndex:0 animated:YES]; 

这没有用?

+0

不,ActionSheet没有解雇按钮,所以我试过 \t [[[[[[[UIApplication sharedApplication]委托]窗口] \t viewWithTag:PREPTIME_PICKER] resignFirstResponder]; 但它没有效果。 – Echilon 2011-05-29 13:54:13