无法关闭 CNContactViewController
Unable to dismiss CNContactViewController
我正在尝试让用户创建新联系人。虽然我有屏幕提示用户输入他的所有详细信息,但顶部没有导航栏(就像默认的 Apple Contacts 应用程序中一样)。没有办法退出现场。我在 swift 2.0 和 Xcode 7.3 中使用 ContactUI 框架。这是代码:
// create a new contact
let createNewActionHandler = {(action: UIAlertAction) -> Void in
let newContact = CNMutableContact()
let contactPicker = CNContactViewController(forNewContact: newContact)
contactPicker.delegate = self
contactPicker.navigationController?.setToolbarHidden(false, animated: false)
self.presentViewController(contactPicker, animated: true, completion: nil)
}
这是我想要得到的:
Apple Default
这是我的资料:
What I have
我正在通过选项卡视图控制器中的操作 sheet 启动新的联系人视图控制器。我尝试将选项卡嵌入导航视图控制器中,但没有效果。我什至尝试设置 navController 的 setToolbarHidden 属性 但它没有帮助。
感谢您的帮助。我在其他论坛上看到了这个问题,但他们没有帮助。
由于您在当前活动控制器的顶部呈现 contactPicker viewcontroller,您将无法访问导航栏,因为视图已完全呈现,如果您想拥有 Apple 联系人应用中的按钮,您需要将您的呈现 viewcontroller 嵌入到 UINavigationController 中,并添加左右栏按钮项。
请参考以下演示相同内容的苹果示例。
https://developer.apple.com/library/ios/samplecode/iPhoneCoreDataRecipes/Listings/Classes_RecipeAddViewController_m.html
视图控制器必须嵌入到 UINavigationController 中,您应该推送或显示视图控制器:
navigationController?.pushViewController(contactPicker, animated: true)
而不是呈现视图控制器
您必须将 contactViewController 嵌入到 UINavigationController
和实施委托方法。
let createNewActionHandler = {(action: UIAlertAction) -> Void in
let newContact = CNMutableContact()
let contactPicker = CNContactViewController(forNewContact: newContact)
contactPicker.delegate = self
let navigation = UINavigationController(rootViewController: contactPicker)
self.presentViewController(navigation, animated: true, completion: nil)
}
//MARK: - Delegate
func contactViewController(viewController: CNContactViewController, didCompleteWithContact contact: CNContact?) {
viewController.dismissViewControllerAnimated(true, completion: nil)
}
func contactViewController(viewController: CNContactViewController, shouldPerformDefaultActionForContactProperty property: CNContactProperty) -> Bool {
return true
}
我尝试了所有方法,但 none 有效,所以我制作了一个自定义方法
extension UIBarButtonItem {
private struct AssociatedObject {
static var key = "action_closure_key"
}
var actionClosure: (()->Void)? {
get {
return objc_getAssociatedObject(self, &AssociatedObject.key) as? ()->Void
}
set {
objc_setAssociatedObject(self, &AssociatedObject.key, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
target = self
action = #selector(didTapButton(sender:))
}
}
@objc func didTapButton(sender: Any) {
actionClosure?()
}
}
extension UIViewController{
func addDissmissButton(){
let cancelButton = UIBarButtonItem.init(title: "Dismiss", style: .plain, target: self, action: nil)
cancelButton.actionClosure = {
self.dismiss(animated: true)
}
self.navigationItem.leftBarButtonItem = cancelButton
}}
DispatchQueue.main.async {
let contactvc = CNContactViewController(for: contact)
contactvc.contactStore = self.store
contactvc.delegate = self
contactvc.addDissmissButton()
let navigationController = UINavigationController(rootViewController: contactvc)
self.present(navigationController, animated: false)
}
我正在尝试让用户创建新联系人。虽然我有屏幕提示用户输入他的所有详细信息,但顶部没有导航栏(就像默认的 Apple Contacts 应用程序中一样)。没有办法退出现场。我在 swift 2.0 和 Xcode 7.3 中使用 ContactUI 框架。这是代码:
// create a new contact
let createNewActionHandler = {(action: UIAlertAction) -> Void in
let newContact = CNMutableContact()
let contactPicker = CNContactViewController(forNewContact: newContact)
contactPicker.delegate = self
contactPicker.navigationController?.setToolbarHidden(false, animated: false)
self.presentViewController(contactPicker, animated: true, completion: nil)
}
这是我想要得到的: Apple Default
这是我的资料: What I have
我正在通过选项卡视图控制器中的操作 sheet 启动新的联系人视图控制器。我尝试将选项卡嵌入导航视图控制器中,但没有效果。我什至尝试设置 navController 的 setToolbarHidden 属性 但它没有帮助。
感谢您的帮助。我在其他论坛上看到了这个问题,但他们没有帮助。
由于您在当前活动控制器的顶部呈现 contactPicker viewcontroller,您将无法访问导航栏,因为视图已完全呈现,如果您想拥有 Apple 联系人应用中的按钮,您需要将您的呈现 viewcontroller 嵌入到 UINavigationController 中,并添加左右栏按钮项。
请参考以下演示相同内容的苹果示例。 https://developer.apple.com/library/ios/samplecode/iPhoneCoreDataRecipes/Listings/Classes_RecipeAddViewController_m.html
视图控制器必须嵌入到 UINavigationController 中,您应该推送或显示视图控制器:
navigationController?.pushViewController(contactPicker, animated: true)
而不是呈现视图控制器
您必须将 contactViewController 嵌入到 UINavigationController 和实施委托方法。
let createNewActionHandler = {(action: UIAlertAction) -> Void in
let newContact = CNMutableContact()
let contactPicker = CNContactViewController(forNewContact: newContact)
contactPicker.delegate = self
let navigation = UINavigationController(rootViewController: contactPicker)
self.presentViewController(navigation, animated: true, completion: nil)
}
//MARK: - Delegate
func contactViewController(viewController: CNContactViewController, didCompleteWithContact contact: CNContact?) {
viewController.dismissViewControllerAnimated(true, completion: nil)
}
func contactViewController(viewController: CNContactViewController, shouldPerformDefaultActionForContactProperty property: CNContactProperty) -> Bool {
return true
}
我尝试了所有方法,但 none 有效,所以我制作了一个自定义方法
extension UIBarButtonItem {
private struct AssociatedObject {
static var key = "action_closure_key"
}
var actionClosure: (()->Void)? {
get {
return objc_getAssociatedObject(self, &AssociatedObject.key) as? ()->Void
}
set {
objc_setAssociatedObject(self, &AssociatedObject.key, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
target = self
action = #selector(didTapButton(sender:))
}
}
@objc func didTapButton(sender: Any) {
actionClosure?()
}
}
extension UIViewController{
func addDissmissButton(){
let cancelButton = UIBarButtonItem.init(title: "Dismiss", style: .plain, target: self, action: nil)
cancelButton.actionClosure = {
self.dismiss(animated: true)
}
self.navigationItem.leftBarButtonItem = cancelButton
}}
DispatchQueue.main.async {
let contactvc = CNContactViewController(for: contact)
contactvc.contactStore = self.store
contactvc.delegate = self
contactvc.addDissmissButton()
let navigationController = UINavigationController(rootViewController: contactvc)
self.present(navigationController, animated: false)
}