gpt4 book ai didi

ios - 遍历电话号码,创建自定义 View 并 swift 将电话号码传递给它

转载 作者:行者123 更新时间:2023-11-28 08:17:01 24 4
gpt4 key购买 nike

我在另一个 Controller 的 for 循环内以编程方式创建 View 时遇到问题。父 Controller 是一个 tableviewcell,我正在循环访问 CNContact 对象中的一堆电话号码。对于联系人拥有的每个电话号码,我希望创建自定义 View 并将其添加到 tableviewcell 并使其垂直堆叠。

到目前为止,我设法创建了 View 并将其添加到 tableviewcell,但无法传递数据。这是我正在努力解决的将数据从一个 Controller 传递到另一个 Controller 的问题。

这是我的代码:

ContactListTableViewCell.swift

import UIKit
import Contacts

class ContactListTableViewCell: UITableViewCell {

@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var phonenumberView: UIView!

func configureCell(contact: CNContact) {
titleLabel.text = "\(contact.givenName) \(contact.familyName)"

for phoneNumber in contact.phoneNumbers {

let view = self.createContactListTableViewTelephoneRow(telephone: phoneNumber)
self.phonenumberView.addSubview(view)


}


}

func createContactListTableViewTelephoneRow(telephone: Any) -> UIView {
let controller = ContactListTableViewTelephoneRow()
let view = UINib(nibName: "ContactListTableViewTelephoneRow", bundle: nil).instantiate(withOwner: controller, options: nil)[0] as! UIView
return view
}

}

Main.storyboard 中的 contactListTableViewCell 原型(prototype) enter image description here

ContactListTableViewTelephoneRow.swift

class ContactListTableViewTelephoneRow: UIView {

@IBOutlet var view: UIView!
@IBOutlet weak var telephoneLabel: UILabel!
@IBOutlet weak var telephoneTypeLabel: UILabel!

func setData(telephoneLabelText: String, telephoneTypeLabelText: String) {
telephoneLabel?.text = telephoneLabelText
telephoneTypeLabel?.text = telephoneTypeLabelText
}


}

ContactListTableViewTelephoneRow.xib

enter image description here

任何帮助将不胜感激。谢谢。

最佳答案

传递数据的简单方法是您需要在第二个 Controller 中创建对象并从第一个 Controller 传递数据

let vc = self.storyboard!.instantiateViewController(withIdentifier: "Secondcontroller") as! Secondcontroller
vc.yourObject = object //To pass
self.present(tabvc, animated: true, completion: nil) // or push

关于ios - 遍历电话号码,创建自定义 View 并 swift 将电话号码传递给它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42342710/

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