一个 ViewController,多个 tabBar 项目
One ViewController, multiple tabBar items
我现在有一个带有一个项目的 tabBar。对于这个项目,我有一个 ViewController.
现在我想向标签栏动态添加更多项目,这些项目应该都打开相同 ViewController。我将在 viewcontroller 中检查哪个按钮被按下并自定义内容。
如何将更多项目添加到链接到相同 Viewcontroller 的标签栏?
我试图将 UITabBarItem
s 添加为列表,但这行不通。
有什么建议吗?
您应该能够继承 UITabBarController
并使用 viewControllers
属性 或 viewDidLoad
中的 setViewController(_:animated:)
方法。我建议使用 .nib
布局 ViewController
,并使用 init(nibName: String?, bundle: Bundle?)
.
实例化它
与其让 ViewController
根据它的 tabBarItem
属性 确定配置,不如在设置 viewController
属性 之前进行配置你的 UITabBarController
.
大概是这样:
class TabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
var controllers = [UIViewController]()
let firstViewController = ViewController(nibName: "NIBNAME", bundle: Bundle.main)
// Configure unique properties for firstViewController here, including
// the tabBarItem.
controllers.append(firstViewController)
// Configure the rest of the ViewControllers with unique properties and add them to controllers
setViewControllers(controllers, animated: false)
}
}
此外,请注意,如果您有超过 5 个控制器,则需要在 UITabBarController 子类中使用 moreNavigationController
属性。
我建议您 read the documentation for UITabBarController 了解如何执行所有这些操作。
虽然我不喜欢你最初在tabController中多个SAME的想法VC,但实际上它是可行的。
import UIKit
class MyTabViewController : UIViewController{
override var tabBarItem: UITabBarItem!{
get{ return UITabBarItem.init(title: "temp", image: nil, tag: 100) }
set{ super.tabBarItem = newValue} }
}
class MyTabController: UITabBarController {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
perform(#selector(change), with: nil, afterDelay: 3.0)
perform(#selector(printViewController), with: nil, afterDelay: 5.0)
}
@objc func printViewController(){
print (viewControllers!)
}
@objc func change(){
if let viewController = self.viewControllers?[0]{
let label = UILabel.init(frame: CGRect.init(x: 0, y: 0, width: 100, height: 100))
label.text = "testing"
viewController.view.addSubview(label)
setViewControllers([viewController,viewController,viewController,viewController,viewController], animated: true)
}
}
}
5 秒后,您可以看到您的 tabController 中有 5 个相同的 vc。
我现在有一个带有一个项目的 tabBar。对于这个项目,我有一个 ViewController.
现在我想向标签栏动态添加更多项目,这些项目应该都打开相同 ViewController。我将在 viewcontroller 中检查哪个按钮被按下并自定义内容。
如何将更多项目添加到链接到相同 Viewcontroller 的标签栏?
我试图将 UITabBarItem
s 添加为列表,但这行不通。
有什么建议吗?
您应该能够继承 UITabBarController
并使用 viewControllers
属性 或 viewDidLoad
中的 setViewController(_:animated:)
方法。我建议使用 .nib
布局 ViewController
,并使用 init(nibName: String?, bundle: Bundle?)
.
与其让 ViewController
根据它的 tabBarItem
属性 确定配置,不如在设置 viewController
属性 之前进行配置你的 UITabBarController
.
大概是这样:
class TabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
var controllers = [UIViewController]()
let firstViewController = ViewController(nibName: "NIBNAME", bundle: Bundle.main)
// Configure unique properties for firstViewController here, including
// the tabBarItem.
controllers.append(firstViewController)
// Configure the rest of the ViewControllers with unique properties and add them to controllers
setViewControllers(controllers, animated: false)
}
}
此外,请注意,如果您有超过 5 个控制器,则需要在 UITabBarController 子类中使用 moreNavigationController
属性。
我建议您 read the documentation for UITabBarController 了解如何执行所有这些操作。
虽然我不喜欢你最初在tabController中多个SAME的想法VC,但实际上它是可行的。
import UIKit
class MyTabViewController : UIViewController{
override var tabBarItem: UITabBarItem!{
get{ return UITabBarItem.init(title: "temp", image: nil, tag: 100) }
set{ super.tabBarItem = newValue} }
}
class MyTabController: UITabBarController {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
perform(#selector(change), with: nil, afterDelay: 3.0)
perform(#selector(printViewController), with: nil, afterDelay: 5.0)
}
@objc func printViewController(){
print (viewControllers!)
}
@objc func change(){
if let viewController = self.viewControllers?[0]{
let label = UILabel.init(frame: CGRect.init(x: 0, y: 0, width: 100, height: 100))
label.text = "testing"
viewController.view.addSubview(label)
setViewControllers([viewController,viewController,viewController,viewController,viewController], animated: true)
}
}
}
5 秒后,您可以看到您的 tabController 中有 5 个相同的 vc。