gpt4 book ai didi

ios - 如何在非 UIView 类的 uiView 之间切换

转载 作者:行者123 更新时间:2023-11-30 13:24:58 24 4
gpt4 key购买 nike

情况是这样的。我有一个应用程序,它呈现一个 tabBarController 作为其 Root View Controller ,并带有许多 tabItems。这是我的应用程序委托(delegate)中的设置

class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

let vc1 = VC1(nibName: "VC1", bundle: nil)
let vc2 = VC2(nibName: "VC2", bundle: nil)

let menuTabBarController = MenuTabBarController(nibName: "MenuTabBarController", bundle: nil)
menuTabBarController.viewControllers = [VC1,VC2]

window = UIWindow(frame: UIScreen.mainScreen().bounds)
window?.rootViewController = menuTabBarController
window?.makeKeyAndVisible()

//Setup tab bar
UITabBar.appearance().barTintColor = UIColor.blackColor()

let IMG1 = UIImage(named: "img2.png")
let IMG2 = UIImage(named: "img2.png")

VC1.tabBarItem = UITabBarItem(
title: "View 1",
image: IMG1,
tag: 1)
VC2.tabBarItem = UITabBarItem(
title: "View 2",
image: IMG2,
tag:2)



return true
}

func applicationWillResignActive(application: UIApplication) {

}

func applicationDidEnterBackground(application: UIApplication) {

}


}

所以请注意,我有两个 UIViewController 类(VC1 和 VC2)嵌套在 menuTabBar 中。现在,从 menuTabBar 类,我可以轻松地在 View 之间切换,如下所示

    class MenuTabBarController: UITabBarController,UITabBarControllerDelegate{

override func viewDidLoad() {
super.viewDidLoad()

self.delegate = self//Set tabbarcontroller delegate

}

// UITabBarControllerDelegate
func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
tabBarController.selectedIndex = 1;//Say I wanted to transition to VC2
}

}

但我最终想要做的是能够从另一个不是 UIViewController 的类在两个 UIViewController 之间切换。我尝试过这样的事情

    class Actions(){
func someFunctionThatSwitchesToVC2(){
//So I create a reference to appDelegate which in turn, is supposed to reference the tab bar and switch between its child views

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

let tabBarController: UITabBarController = (appDelegate.window?.rootViewController as? UITabBarController)!
tabBarController.selectedIndex = 1
}
}

当我运行 someFucntionThatSwitchesToVC2 时,出现此错误“ fatal error :索引超出范围”。显然我没有做正确的事情。任何想法都非常感激PS,当我执行 tabBarController.selectedIndex = 0 时,我没有收到错误,但什么也没有发生

最佳答案

我认为您在分配 ViewController 时犯了拼写错误。更改此行:

menuTabBarController.viewControllers = [VC1,VC2]

下面一行:

menuTabBarController.viewControllers = [vc1,vc2]

关于ios - 如何在非 UIView 类的 uiView 之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37340450/

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