如何用关闭按钮解散UIActionSheet并避免崩溃



我在另一篇文章中读到我可以使用

[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"。我想我的两个问题是相关的。有人帮忙吗?

只需实现dismissActionSheet并将您的解散消息放在那里。

dismissActionSheet方法看起来像这样:

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

最新更新