在Swift

时间:2017-09-14 04:37:54

标签: ios swift boolean extension-methods viewcontroller

我正在尝试为视图控制器进行扩展。这是一个带有水龙头识别器的简单视图。如果按下一次,背景颜色应从品红色变为橙色。到下一次推动它应该再次成为洋红色。

如果我为具体的VC做这个,比方说,StartVC就可以了。这是代码:

import UIKit

class StartVC: UIViewController {
    // bool
    var colorChanged = false

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor.magenta
        setMyTouchableView2()
    }
}

extension StartVC {
    // make touchable view
    func setMyTouchableView2() {
        let myView = UIImageView(frame: CGRect(x: 100, y: 100, width: 50, height: 50))
        myView.backgroundColor = UIColor.yellow

        // add gesture recognizer
        let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(changeColor2(_:)))
        myView.addGestureRecognizer(tapGestureRecognizer)
        myView.isUserInteractionEnabled = true

        view.addSubview(myView)
    }

    func changeColor2(_ recognizer:UITapGestureRecognizer){
        if(colorChanged) {
            view.backgroundColor = UIColor.magenta
        } else {
            view.backgroundColor = UIColor.orange
        }

        colorChanged = !colorChanged
    }
}

但是,如果我尝试为任何视图控制器推广此扩展,我会遇到以下问题:我无法将我的布尔值(colorChanged)放在视图控制器中,因为扩展程序不会看到它。我该如何解决?

以下是代码:

import UIKit

class ViewController: UIViewController {
    // bool
    var colorChanged = false

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor.magenta
        setMyTouchableView()
    }
}

extension UIViewController {
    // make touchable view
    func setMyTouchableView() {
        let myView = UIImageView(frame: CGRect(x: 100, y: 100, width: 50, height: 50))
        myView.backgroundColor = UIColor.yellow

        // add gesture recognizer
        let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(changeColor(_:)))
        myView.addGestureRecognizer(tapGestureRecognizer)
        myView.isUserInteractionEnabled = true

        view.addSubview(myView)
    }

    func changeColor(_ recognizer:UITapGestureRecognizer){
        if(colorChanged) {
            view.backgroundColor = UIColor.magenta
        } else {
            view.backgroundColor = UIColor.orange
        }
    }

2 个答案:

答案 0 :(得分:1)

默认情况下,扩展名不能存储属性。但是,如果你真的需要这个,你可以通过objc runtime

创建它
private var associationKey: UInt8 = 111 // Some constant

extension ViewController {
    var colorChanged: Bool {
        get {
            return objc_getAssociatedObject(self, &associationKey) as! Bool
        }
        set {
            objc_setAssociatedObject(self, &associationKey, newValue, .OBJC_ASSOCIATION_ASSIGN)
        }
    }
}

但是你可能遇到问题,几个控制器会改变这个标志。所以你也可以尝试使用协议

protocol ColorChangable {
    var colorChange: Bool { get set }
}

extension ColorChangable where Self: UIViewController {
    func setMyTouchableView {
        // Your logic
    }
}

class ViewController: UIViewController {
    var colorChange: Bool

    func viewDidLoad() {
        self.viewDidLoad()

        setMyTouchableView()
    }
}

extension ViewController: ColorChangable {}

答案 1 :(得分:0)

您可以使用struct来创建全局变量 -

var sockets = [A,B,C]
    socket.on('vote',data=>{
    let []tovisible = sockets - data.senderId 
    //emit event only tovisible arry
    socket.to(tovisible).emit(`vote`,STORE[channelId])
   }