简体   繁体   English

CNContactViewController取消按钮不起作用

[英]CNContactViewController Cancel Button Not Working

I'm trying to use the built-in new contact UI and am getting unexpected behavior with the cancel button. 我正在尝试使用内置的新联系人UI,并使用取消按钮获得意外行为。 The code below works and calls up the new contact screen but the cancel button will only clear the screen entries not cancel out of the new contact screen. 下面的代码工作并调出新的联系人屏幕,但取消按钮将仅清除未从新联系人屏幕取消的屏幕条目。 In the built in contacts app hitting cancel returns to the contact list screen. 在内置联系人应用程序点击取消返回到联系人列表屏幕。 I would like the cancel button to close out the window. 我想取消按钮关闭窗口。

@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)
         }
    }

}

did you implement CNContactViewControllerDelegate methods? 你实现了CNContactViewControllerDelegate方法吗? Here's a link to documentation 这是文档的链接

for example: 例如:

 func contactViewController(viewController: CNContactViewController, didCompleteWithContact contact: CNContact?) {

    self.dismissViewControllerAnimated(true, completion: nil)
}

Better way to do the dismissing would be to check if the contact is nil and then dismiss it. 更好的解雇方法是检查联系人是否为零,然后解雇。 The dismiss doesn't work if you've pushed the view controller from a navigation controller. 如果您已从导航控制器推动视图控制器,则忽略不起作用。 You might have to do the following: 您可能必须执行以下操作:

func contactViewController(viewController: CNContactViewController, didCompleteWithContact contact: CNContact?) {
  if let contactCreated =  contact
  {

  }
  else
  {
    _ = self.navigationController?.popViewController(animated: true)
  }
}

It worked for me using the following code: 它使用以下代码为我工作:

Swift 3 斯威夫特3

func contactViewController(_ vc: CNContactViewController, didCompleteWith con: CNContact?) {
    vc.dismiss(animated: true)
}

Also I changed the way I was calling the controller: 我也改变了调用控制器的方式:

Instead of: 代替:

self.navigationController?.pushViewController(contactViewController, animated: true)

the solution was: 解决方案是:

self.present(UINavigationController(rootViewController: contactViewController), animated:true)

I found the solution using the example code Programming-iOS-Book-Examples written by Matt Neuburg : 我使用Matt Neuburg编写的示例代码Programming-iOS-Book-Examples找到了解决方案:

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

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