簡體   English   中英

在 Swift 中以編程方式調用 UITabBarController 委托

[英]Calling UITabBarController delegate programmatically in Swift

我在 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]? 是一個可選數組,它沒有名為下標的成員,但 [AnyObject] 有,即您不能在可選數組上使用下標表示法,因此您必須先用“! ”。

嘗試這個:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM