gpt4 book ai didi

ios - 单击 UITabbarItem 打开 UIViewController

转载 作者:行者123 更新时间:2023-11-28 19:30:32 26 4
gpt4 key购买 nike

我创建了一个带有 UITabbar 的 UIViewController。

enter image description here

我没有使用 UITabbarController,因为我希望 UITabbar 位于屏幕顶部。

单击 tab1 时,我想显示 Controller 1,单击 tab2 时,我想显示 Controller 2。我不想隐藏标签栏。我想在标签栏下方显示 Controller 。

@interface MTLeaderFactoViewController () <UITabBarDelegate>
@property (weak, nonatomic) IBOutlet UITabBar *tabBar;
@end

@implementation MTLeaderFactoViewController

- (void)viewDidLoad {
[super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
if (item.tag == 0) {
NSLog(@"item tag 0");
} else {
NSLog(@"item tag 1");
}
}
@end

我的问题:

1) 即使在使用 UITabbarDelegate 之后,didSelectItem 方法也没有被触发

2) 单击按钮时显示 Controller 的最优雅方式是什么?我不想使用 segue,因为所有 Controller 都在不同的 Storyboard中。现在,我打算做

Controller1 *fp = [Controller1 controllerStoryboard:STORYBOARD_COURSE];
[self addChildViewController:fp];
[self.view addSubview:fp.view];
[fp didMoveToParentViewController:self];

编辑 1:

    Controller1 *fp = [Controller1 controllerStoryboard:STORYBOARD_COURSE];
[self addChildViewController:fp];
[self.view addSubview:fp.view];
[fp didMoveToParentViewController:self];

试过了,但它隐藏了标签栏。我想利用标签栏下方的空间来显示 Controller

最佳答案

你需要做的是有一个 basecontroller 类,它将包含一个标签栏(以编程方式创建)然后你可以在这里获得所需的输出我创建的示例 baseController,

import UIKit

class BaseViewController: UIViewController,UITabBarDelegate{

override func viewDidLoad() {
super.viewDidLoad()

let myTabBar = UITabBar()
myTabBar.frame = CGRect(x: 0, y: 60, width:self.view.frame.size.width, height: 50)

let one = UITabBarItem()
one.title = "one"
one.tag = 1
let two = UITabBarItem()
two.title = "two"
two.tag = 2
myTabBar.setItems([one,two], animated: false)
self.view.addSubview(myTabBar)
myTabBar.delegate = self

}


func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
switch item.tag {
case 1:
let controller = storyboard?.instantiateViewController(withIdentifier: "SecondViewController")
addChildViewController(controller!)
view.addSubview((controller?.view)!)
controller?.didMove(toParentViewController: self)
break
case 2:
let controller = storyboard?.instantiateViewController(withIdentifier: "ViewController")
addChildViewController(controller!)
view.addSubview((controller?.view)!)
controller?.didMove(toParentViewController: self)
break
default:
break
}

}

}

View Controller 类:

import UIKit

class ViewController: BaseViewController {


override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}


}

第二 View Controller :

import UIKit

class SecondViewController: BaseViewController {

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
}



}

关于ios - 单击 UITabbarItem 打开 UIViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45319570/

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