目标是在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()
}
}发布于 2018-12-04 01:52:02
让我附上CNContactViewController类声明的屏幕截图:CNContactViewController
正如您在图像中看到的那样,这里有一个@note,上面写着:“所有属性在编辑联系人时都是可见的”。所以我认为,当你创建一个联系人时,它被认为是编辑联系人的一个特例。
我使用displayedPropertyKeys只是为了显示一个联系人,并且在这种情况下工作得很好。
希望这对你有帮助!
https://stackoverflow.com/questions/53603801
复制相似问题