ios - 从 CNContactPickerViewController 选择联系人后移动到新 View

标签 ios swift contacts

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

我想要的是,当用户使用 CNContactPickerViewController 选择一个联系人时,选择器被取消(这是有效的)并且联系人详细信息被发送到一个新的聊天 View (这是无效的).我得到的错误是,在 func contactPicker 处,delegate 返回 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 从未设置,只是创建,但在教程中这就是他们所做的一切,而且似乎有效。

在此先感谢您对此的任何想法。

编辑: 最后的解决方案:我不需要委托(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.
     }
}

希望这对您有帮助。

关于ios - 从 CNContactPickerViewController 选择联系人后移动到新 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37876975/

相关文章:

csv - 合并来自 vcf 的大量联系人的重复项

iphone - 将 CSV 文件附加到电子邮件目标 - C

xcode - 添加使用 IB 构建的 UINavigationController

ios - OneSignal iOS - 无法阻止 webview

ios - 如何检索所有连接的蓝牙外围设备

ios - 键盘出现时如何调整ScrollView的大小?

ios - 如何访问 MKMapItem 的类别或类型

android - 访问联系人过滤器

ios - 呈现特定的 TabBarItem - iOS

android - 尝试编写和更新联系人