我有这两个文件,tab2和KeyboardViewController。我正在尝试在这两个文件之间创建一个委托。
为简单起见,我删除了一些必需的tableView函数。
KeyboardViewController链接到一个名为keyboard.storyboard的情节提要。 tab2链接到另一个名为main.storyboard的故事板。
我想将数据从KeyboardViewController中名为Lines的列表发送到文件tab2,并将其追加到该文件的列表中。
我的问题是程序正在运行,但是当我尝试将委托从KeyboardViewController发送到tab2时,tab2文件将无法接收数据。 (例如,我在textChoice函数中放置的打印根本无法运行)
我试图以不同的方式启动tab2 Viewcontroller,但没有成功。谢谢您的时间。
tab2.swift
echo $PATH | tr ':' '\n'
KeyboardViewController.swift
import UIKit
class tab2: UIInputViewController, UITableViewDelegate, UITableViewDataSource, icebreakerData {
func textChoice(string: String) {
print("textChoice is run")
likedList.append(string)
}
var myVC = KeyboardViewController()
var likedList: [String] = ["Hello there", "Hello there again"]
@IBOutlet var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
tableView.dataSource = self
tableView.delegate = self
print("INITIATING THE VCs")
myVC.iceBreakerDataDelegate = self
// this code makes the app crash when i enter site in the app
// let myVC = self.storyboard?.instantiateViewController(withIdentifier: "tab2") as? KeyboardViewController
// myVC?.iceBreakerDataDelegate = self
// navigationController!.pushViewController(myVC!, animated: true)
// self.present(myVC!, animated: true)
}
}
答案 0 :(得分:0)
问题是参考。我可以假设tab2
是您的初始View Controller,并从中实例化KeyboardViewController
。虽然您创建了
var myVC = KeyboardViewController()
,但是可能会从另一个对象向您展示/导航KeyboardViewController
。喜欢
let storyboard = UIStoryboard(name: "keyboard", bundle: nil)
let VC = storyboard.instantiateViewController(withIdentifier: "KeyboardViewController") as! KeyboardViewController
VC.iceBreakerDataDelegate = self // in this object you should conform self
present(VC, animated: true, completion: nil)
这就是为什么你没钱的原因。
或者,如果您使用segue,则可以执行以下操作
if(segue.identifier == "YourSegueIdentifier"){
let v = segue.destinationViewController as! YourViewController
v.delegate = self
}