简体   繁体   English

Swift:用联系人数据填充TextField的最简单方法

[英]Swift: Simplest way to populate TextFields with Contacts data

I've been trying to populate five empty textfields on a viewcontroller with data from the user's contacts. 我一直在尝试用来自用户联系人的数据填充ViewController上的五个空文本字段。 This seems to be the most comprehensive discussion and example of working with the Contacts Framework, but because it's using two viewcontrollers, I just can't get my head wrapped around what gets done where. 似乎是使用Contacts Framework进行的最全面的讨论和示例,但是由于它使用的是两个视图控制器,因此我无法集中精力在哪里完成工作。 Right now, my code basically consists of: 现在,我的代码基本上包括:

var thisContact = [CNContact]()

@IBAction func actionBtn_AddClientsFromContactsBtn(sender: AnyObject) {

    let contactPickerViewController = CNContactPickerViewController()

    contactPickerViewController.delegate = self

    presentViewController(contactPickerViewController, animated: true, completion: nil)
}


func contactPicker(picker: CNContactPickerViewController, didSelectContact contact: CNContact) {

    didFetchContacts([contact])

    //The magic should be happening here, right???

    navigationController?.popViewControllerAnimated(true)

}


func didFetchContacts(contacts: [CNContact]) {
    for contact in contacts {
        self.thisContact.append(contact)
    }
}

The Contacts list appears, and I'm able to select a contact whereafter the view (Contacts list) pops. 出现“联系人”列表,我可以选择一个联系人,然后弹出视图(“联系人”列表)。 When I do a count of thisContact - all 529 contacts are counted. 当我对thisContact进行计数时,将统计所有529个联系人。

Please enlighten me on what I'm doing wrong/right, and what I need to do to get those five keys' data and get it displayed in the five TextFields. 请启发我做错了什么/正确的事情,以及我需要做些什么来获取这五个键的数据并将其显示在五个TextField中。

Well, as it turns out, I've been overthinking the problem and the solution. 好吧,事实证明,我一直在思考问题和解决方案。 the answer was quite easy: 答案很简单:

func contactPicker(picker: CNContactPickerViewController, didSelectContact contact: CNContact) {

    let myContact = contact
    let myContactFirstName = myContact.givenName
    let myContactLastName = myContact.familyName
    let myContactJobTitle = myContact.jobTitle

    outletTextField_UserClientFirstName.text = myContactFirstName.uppercaseString
    outletTextField_UserClientLastName.text = myContactLastName.uppercaseString
    outletTextField_UserClientJobTitle.text = myContactJobTitle


    // Set the contact's work email address.
    var myContactWorkEmailAddress: String!
    for workEmailAddress in contact.emailAddresses {
        if workEmailAddress.label == CNLabelWork {
            myContactWorkEmailAddress = workEmailAddress.value as! String
            outletTextField_UserClientWorkEmailAddress.text = myContactWorkEmailAddress
            break
        }
    }

    // Set the contact's work phone number.
    var myContactWorkPhoneNumber: String!
    var myContactWorkPhoneNumberArray = [String]()
    for mainPhone in contact.phoneNumbers {
        if mainPhone.label == CNLabelPhoneNumberMain {
            let mainPhoneValue = mainPhone.value as! CNPhoneNumber
            myContactWorkPhoneNumberArray.append(mainPhoneValue.stringValue)
            myContactWorkPhoneNumber = myContactWorkPhoneNumberArray[0]
            outletTextField_UserClientMobilePhoneNumber.text = myContactWorkPhoneNumber
            break
        }
    }

    navigationController?.popViewControllerAnimated(true)

It might not be perfect, but it works perfectly! 它可能并不完美,但是却可以完美运行!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM