2

UIButton addTarget を使用して、NSString オブジェクトを別のメソッドに引数として渡すにはどうすればよいですか。

これが私のコードです:

 - (void)vision:(PBJVision *)vision capturedVideo:(NSDictionary *)videoDict error:(NSError *)error
{

    _recording = NO;

    if (error) {
        NSLog(@"encounted an error in video capture (%@)", error);
        return;
    }


    _currentVideo = videoDict;

    NSString *videoPath = [_currentVideo  objectForKey:PBJVisionVideoPathKey];
    NSLog(@"string = %@",videoPath);



UIButton *ok = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    ok.frame = CGRectMake(95, 3, 50, 30);
    [ok setTitle:@"OK" forState:UIControlStateNormal];
    [ok addTarget:self action:@selector(okwindow:) forControlEvents:UIControlEventTouchUpInside];
    [customview addSubview:ok];

}

addTarget のパラメーターとして文字列値を渡す方法:

解決策と同様に、何か提案をお願いします...

4

1 に答える 1

1

おそらく、okwindow: メソッドで videoPath 文字列にアクセスしたいですか? ボタンを介して状態を渡すことによっては行われません。ボタンは、タッチ イベントを収集するためのものです。

代わりに、プロパティを追加してView Controllerの状態を変更してください...

@property (nonatomic, strong) NSString *videoPath;

文字列を取得するときに設定します...

NSString *videoPath = [_currentVideo  objectForKey:PBJVisionVideoPathKey];
NSLog(@"string = %@",videoPath);
self.videoPath = videoPath;

ユーザーがボタンを押したときにアクセスします...

- (void)okwindow:(id)sender {

    NSLog(@"I still have the string = %@",self.videoPath);
}
于 2013-09-09T21:28:51.110 に答える