在Swift中使用来自单独函数的变量

时间:2015-01-24 03:29:07

标签: ios xcode swift

我正在学习Swift并尝试使用iOS8 Swift Dev's Cookbook中的代码,但我很难使用viewDidLoad函数中自定义函数生成的变量将该产品放入{{1对象。

顶部的可选UILabelphone创建的phone功能不对应。因此,labelPhone对象不显示任何内容,而是显示从peoplePickerNavigationController函数中选择的电话号码。

如何从一个函数获取变量/常量到peoplePickerNavigationController函数中的对象,以便可以在UI中使用它?

作为参考,我的故事板包含viewDidLoad“联系人”,可以点按这些“联系人”来执行UIButton功能。

peoplePickerNavigationController

谢谢< 3

当前代码:

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)


        }
    }

}

1 个答案:

答案 0 :(得分:1)

首先,viewDidLoad仅在加载视图时执行一次。要更改标签文本,您需要执行以下操作:

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
}