UITabBarController 仅显示第一个选项卡
UITabBarController showing only the first tab
我重新提出这个问题是因为 UITabbarController showing only first tab 中的问题没有得到回答...我不明白为什么我的 UITabBarController 只显示第一个选项卡。
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.backgroundColor = UIColor.darkGray
self.window?.makeKeyAndVisible()
let tab = UITabBarController()
let v1 = VC1(nibName: "View1", bundle: nil)
let v2 = VC2(nibName: "View2", bundle: nil)
let v3 = VC3(nibName: "View3", bundle: nil)
let myViews = [v1,v2,v3]
tab.viewControllers = myViews
self.window?.rootViewController = tab
return true
}
}
VC1代码:
class VC1: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
print("view 1 will load")
self.title = "View 1"
// Do any additional setup after loading the view.
}
}
class VC2:
class VC1: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
print("view 2 will load")
self.title = "View 2"
// Do any additional setup after loading the view.
}
}
VC3:
class VC1: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
print("view 3 will load")
self.title = "View 3"
// Do any additional setup after loading the view.
}
}
我认为这是一种经过测试和验证的创建选项卡的方法,但它似乎不起作用,这是结果截图
我找到了一种更简洁的实现方法,View Controller 的标题是 属性 并且可以在实例化后立即设置
因此更新后的代码如下所示:
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.backgroundColor = UIColor.darkGray
self.window?.makeKeyAndVisible()
let tab = UITabBarController()
let v1 = VC1(nibName: "View1", bundle: nil)
let v2 = VC2(nibName: "View2", bundle: nil)
let v3 = VC3(nibName: "View3", bundle: nil)
v1.title = "View 1"
v2.title = "View 2"
v3.title = "View 3"
let myViews = [v1,v2,v3]
tab.setViewControllers(myViews, animated: false)
self.window?.rootViewController = tab
return true
}
}
屏幕截图是:
UITabBarController 使用来自每个视图控制器的 title
来创建显示在屏幕上的标签。这里的问题是,在调用 viewDidLoad
之前,您的视图控制器对 title
属性 没有任何价值,但为时已晚。
您应该在初始化期间设置 title
属性; initWithNibName:bundle:
是个好地方。
我重新提出这个问题是因为 UITabbarController showing only first tab 中的问题没有得到回答...我不明白为什么我的 UITabBarController 只显示第一个选项卡。
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.backgroundColor = UIColor.darkGray
self.window?.makeKeyAndVisible()
let tab = UITabBarController()
let v1 = VC1(nibName: "View1", bundle: nil)
let v2 = VC2(nibName: "View2", bundle: nil)
let v3 = VC3(nibName: "View3", bundle: nil)
let myViews = [v1,v2,v3]
tab.viewControllers = myViews
self.window?.rootViewController = tab
return true
}
}
VC1代码:
class VC1: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
print("view 1 will load")
self.title = "View 1"
// Do any additional setup after loading the view.
}
}
class VC2:
class VC1: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
print("view 2 will load")
self.title = "View 2"
// Do any additional setup after loading the view.
}
}
VC3:
class VC1: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
print("view 3 will load")
self.title = "View 3"
// Do any additional setup after loading the view.
}
}
我认为这是一种经过测试和验证的创建选项卡的方法,但它似乎不起作用,这是结果截图
我找到了一种更简洁的实现方法,View Controller 的标题是 属性 并且可以在实例化后立即设置
因此更新后的代码如下所示:
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.backgroundColor = UIColor.darkGray
self.window?.makeKeyAndVisible()
let tab = UITabBarController()
let v1 = VC1(nibName: "View1", bundle: nil)
let v2 = VC2(nibName: "View2", bundle: nil)
let v3 = VC3(nibName: "View3", bundle: nil)
v1.title = "View 1"
v2.title = "View 2"
v3.title = "View 3"
let myViews = [v1,v2,v3]
tab.setViewControllers(myViews, animated: false)
self.window?.rootViewController = tab
return true
}
}
屏幕截图是:
UITabBarController 使用来自每个视图控制器的 title
来创建显示在屏幕上的标签。这里的问题是,在调用 viewDidLoad
之前,您的视图控制器对 title
属性 没有任何价值,但为时已晚。
您应该在初始化期间设置 title
属性; initWithNibName:bundle:
是个好地方。