gpt4 book ai didi

iOS - 在标签栏中初始化 View Controller

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:48:50 27 4
gpt4 key购买 nike

我正在处理一个有两个选项卡的项目。在我的第一个 View Controller 上,我试图调用一个位于我的第二个 View Controller 中的方法。 Second View Controller 上的方法更新此 Second View Controller 上的 UILabel。问题是第二个 View Controller 在启动时没有初始化,因此没有正确调用该方法。我可以使用以下内容手动初始化第二个 View Controller

     if (secondView==nil) {
secondView = [[SecondView alloc]init];
}

然后该方法启动(通过在所述方法上放置断点进行测试并验证它确实触发),但我的 UILabel 没有更新,这让我相信第二个 View Controller 是在另一个线程上初始化的.

我的问题是:如何在同一线程上初始化第二个 View Controller ,以便在我单击该选项卡时更新 UI?有没有办法在 appDelegate 中做到这一点?

我已验证 IBOutlet 已正确连接到标签,.h 文件已正确导入,SecondViewController 的方法已在 .H 文件中说明。

代码如下:

FirstViewController.m

-(void)myMethod
{
[secondView updateLabel];
}

SecondViewController.m
-(void)updateLabel
{
myLabel.text = myString;
}

最佳答案

“我的问题是:如何在同一线程上初始化第二个 View Controller ……”

你不需要。如果您的 Controller 位于选项卡栏 Controller 中,则每个选项卡的根 Controller 都会在启动时实例化。如果您需要引用它,请使用类似 self.tabBarController.viewControllers[1] 的东西。这将是第二个选项卡中的 Controller 。您不想分配 init 一个,这将创建一个不同的 SecondView 实例。

即使您获得了对 secondView 的正确引用,您尝试执行的操作也不会起作用,因为 secondView 的 View 尚未加载,因此尝试更新标签将不起作用。从你的问题中不清楚为什么你试图从你的第一个 Controller 执行此操作,因为你没有从它传递任何东西。如果这就是您想要做的全部,那么只需更新 secondController 的 viewDidAppear 方法中的标签。如果你确实想从第一个 View Controller 传递一个字符串,那么你应该在 secondView 中有一个字符串属性,并从第一个 Controller 设置它的值。然后,让 secondView 将其标签的文本设置为 viewDidAppear 中的那个字符串。

关于iOS - 在标签栏中初始化 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15352169/

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