2016-09-22 4 views
0

私は、入力としてstickerURLStringを受け取るデリゲートメソッドの下にこれを持っている:ボタンセレクタで文字列パラメータを別のメソッドに渡すにはどうすればいいですか?

- (void)selectedSticker:(NSString *)stickerURLString { 
    //... 
     [self.stickerPreviewButton addTarget:self action:@selector(sendStickerPreviewButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
    //... 
} 

そして、セレクタは、この方法 sendStickerPreviewButtonPressed呼び出します。あなたはこの作品を作るために見ることができるように

- (void)sendStickerPreviewButtonPressed: (NSString *)stickerURLString { 
    [self.delegate InputFunctionView:self sendSticker:stickerURLString]; 
} 

を私は渡す必要があります期待通りにstickerURLStringから選択されたステッカーメソッドsendStickerPreviewButtonPressed。これに代えて

[self.stickerPreviewButton performSelector:@selector(sendStickerPreviewButtonPressed:) withObject:stickerURLString]; 

私はこれを試してみました

[self.stickerPreviewButton addTarget:self action:@selector(sendStickerPreviewButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 

をしかし、私はエラー "タイプNSExceptionのキャッチされない例外で終了する" しまいました。

誰もボタンセレクタで文字列パラメータを別のメソッドに渡す方法を知っていますか?あなたは、あなたがこの変化を使用することができる機能を呼び出すためにperformSelectorを使用している場合

+0

(http://stackoverflow.com/questions/24814646/attach-parameter-to-button-addtarget-action-in-swift)スイフトでbutton.addTargetアクションにパラメータを添付]の可能な重複 –

+2

グローバル変数を宣言し、メソッド内で値を代入します。 –

+0

@AnuradhSはい、私はコードがかなり複雑なので、それを避けようとしていますが、グローバル変数を使用する方法は私にとってはそうです。ありがとう! – SanitLee

答えて

0

UIControlオブジェクト関数第1パラメータはid型で、コントロール自体が渡します。あなたはsendStickerPreviewButtonPressedのパラメータとしてuibuttonオブジェクトを取得します。文字列に変換できない関数です。エラーが発生しました。文字列を渡すことができます。カスタムクラス。独自のクラスのボタンを作成し、カスタムプロパティフォームの送信ボタンを取得します。

 -(void)sendStickerPreviewButtonPressed:(mycustomButton*)sender{ 
     //sender.myCustomString 
} 
関連する問題