ContactsUI:在iOS上创建新联系人时,如何仅显示特定字段?

时间:2018-12-04 00:04:28

标签: ios swift contacts cncontact cncontactviewcontroller

目标是在iOS上添加新联系人时仅显示某些字段。

例如,假设您只想显示和编辑联系人的地址,电话号码和名字。

下面的代码不起作用。所有字段仍会出现。

将此视图控制器拖放到项目中,尽管使用了displayedPropertyKeys.

,您仍然可以看到所有联系人字段仍然显示。

您将如何做?

import Foundation
import Contacts
import ContactsUI


class ContactViewController: UIViewController, CNContactViewControllerDelegate {


    override func viewDidLoad() {
        super.viewDidLoad()
    }


    func createContact() {
        let contactController = CNContactViewController(forNewContact: nil)

        contactController.delegate = self
        contactController.allowsEditing = true
        contactController.allowsActions = true
        contactController.displayedPropertyKeys = [CNContactPostalAddressesKey, CNContactPhoneNumbersKey, CNContactGivenNameKey]

        contactController.view.layoutIfNeeded()

        present(UINavigationController(rootViewController: contactController), animated:true)
    }


    // =============================================================================================================
    // MARK: IB Actions
    // =============================================================================================================
    @IBAction func newContactButtonDidTap(_ sender: UIButton) {
        createContact()
    }


    // =============================================================================================================
    // MARK: UIViewController Functions
    // =============================================================================================================
    override var prefersStatusBarHidden: Bool {
        return true
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

1 个答案:

答案 0 :(得分:1)

让我附上from datetime import datetime z['Date'] = z.apply(lambda row: datetime(row['Year'], row['Month'], row['day']), axis=1) 类声明的屏幕截图: CNContactViewController

如您在图像中看到的,那里有一个@note,并说:'在编辑联系人时所有属性都是可见的'。因此,我认为在创建联系人时,将其视为编辑联系人的特例。

我仅使用CNContactViewController来显示联系人,并且在这种情况下运行良好。

希望这对您有帮助!