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

Sha*_*ddy 6 xcode objective-c uisegmentedcontrol uipickerview uiactionsheet

我已阅读另一篇文章(如何解散行动表),我可以使用

[actionSheet dismissWithClickedButtonIndex:0 animated:YES];
Run Code Online (Sandbox Code Playgroud)

使用关闭按钮关闭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)];
Run Code Online (Sandbox Code Playgroud)

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

[actionSheet dismissWithClickedButtonIndex:0 animated:YES]; 
Run Code Online (Sandbox Code Playgroud)

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

Mun*_*ndi 10

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

dismissActionSheet方法看起来像这样:

-(void)dismissActionSheet {
   [sheet dismissWithClickedButtonIndex:0 animated:YES];
}
Run Code Online (Sandbox Code Playgroud)