gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 00:53:17 25 4
gpt4 key购买 nike

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

我想要的是,当用户使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com