Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
582 views
in Technique[技术] by (71.8m points)

ios - Unable to dismiss CNContactViewController

I'm trying to let the user create a new contact. While I've got the screen to prompt the user to put in all his details there is no navigation bar at the top(Like there is in the default Apple Contacts app). There is no way to exit the scene. I'm using the ContactUI framework in swift 2.0 and Xcode 7.3. Here's the code:

// 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)

    }

Here's what I'm trying to get: Apple Default

Here's what I have: What I have

I'm launching the new contact view controller from an action sheet in a tab view controller. I tried embedding the tab in a Navigation view controller but to no effect. I even tried setting the setToolbarHidden property of the navController but it didn't help.

Thanks for any help. I saw the issue raised in other forums but they didn't help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have to embed contactViewController to UINavigationController and Implement Delegate Methods.

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
  }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...