从CNContactPickerViewController中选择联系人后,移至新视图

时间:2016-06-17 08:44:52

标签: ios swift contacts

我正在编写一个概念聊天应用。我现在正处于可以开始新聊天的地步。

我想要的是,当用户使用CNContactPickerViewController选择联系人时,选择器将被解除(这是有效的)并且联系人详细信息将发送到新的聊天视图(这不起作用)。我得到的错误是func contactPicker delegate返回nil。我一直在关注this教程,从Chat s ViewController获取联系人数据到ChatViewController。相关代码:

import UIKit
import Contacts
import ContactsUI

class ChatsViewController: UITableViewController, CNContactPickerDelegate {
    var chats:[Chat] = chatData
    var selectedContact: CNContact = CNContact()
    @IBAction func createNewChat(sender: AnyObject) {
        let contactPickerViewController = CNContactPickerViewController()
        contactPickerViewController.delegate = self
        presentViewController(contactPickerViewController, animated: true, completion: nil)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false
        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return chats.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("ChatCell", forIndexPath: indexPath) as! ChatCell

        let chat = chats[indexPath.row] as Chat
        cell.chat = chat
        return cell
    }

    func contactPicker(picker: CNContactPickerViewController, didSelectContact contact: CNContact) {
        self.selectedContact = contact
        self.performSegueWithIdentifier("idContactSelected", sender: self)
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "idContactSelected" {
            let chatVC = segue.destinationViewController as! ChatViewController
            chatVC.contact = self.selectedContact
        }
    }
}

这是新的聊天ViewController。

import UIKit
import Contacts
import ContactsUI

class ChatViewController: UIViewController {

    @IBOutlet weak var testLabel: UILabel!
    var contact: CNContact = CNContact()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        self.testLabel.text = contact.givenName
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

所以回顾一下:我有一个ViewController,我可以在那里按一个按钮开始一个新的聊天,这会打开联系人选择器。选择联系人后,将执行func contactPicker,但不会发送联系人数据,也不会显示新的聊天ViewController,delegatenil。为什么会这样?我认为这是因为delegate永远不会被设置,只是被创建,但是在教程中这就是他们所做的一切,它似乎有效。

提前感谢您对此的任何想法。

编辑:最终解决方案:我不需要代理,我可以通过prepareForSegueWithIdentifier推送数据。此外,segue未正确连接,重新连接segue固定转换不起作用。

1 个答案:

答案 0 :(得分:1)

尝试这样的事情。改变你的代码就像这样

ChatsViewController

import UIKit
import Contacts
import ContactsUI

class ChatsViewController: UITableViewController, CNContactPickerDelegate {
    var chats:[Chat] = chatData
    var selectedContact: CNContact = CNContact()
    @IBAction func createNewChat(sender: AnyObject) {
        let contactPickerViewController = CNContactPickerViewController()
        contactPickerViewController.delegate = self
        presentViewController(contactPickerViewController, animated: true, completion: nil)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false
        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return chats.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("ChatCell", forIndexPath: indexPath) as! ChatCell

        let chat = chats[indexPath.row] as Chat
        cell.chat = chat
        return cell
    }

    func contactPicker(picker: CNContactPickerViewController, didSelectContact contact: CNContact) {
         self.selectedContact = contact
         self.performSegueWithIdentifier("idContactSelected", sender: self)
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
         if segue.identifier == "idContactSelected" {
             let chatVC = segue.destinationViewController as! ChatViewController
             chatVC.contact = self.selectedContact
         }
    }
}

之后更改ChatViewController

import UIKit
import Contacts
import ContactsUI
class ChatViewController: UIViewController {

     @IBOutlet weak var testLabel: UILabel!
     var contact: CNContact = CNContact()

     override func viewDidLoad() {
         super.viewDidLoad()
         // Do any additional setup after loading the view.
         self.testLabel.text = contact.givenName
     }

     override func didReceiveMemoryWarning() {
         super.didReceiveMemoryWarning()
         // Dispose of any resources that can be recreated.
     }
}

希望这会对你有所帮助。