我尝试使用内置的新联系人UI,并使用取消按钮获得意外行为。下面的代码工作并调出新的联系人屏幕,但取消按钮将仅清除未从新联系人屏幕取消的屏幕条目。在内置联系人应用程序点击取消返回到联系人列表屏幕。我想取消按钮关闭窗口。
@IBAction func newTwo(sender: AnyObject) {
AppDelegate.getAppDelegate().requestForAccess { (accessGranted) -> Void in
if accessGranted {
let npvc = CNContactViewController(forNewContact: nil)
npvc.delegate = self
self.navigationController?.pushViewController(npvc, animated: true)
}
}
}
答案 0 :(得分:10)
你实现了CNContactViewControllerDelegate方法吗? 这是a link to documentation
例如:
func contactViewController(viewController: CNContactViewController, didCompleteWithContact contact: CNContact?) {
self.dismissViewControllerAnimated(true, completion: nil)
}
答案 1 :(得分:3)
更好的解雇方法是检查联系人是否为零,然后解雇。如果您已从导航控制器推动视图控制器,则忽略不起作用。您可能必须执行以下操作:
func contactViewController(viewController: CNContactViewController, didCompleteWithContact contact: CNContact?) {
if let contactCreated = contact
{
}
else
{
_ = self.navigationController?.popViewController(animated: true)
}
}
答案 2 :(得分:3)
使用以下代码对我有用:
Swift 3
func contactViewController(_ vc: CNContactViewController, didCompleteWith con: CNContact?) {
vc.dismiss(animated: true)
}
我也改变了调用控制器的方式:
而不是:
self.navigationController?.pushViewController(contactViewController, animated: true)
解决方案是:
self.present(UINavigationController(rootViewController: contactViewController), animated:true)
我使用Programming-iOS-Book-Examples编写的示例代码Matt Neuburg找到了解决方案: