Reputation: 1067
All,
In ViewDidLoad , I have set :
BarButtonPAY.enabled = false
and that disables the bar button item and it is not selectable, thats fine.
I have an computed property on the View Controller, which enables the button with 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 ?
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).
@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 :
@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
}
Upvotes: 2
Views: 762
Reputation: 10286
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
var PayButton : Int = -1 {
didSet {
println("Barracuda")
BarButtonPAY?.enabled = true
}
}
or declare it as optional Int
var PayButton : Int? {
didSet {
println("Barracuda")
BarButtonPAY?.enabled = true
}
}
and remove the line when you set its value on init on both cases
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
@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
override func viewDidLoad(){
PayButton = initialValue
}
Upvotes: 1
Reputation: 4931
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:
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
Upvotes: 0