我创建了一个传递" itemInformation"的协议。到另一个视图控制器通过代理如下:
protocol ItemDataDelegate {
func ItemInformation(itemID:String,itemname:String,itemPrice:String)
}
class ItemTableViewCell: UITableViewCell {
var passItemID:String?
var PassItemName : String?
var PassAddon : String?
var PassItemPrice : String?
var Itemdelegate: ItemDataDelegate?
@IBOutlet var itemName: UILabel!
@IBOutlet var itemPrice: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
@IBAction func OrderBtnPressed(sender: AnyObject) {
print(PassItemName)
print(PassAddon)
print(passItemID)
print(PassItemPrice)
let itemnameInfo:String = PassItemName!
let itempriceInfo:String = PassItemPrice!
let itemID:String = passItemID!
Itemdelegate?.ItemInformation(itemID,itemname:itemnameInfo as String, itemPrice: itempriceInfo as String)
}
}
现在我在这里通过实现它来检索协议。为了exapmle我只是试图在这里打印传递值,但没有任何反应。
class CustomAddOnVC: UIViewController,UITableViewDataSource,UITableViewDelegate, ItemDataDelegate{
var ItemTableViewCellObj:ItemTableViewCell?
override func viewDidLoad() {
super.viewDidLoad()
ItemTableViewCellObj?.Itemdelegate = self
}
func ItemInformation(itemID: String, itemname: String, itemPrice: String) {
print(itemID)
print(itemname)
print(itemPrice)
}
}
我在这里做错了吗?
答案 0 :(得分:0)
在CustomAddOnVC中,请写
ItemTableViewCellObj.Itemdelegate = self
在viewDidLoad或其中的ItemTableViewCell。在初始化。并使用弱引用定义委托
weak var Itemdelegate: ItemDataDelegate?
在ItemTableViewCell OrderBtnPressed方法
中替换此行 Itemdelegate?.ItemInformation(itemID,itemname:itemnameInfo as String, itemPrice: itempriceInfo as String)
用这个
self.itemDelegate?.itemInformation("itemID", itemname: "itemname", itemPrice: "itemPrice")