这不是this question或this question的欺骗,因为这些答案不起作用。
由于某些原因,当在CNContactViewController中显示UINavigationController时,取消按钮没有任何效果。
复制步骤:
预期行为:
CNContactViewController被解职。
实际行为:
什么都没发生。委托函数中的断点永远不会被调用。
import Foundation
import Contacts
import ContactsUI
class ContactViewController: UIViewController, CNContactViewControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
createContact()
}
func createContact() {
let contactController = CNContactViewController(forNewContact: nil)
contactController.delegate = self
contactController.allowsEditing = true
contactController.allowsActions = true
contactController.title = ""
contactController.view.layoutIfNeeded()
let contactNavController = UINavigationController(rootViewController: contactController)
contactNavController.navigationBar.backgroundColor = UIColor.red
present(contactNavController, animated:true)
}
// =============================================================================================================
// MARK: CNContactViewControllerDelegate Functions
// =============================================================================================================
func contactViewController(_ viewController: CNContactViewController, didCompleteWith contact: CNContact?) {
viewController.dismiss(animated: true, completion: nil)
dismiss(animated: true, completion: nil)
print("hi")
}
func contactViewController(_ viewController: CNContactViewController, shouldPerformDefaultActionFor property: CNContactProperty) -> Bool {
print("yo")
return true
}
// =============================================================================================================
// MARK: UIViewController Functions
// =============================================================================================================
override var prefersStatusBarHidden: Bool {
return true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}发布于 2018-12-04 05:10:26
我现在已经尝试过您的代码;并且没有问题地取消按钮,关闭视图控制器。在我的例子中,问题在于如何显示视图控制器,通过将contactNavController的存在更改为:
DispatchQueue.main.async {
self.present(contactNavController, animated:true)
}试试看,也许你有线程问题
https://stackoverflow.com/questions/53604922
复制相似问题