gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:21:07 26 4
gpt4 key购买 nike

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

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

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

作为引用,我的 Storyboard 包含一个 UIButton“联系人”,点击它可以执行 peoplePickerNavigationController 功能。

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)


}
}

}

谢谢<3

当前代码:

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)



}
}

}

最佳答案

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

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
}

关于ios - 在 Swift 中使用来自单独函数的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28121978/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com