如何使用关闭按钮关闭UIActionSheet并避免崩溃?

时间:2011-12-04 20:54:06

标签: objective-c xcode uipickerview uisegmentedcontrol uiactionsheet

我已阅读另一篇文章(how to dismiss action sheet),我可以使用

[actionSheet dismissWithClickedButtonIndex:0 animated:YES];

使用关闭按钮关闭uiactionsheet,如下所述:

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil 
                                                         delegate:nil
                                                cancelButtonTitle:nil
                                           destructiveButtonTitle:nil
                                                otherButtonTitles:nil];

[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];

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

UIPickerView *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];


[actionSheet dismissWithClickedButtonIndex:0 animated:YES];


[closeButton release];


[actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];

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

但是,我不知道在哪里放置这一行,假设这将解决我的问题:

[actionSheet dismissWithClickedButtonIndex:0 animated:YES]; 

此外,当我点击关闭按钮时,应用程序崩溃,并显示错误消息" PROGRAM RECEIVED ERROR MESSAGE SIGBRT"。我认为我的两个问题是相关的。有任何帮助吗?

1 个答案:

答案 0 :(得分:10)

只需实施dismissActionSheet并将您的解雇信息放在那里。

dismissActionSheet方法看起来像这样:

-(void)dismissActionSheet {
   [sheet dismissWithClickedButtonIndex:0 animated:YES];
}