user3246092
user3246092

Reputation: 900

Using a Variable from a Separate Function in Swift

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)



    }
}

}

Upvotes: 0

Views: 184

Answers (1)

ljk321
ljk321

Reputation: 16790

First, viewDidLoad only execute once when the view is loaded. To change the text of the label you need to do something like:

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
    self.view.addSubview(labelPhone)
}

func peoplePickerNavigationController(peoplePicker: ABPeoplePickerNavigationController!, didSelectPerson person: ABRecordRef!) {
    // Got the phone 
    phone = ABMultiValueCopyValueAtIndex(phones, index).takeRetainedValue() as String
    labelPhone.text = phone
}

Upvotes: 1

Related Questions