gpt4 book ai didi

ios - 在 Swift 中以编程方式调用 UITabBarController 委托(delegate)

转载 作者:行者123 更新时间:2023-11-29 02:21:50 28 4
gpt4 key购买 nike

我在 Objective-C 中有一个完美的工作代码,它被调用以根据特定条件以编程方式更改所选选项卡。

-(void)loadNewView
{
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
[tabBarController setSelectedIndex:2];
[tabBarController.delegate tabBarController:tabBarController didSelectViewController:[tabBarController.viewControllers objectAtIndex:2]];
}

我正在尝试在 Swift 中获得相同的等价物,下面是我尝试过的代码

func loadNewView() {
var tabbarController: UITabBarController = self.window?.rootViewController as UITabBarController
tabbarController.selectedIndex = 2
var svc = tabbarController.viewControllers[2] as UINavigationController
tabbarController.delegate?.tabBarController(tabbarController, didSelectViewController:svc)
}

但是我收到“[AnyObject]? 没有名为下标的成员”。我知道上面的 Swift 代码有问题,但有人可以帮助我理解错误吗?

最佳答案

是的,[AnyObject]?,它是一个可选数组,没有名为 subscript 的成员,但 [AnyObject] 有,即您不能在可选数组上使用下标表示法,因此您必须先将其解包与“!”。

试试这个:

var svc = tabbarController.viewControllers![2] as UINavigationController

关于ios - 在 Swift 中以编程方式调用 UITabBarController 委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28044712/

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