在联系人来自 CNContactPickerViewController select 后移动到新视图

Move to new view after contact is select from CNContactPickerViewController

我正在编写一个概念聊天应用程序。我现在可以开始新的聊天了。

我想要的是,当用户使用 CNContactPickerViewController 选择一个联系人时,选择器将被取消(这是有效的)并且联系人详细信息被发送到新的聊天视图(这是无效的) .我得到的错误是 func contactPicker delegate returns nil。我一直在按照 this 教程从 ChatsViewController 获取联系人数据到 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 修复了过渡无效。

尝试这样的事情。像这样更改您的代码

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.
     }
}

希望对您有所帮助。