0

UIActionSheet があり、UIPickerView と 2 つの BarButtonItems を追加しました。誰かがキャンセル ボタン (バー ボタン項目の 1 つ) をクリックしたときに、アクション シートを閉じようとしています。

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];
UIToolbar *tools = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 40)];
tools.barStyle = UIBarStyleBlackOpaque;
[actionSheet addSubview:tools];

UIBarButtonItem *doneButton=[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(btnActinDoneClicked)];
doneButton.imageInsets=UIEdgeInsetsMake(200, 6, 50, 25);
UIBarButtonItem *CancelButton=[[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(btnActinCancelClicked)]; //this is where the problem is

NSArray *array = [[NSArray alloc]initWithObjects:CancelButton, doneButton, nil];
[tools setItems:array];

[actionSheet showFromTabBar:self.tabBarController.tabBar];
[actionSheet setBounds:CGRectMake(0, 0, 320, 485)];

アクションシートを閉じる別の方法を追加しました -

-(IBAction)btnActinCancelClicked:(id)sender{
    [sender dismissWithClickedButtonIndex:0 animated:YES];
}

問題は、BarButton をクリックするとアプリがクラッシュすることです。CancelButton が宣言され、btnActinCancelClicked メソッドにメッセージを送信できない場所で問題が発生していると思いますが、修正方法がわかりません。

ありがとう

4

2 に答える 2

1

却下したいのは送信者ではありません。それはActionSheetです。そのためのリファレンスが必要になり、コードを次のように変更します。

- (IBAction)btnActinCancelClicked:(id)sender{
    [self.actionSheet dismissWithClickedButtonIndex:0 animated:YES];
}
于 2012-08-28T19:21:24.290 に答える
1

あなたの方法はこれです。

-(IBAction)btnActinCancelClicked:(id)sender

したがって、このように追加する必要があります(パラメーターを受け取るため)。

@selector(btnActinCancelClicked:)

したがって、たとえば、メソッドに3つのパラメーターがある場合は、それを呼び出します。@selector(myMethod:andX:andY:)

また、UIActionSheetにアクセスするため(これが機能するかどうかは完全にはわかりません)。

UIActionSheet *acSheet = sender.superView;

//Check if it is the ActionSheet here then dismiss it

[acSheet dismissWithClickedButtonIndex:0 animated:YES];
于 2012-08-28T19:23:06.527 に答える