【问题标题】:Swift protocol interacting with objective-c ViewController ClassSwift 协议与objective-c ViewController 类交互
【发布时间】:2019-06-12 14:58:25
【问题描述】:

我正在尝试使用协议从 swift 类中的视图控制器类中触发一个函数。

@objc protocol KestrelDeviceConnectDelegate: AnyObject {    
    func readingIsOver()    
}

class KestrelDeviceConnect: NSObject {    

    weak var delegate: KestrelDeviceConnectDelegate?

    func timerAction() {    
        timerCounter += 1
        if(timerCounter >= 20){
            progressHUD.hide()
            timerCounter = 0
            timer.invalidate()
            self.delegate?.readingIsOver()

            //present kestrel alert
        }
    }
}

在视图控制器类viewdidload

KestrelDeviceConnect *bluetoothManager = [[KestrelDeviceConnect alloc]init];
bluetoothManager.delegate = self;

我想从我的 swift 类中调用的函数

- (void)readingIsOver {
    [[self logConditionsButton] setHidden:NO];
}

【问题讨论】:

    标签: ios objective-c swift protocols


    【解决方案1】:

    您需要通过将bluetoothManager 设为实例变量来保持强引用

    KestrelDeviceConnect *bluetoothManager;
    

    bluetoothManager = [[KestrelDeviceConnect alloc]init];
    

    【讨论】:

    • 这并没有改变什么
    • 这个KestrelDeviceConnect *bluetoothManager;viewDidLoad之外?
    • self.timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.timerAction), userInfo: nil, repeats: true)
    • 我知道timerAction函数中的if语句执行了,但是self.delegate?.readingIsOver()什么也没做
    猜你喜欢
    • 2016-11-17
    • 1970-01-01
    • 2014-09-18
    • 2015-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-23
    • 2017-03-19
    相关资源
    最近更新 更多