OGeek|极客世界-中国程序员成长平台

标题: iphone - 如何将 NSString 值从一种方法传递到按钮 addTarget 方法的另一种方法? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 14:21
标题: iphone - 如何将 NSString 值从一种方法传递到按钮 addTarget 方法的另一种方法?

如何使用 UIButton addTarget 将 NSString 对象作为参数传递给另一个方法:

这是我的代码:

 - (void)visionPBJVision *)vision capturedVideoNSDictionary *)videoDict errorNSError *)error
{

    _recording = NO;

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


    _currentVideo = videoDict;

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



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

}

如何将字符串值作为参数传递给 addTarget:

请给出任何建议以及解决方案...



Best Answer-推荐答案


想必你想在 okwindow: 方法中访问 videoPath 字符串?这不是通过按钮传递状态来完成的。按钮用于收集触摸事件。

相反,通过添加属性来更改 View Controller 的状态...

@property (nonatomic, strong) NSString *videoPath;

当你得到字符串时设置它...

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

并在用户按下按钮时访问它...

- (void)okwindowid)sender {

    NSLog(@"I still have the string = %@",self.videoPath);
}

关于iphone - 如何将 NSString 值从一种方法传递到按钮 addTarget 方法的另一种方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18706998/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4