2011-05-21 88 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