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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8378514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:40:25  来源:igfitidea点击:

How to dismiss UIActionSheet with close button and avoid crash?

objective-cxcodeuipickerviewuisegmentedcontroluiactionsheet

提问by Shaddy

I have read on another post (how to dismiss action sheet) that I can use

我已阅读另一篇文章(如何解除操作表),我可以使用

[actionSheet dismissWithClickedButtonIndex:0 animated:YES];

to dismiss the uiactionsheet with the close button, as defined in:

使用关闭按钮关闭 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)];


However, I am not sure where to place this line, assuming this will solve my problem:

但是,我不确定将这条线放在哪里,假设这将解决我的问题:

[actionSheet dismissWithClickedButtonIndex:0 animated:YES]; 

Also, the app crashes when I click the close button, with error message "PROGRAM RECEIVED ERROR MESSAGE SIGBRT". I assume my two problems are related. Any help out there?

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

回答by Mundi

Just implement dismissActionSheetand put your dismiss message there.

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

The dismissActionSheetmethod would look something like this:

dismissActionSheet方法看起来像这样:

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