在Swift中启用栏按钮项

时间:2023-01-16 12:19:45

All,

所有人,

In ViewDidLoad , I have set :

在ViewDidLoad中,我设置:

BarButtonPAY.enabled = false

and that disables the bar button item and it is not selectable, thats fine.

这将禁用bar按钮项,它是不可选的,这很好。

I have an computed property on the View Controller, which enables the button with enable = true.

我在视图控制器上有一个computed属性,它允许使用enable = true按钮。

I am getting the error 'found nil in optional value' and I noticed that BarButtonItem is an optional. When I put the ? at the end of the variable it still does not enable. any ideas ?

我得到了“在可选值中找到nil”的错误,我注意到BarButtonItem是可选的。当我把?在变量的末尾,它仍然不启用。什么好主意吗?

class ViewController: UIViewController {

    @IBOutlet weak var BarButtonPAY: UIBarButtonItem!

    var PayButton : Int {
        didSet {
                println("Barracuda")
                    BarButtonPAY?.enabled = true
        }
    }

I have set the computed property differently now in the view controller - like so (removed the var from init).

我现在已经在视图控制器中以不同的方式设置了computed属性- like so(从init中删除var)。

@IBOutlet weak var BarButtonPAY: UIBarButtonItem!

    var PayButton : Int? {
        didSet {
            println("Barracuda")
            BarButtonPAY?.enabled = true
        }
    }


    required init(coder aDecoder: NSCoder) {
      //  self.PayButton = 0
        super.init(coder: aDecoder)
    }

    init() {
       // self.PayButton = 0
        super.init(nibName: nil, bundle: nil)

    }

When the button is pressed on the tableview cell. To select a product, and highlight the pay button as a BarButtonItem :

按下tableview单元格上的按钮时。选择产品,并突出显示pay按钮作为BarButtonItem:

@IBAction func SelectedButton(sender: UIButton) {


    // Set the colours and then highlight 'Next' on
    sender.backgroundColor = UIColor.blueColor()
    sender.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
    sender.setTitle("Ordered", forState: UIControlState.Normal)

    let test = ViewController()
    test.PayButton = 2

}

2 个解决方案

#1


1  

Since PayButton is not an optional property I assume that you are setting its value in initializer before BarButtonPAY is initialized. Try setting a default value to PayButton

由于PayButton不是可选属性,所以我假设在BarButtonPAY初始化之前,您正在初始化器中设置它的值。尝试设置PayButton的默认值

var PayButton : Int = -1 {
    didSet {
            println("Barracuda")
                BarButtonPAY?.enabled = true
    }
}

or declare it as optional Int

或者声明为可选的Int类型

var PayButton : Int? {
    didSet {
            println("Barracuda")
                BarButtonPAY?.enabled = true
    }
}

and remove the line when you set its value on init on both cases

并在两种情况下,在init上设置该行值时删除该行

Edited

编辑

Even after creating object typed ViewController in let test = ViewController() BarButtonPAY is not initialized (you can not use it until ViewController viewDidLoad method). A possible workaround : create another optional Integer property in ViewController and name it lets say initialValue. On selectedButton set initialValue as follow

即使在在let test = ViewController()中创建对象类型的ViewController()之后也没有初始化(直到ViewController viewDidLoad方法你才能使用它)。一种可能的解决方案:在ViewController中创建另一个可选的整型属性,并命名它,让它写入initialValue。在selectedButton设置initialValue如下所示

@IBAction func SelectedButton(sender: UIButton) {


// Set the colours and then highlight 'Next' on
sender.backgroundColor = UIColor.blueColor()
sender.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
sender.setTitle("Ordered", forState: UIControlState.Normal)

let test = ViewController()
test.initialValue = 2

}

then in ViewController viewDidLoad set PayButton to initialValue

然后在ViewController viewDidLoad将PayButton设置为initialValue

override func viewDidLoad(){

    PayButton = initialValue
}

#2


0  

When you do this let test = ViewController() you are creating a new instance of your view controller. This way your outlets will be nil. Try accessing your view controller from storyboard:

这样做时,让test = ViewController()创建视图控制器的新实例。这样你的outlet将为nil。尝试从故事板访问视图控制器:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewControllerWithIdentifier("yourViewController") as! UIViewController
self.presentViewController(viewController, animated: true, completion: nil)

And then access the property:

然后访问属性:

viewController.PayButton = 2

#1


1  

Since PayButton is not an optional property I assume that you are setting its value in initializer before BarButtonPAY is initialized. Try setting a default value to PayButton

由于PayButton不是可选属性,所以我假设在BarButtonPAY初始化之前,您正在初始化器中设置它的值。尝试设置PayButton的默认值

var PayButton : Int = -1 {
    didSet {
            println("Barracuda")
                BarButtonPAY?.enabled = true
    }
}

or declare it as optional Int

或者声明为可选的Int类型

var PayButton : Int? {
    didSet {
            println("Barracuda")
                BarButtonPAY?.enabled = true
    }
}

and remove the line when you set its value on init on both cases

并在两种情况下,在init上设置该行值时删除该行

Edited

编辑

Even after creating object typed ViewController in let test = ViewController() BarButtonPAY is not initialized (you can not use it until ViewController viewDidLoad method). A possible workaround : create another optional Integer property in ViewController and name it lets say initialValue. On selectedButton set initialValue as follow

即使在在let test = ViewController()中创建对象类型的ViewController()之后也没有初始化(直到ViewController viewDidLoad方法你才能使用它)。一种可能的解决方案:在ViewController中创建另一个可选的整型属性,并命名它,让它写入initialValue。在selectedButton设置initialValue如下所示

@IBAction func SelectedButton(sender: UIButton) {


// Set the colours and then highlight 'Next' on
sender.backgroundColor = UIColor.blueColor()
sender.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
sender.setTitle("Ordered", forState: UIControlState.Normal)

let test = ViewController()
test.initialValue = 2

}

then in ViewController viewDidLoad set PayButton to initialValue

然后在ViewController viewDidLoad将PayButton设置为initialValue

override func viewDidLoad(){

    PayButton = initialValue
}

#2


0  

When you do this let test = ViewController() you are creating a new instance of your view controller. This way your outlets will be nil. Try accessing your view controller from storyboard:

这样做时,让test = ViewController()创建视图控制器的新实例。这样你的outlet将为nil。尝试从故事板访问视图控制器:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewControllerWithIdentifier("yourViewController") as! UIViewController
self.presentViewController(viewController, animated: true, completion: nil)

And then access the property:

然后访问属性:

viewController.PayButton = 2