I am learning Swift and attempting to make use of code from the iOS8 Swift Dev's Cookbook, but am having difficulty using a variable produced from a custom function in the viewDidLoad function to put that product in a UILabel object.
The optional phone toward the top is not corresponding to the phone created peoplePickerNavigationController function. Thus, the labelPhone object displays nothing rather than displaying the phone number selected out of the peoplePickerNavigationController function.
How does one get a variable/constant from one function into an object in the viewDidLoad function so that it can be used in the UI?
For reference, my Storyboard contains a UIButton "Contacts" that can be tapped to execute the peoplePickerNavigationController function.
import UIKit
import AddressBookUI
class ViewController: UIViewController, ABPeoplePickerNavigationControllerDelegate {
var phone : String?
//I'm trying to pull up the phone variable down below, but the two are not corresponding. The optional above is what appears in the `labelPhone` object below rather than the contents of the phone way below.
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var labelPhone = UILabel(frame: CGRectMake(0, 0, 200, 21))
labelPhone.center = CGPointMake(160, 284)
labelPhone.textAlignment = NSTextAlignment.Center
labelPhone.text = phone
self.view.addSubview(labelPhone)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
let personPicker: ABPeoplePickerNavigationController
required init(coder aDecoder: NSCoder) {
personPicker = ABPeoplePickerNavigationController()
super.init(coder: aDecoder)
personPicker.peoplePickerDelegate = self
}
func peoplePickerNavigationControllerDidCancel(peoplePicker: ABPeoplePickerNavigationController!) {
/* Mandatory */
}
func peoplePickerNavigationController(peoplePicker: ABPeoplePickerNavigationController!, didSelectPerson person: ABRecord!, property: ABPropertyID, identifier: ABMultiValueIdentifier) {
/* A property in a person was picked */
}
@IBAction func performPickPerson(sender : AnyObject) {
self.presentViewController(personPicker, animated: true, completion: nil)
}
func peoplePickerNavigationController(peoplePicker: ABPeoplePickerNavigationController!, didSelectPerson person: ABRecordRef!) {
/* Do we know which picker it is? */
if peoplePicker != personPicker{
return
}
/* Get all phone numbers this user has */
let phones: ABMultiValueRef = ABRecordCopyValue(person, kABPersonPhoneProperty).takeRetainedValue()
let countOfPhones = ABMultiValueGetCount(phones)
for index in 0..<countOfPhones{
let phone = ABMultiValueCopyValueAtIndex(phones, index).takeRetainedValue() as String
println(phone)
}
}
}
Thanks <3
Current code:
import UIKit
import AddressBookUI
class ViewController: UIViewController, ABPeoplePickerNavigationControllerDelegate {
var phone : String?
var labelPhone: UILabel?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
labelPhone = UILabel(frame: CGRectMake(0, 0, 200, 21))
labelPhone!.center = CGPointMake(160, 284)
labelPhone!.textAlignment = NSTextAlignment.Center
labelPhone!.text = phone
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
let personPicker: ABPeoplePickerNavigationController
required init(coder aDecoder: NSCoder) {
personPicker = ABPeoplePickerNavigationController()
super.init(coder: aDecoder)
personPicker.peoplePickerDelegate = self
}
func peoplePickerNavigationControllerDidCancel(peoplePicker: ABPeoplePickerNavigationController!) {
/* Mandatory */
}
func peoplePickerNavigationController(peoplePicker: ABPeoplePickerNavigationController!, didSelectPerson person: ABRecord!, property: ABPropertyID, identifier: ABMultiValueIdentifier) {
/* A property in a person was picked */
}
@IBAction func performPickPerson(sender : AnyObject) {
self.presentViewController(personPicker, animated: true, completion: nil)
}
func peoplePickerNavigationController(peoplePicker: ABPeoplePickerNavigationController!, didSelectPerson person: ABRecordRef!) {
/* Do we know which picker it is? */
if peoplePicker != personPicker{
return
}
/* Get all phone numbers this user has */
let phones: ABMultiValueRef = ABRecordCopyValue(person, kABPersonPhoneProperty).takeRetainedValue()
let countOfPhones = ABMultiValueGetCount(phones)
for index in 0..<countOfPhones{
//let phone = ABMultiValueCopyValueAtIndex(phones, index).takeRetainedValue() as String
phone = ABMultiValueCopyValueAtIndex(phones, index).takeRetainedValue() as? String
labelPhone!.text = phone
println(phone)
}
}
}
viewDidLoadhappens long before anything else. So how could it possible be affected by whatdidSelectPersonwill do in the future? It doesn't have a time machine...phoneindidSelectPersonis not the samephoneup at the top, because you saidletwhich declares a completely different local variable. But even if it were, it wouldn't matter, because it happens later. Try to think about the order in which methods are called. Use logging to help you understand.viewDidLoad; it happened earlier. You want to set the label's text tophone, then set it. Don't wait and pray for it to happen magically.