Reputation: 111
I have an alert pop-up with a textField requesting the user input data. I have finally gotten the textField to cast the data to where I need it, however I cannot save the item anymore. If the alertAction doesn't have an handler func attached to it, I can call the save func with no problem. Now that I have the "set" action casting the data from the textfield, it will no longer allow me to call the function. It gives an error of "expression resolves to unused function". And, for some reason it wants me to have self. before all of the data...see code.
AlertController:
@IBAction func saveButton(sender: AnyObject) {
if (item?.slminqty == nil) {
let alert = UIAlertController(title: "Minimun Qty", message: "Please set minimun Qty. for pantry.", preferredStyle: UIAlertControllerStyle.Alert)
alert.addTextFieldWithConfigurationHandler { (textField: UITextField!) -> Void in
textField.placeholder = "Minimun Qty."
textField.keyboardType = .NumbersAndPunctuation
textField.clearButtonMode = UITextFieldViewMode.WhileEditing
}
alert.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Default, handler: {saveitem}()))
alert.addAction(UIAlertAction(title: "Set", style: UIAlertActionStyle.Default, handler: {(action) -> Void in
let textField = alert.textFields![0].text!
self.item?.slminqty = textField
if self.item?.slminqty != nil{
//if I place saveitem here it wants self.saveitem and gives error.
}
}))
self.presentViewController(alert, animated: true, completion: nil)
}else{
if item != nil {
edititems()
print(item!.slminqty!)
} else {
createitems()
}
dismissVC()
}
}
Save function:
func saveitem(sender: AnyObject) {
if item != nil {
edititems()
} else {
createitems()
}
print(item?.slminqty)
dismissVC()
}
I'm obviously doing it the extremely hard way or just wrong in general. How do I get the textField data to cast to item.slminqty and save the item?
Upvotes: 0
Views: 47
Reputation: 3956
Yes you'll have to use self reference to call saveitems as both have different scopes. You should probably be using self.saveitem()
inside.
Upvotes: 1