[英]Prepare for segue not getting called in UITabbarController
我浏览了有关该主题的所有其他帖子,但它们似乎对我没有帮助。
我有一个正在启动两个选项卡的UITabBarController。 我想将在tab1中收集的数据传递给UITabBar ViewController。 我试图为此使用delegete协议,但是在发送ViewController中设置委托变量时遇到问题。 进行准备的准备从未得到要求。 我什至无法循环浏览Tabbar控制器的ViewDidLoad()内的选项卡的视图控制器,因为它们尚未创建,因此无效。
我以前使用过委托,这似乎很简单。 我在标签栏中使用它有关系吗?
当我运行代码时,将调用TabBarViewController中的viewDidLoad(),但不会调用preparefor segue。
调用了MeViewController中的IBAction donePressed,但未调用委托,因为未设置委托。
这是代码-
protocol DetailsDelegate: class {
func myDetailsGathered( myDetails: MyDetails )
}
/// RECEIVING VIEW CONTROLLER
class TabBarViewController: UITabBarController, DetailsDelegate
{
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
print("prepare for segue called\n");
if let destinationViewController = segue.destination as? MeViewController
{
destinationViewController.delegate = self
}
}
override func viewDidLoad()
{
print("ViewDidLoad Called \n")
}
func myDetailsGathered(myDetails: MyDetails)
{
self.myDetails = myDetails
print("My details gathered \n")
}
}
---------------
/// SENDING VIEW CONTROLLER
class MeViewController: UIViewController
{
weak var delegate: DetailsDelegate?
override func viewDidLoad()
{
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
}
// I have UIButton in the view and this is invoked when its pressed.
@IBAction func donePressed(_ sender: Any)
{
var infoToPass = MyDetails()
print("looks like we are done")
delegate?.myDetailsGathered(infoToPass: myDetails)
}
}
执行segue时会调用prepareForSegue
。 您不这样做,这就是为什么它不被调用的原因。
顺序定义了应用程序的故事板文件中两个视图控制器之间的过渡。
您应该使用singleton
类来存储变量并在不同的控制器之间访问它们。 您可以这样声明:
class Singleton {
static let sharedInstance = Singleton()
var name = ""
}
分配给Singleton:
Singleton.sharedInstance.name = "Some name"
要从任何控制器读取它:
let name = Singleton.sharedInstance.name
首先,为什么要让tabbarController接收一些信息/数据?
进行准备的准备从未得到要求。
prepareForSegue
方法将在performSegue
之后立即调用。 那么您的performSegue
方法在哪里? 或者是你确信那种segue
要MeViewController
正在执行?
您还有一个选择是使用NotificationCenter
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.