Ios Swift中的条形按钮项启用问题

Ios Swift中的条形按钮项启用问题,ios,swift,Ios,Swift,全部, 在ViewDidLoad中,我设置了: BarButtonPAY.enabled = false 这会禁用bar按钮项,并且它是不可选择的,这很好 我在视图控制器上有一个computed属性,它启用enable=true的按钮 我得到错误“在可选值中找到nil”,我注意到BarButtonItem是可选的。我什么时候放的?在变量末尾,它仍然无法启用。有什么想法吗 class ViewController: UIViewController { @IBOutlet weak v

全部,

在ViewDidLoad中,我设置了:

BarButtonPAY.enabled = false
这会禁用bar按钮项,并且它是不可选择的,这很好

我在视图控制器上有一个computed属性,它启用enable=true的按钮

我得到错误“在可选值中找到nil”,我注意到BarButtonItem是可选的。我什么时候放的?在变量末尾,它仍然无法启用。有什么想法吗

class ViewController: UIViewController {

    @IBOutlet weak var BarButtonPAY: UIBarButtonItem!

    var PayButton : Int {
        didSet {
                println("Barracuda")
                    BarButtonPAY?.enabled = true
        }
    }
我现在在视图控制器中设置了不同的computed属性,就像这样(从init中删除了var)

当按下tableview单元格上的按钮时。要选择一种产品,并将“付款”按钮高亮显示为按钮图标,请执行以下操作:

@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

}

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

var PayButton : Int = -1 {
    didSet {
            println("Barracuda")
                BarButtonPAY?.enabled = true
    }
}
或者将其声明为可选Int

var PayButton : Int? {
    didSet {
            println("Barracuda")
                BarButtonPAY?.enabled = true
    }
}
并在两种情况下都将其值设置为init时删除该行

编辑

即使在
let test=ViewController()
BarButtonPAY中创建了对象类型的ViewController之后,也不会初始化它(在ViewController viewDidLoad方法之前,您不能使用它)。一个可能的解决方法是:在ViewController中创建另一个可选的Integer属性,并将其命名为initialValue。在selectedButton上设置初始值,如下所示

@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

}
然后在ViewController viewDidLoad中将PayButton设置为initialValue

override func viewDidLoad(){

    PayButton = initialValue
}

执行此操作时,让test=ViewController()创建视图控制器的新实例。这样,您的门店将
nil
。尝试从情节提要访问视图控制器:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewControllerWithIdentifier("yourViewController") as! UIViewController
self.presentViewController(viewController, animated: true, completion: nil)
然后访问该属性:

viewController.PayButton = 2

似乎合理,我该如何修复?该按钮在界面生成器中设置。我在视图控制器上有一个容器(其中有一个tableview),当按下单元格上的按钮时,它应该调用这个计算机变量(确实如此)。您可以首先显示设置computed属性的代码并显示视图控制器。我想按钮来自故事板,请检查按钮连接(委托)故事板到ViewController,并在代码BarButtonPAY!中使用!。启用=真实且静止,在显示视图控制器的位置添加代码,并访问
PayButton
属性。我已在init中删除了var,并直接添加了-1,但仍然不起作用。在其他位置您在哪里设置它?可能您是在RootViewController的prepareForSegue中设置的?那么您是否确定BarButtonPAY已从故事板正确连接(检查参考插座)?当你设置PayButton的值时,你能给我看一下代码吗让我们来看看。这不起作用,因为我用来向PayButton添加值的类没有ViewController委托,因此“presentViewController”无效。我同意对象已脱离视图层次结构-我只需要以对象不会消失的方式连接到视图控制器。
viewController.PayButton = 2