选项卡栏项目标题以编程方式不显示在目标 c 中

发布于 2024-12-27 03:15:59 字数 873 浏览 0 评论 0原文

我尝试以编程方式创建选项卡栏控制器,它可以工作,但我无法将标题设置为选项卡栏项目。运行应用程序时我看不到标题。我的代码在这里。请帮帮我,有什么问题吗?

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.

myTabBarController = [[UITabBarController alloc] init];        
view2Controller = [[testView alloc] init];   
view3Controller = [[testView2 alloc] init];   
view4Controller = [[testView3 alloc] init];   
view5Controller = [[testView4 alloc] init];   
view6Controller = [[testView5 alloc] init];   


myTabBarController.viewControllers = [NSArray arrayWithObjects: view2Controller, view3Controller,view4Controller,view5Controller,view6Controller,nil]; 
 UITabBarItem *tabItem = [[[myTabBarController tabBar] items] objectAtIndex:1];
 [tabItem setTitle:@"theTitle"];

[self.view addSubview:myTabBarController.view];    
myTabBarController.selectedIndex=0;

}

I try create tab bar controller programatically, it works but I can not set title to ta bar items. I can not see title when I running my application. My code is here. Please help me, what is the problem?

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.

myTabBarController = [[UITabBarController alloc] init];        
view2Controller = [[testView alloc] init];   
view3Controller = [[testView2 alloc] init];   
view4Controller = [[testView3 alloc] init];   
view5Controller = [[testView4 alloc] init];   
view6Controller = [[testView5 alloc] init];   


myTabBarController.viewControllers = [NSArray arrayWithObjects: view2Controller, view3Controller,view4Controller,view5Controller,view6Controller,nil]; 
 UITabBarItem *tabItem = [[[myTabBarController tabBar] items] objectAtIndex:1];
 [tabItem setTitle:@"theTitle"];

[self.view addSubview:myTabBarController.view];    
myTabBarController.selectedIndex=0;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

呆头 2025-01-03 03:15:59

设置 侧视图控制器的视图中的视图控制器的标题将会出现/消失,它将显示在您的选项卡项中。

Set the title of ur viewcontroller in side viewcontroller's viewwillappear/disappear and it will be displayed in ur tabbaritem.

友欢 2025-01-03 03:15:59

您可以设置 UIViewController 的标题,该标题反映了它何时被推送到 UINavigationControllerUITabBarController 中。但您应该先设置标题,然后再将其放入其中任何一个中。

init 通常是设置标题的好地方。

- (id)init {
    // ... other code including check for self
    self.title = @"My Title";
    return self;
}

You can set the title of UIViewController, which reflects when it is pushed in UINavigationController or a UITabBarController. But you should set the title before putting it inside any of them.

init is generally a good place to set the title.

- (id)init {
    // ... other code including check for self
    self.title = @"My Title";
    return self;
}
離殇 2025-01-03 03:15:59
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

        // self.title = @"My Title";
    }

    self.title = @"My Title";
    return self;
}

在标签栏控制器的每个子视图中使用上面的代码。

这可能会有帮助。

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

        // self.title = @"My Title";
    }

    self.title = @"My Title";
    return self;
}

Use above code in every subviews of tabbar controllers.

This may be help.

静水深流 2025-01-03 03:15:59
use this code

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title=@"FREE BOOKS";
        self.tabBarItem.image = [UIImage imageNamed:@"first"];
    }
    return self;
}
use this code

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title=@"FREE BOOKS";
        self.tabBarItem.image = [UIImage imageNamed:@"first"];
    }
    return self;
}
时光沙漏 2025-01-03 03:15:59

这里是从头开始的教程,应该可以帮助您实现您想要实现的确切功能。下载源代码并直接运行,如果标题正确,您可以按照教程正确理解 UITabBarController 的流程。

Here is the tutorial from the scratch which should help you to achieve the exact functionality you want to achieve. Download the source code and run it directly and if title are correct the you can follow the tutorial to get the proper understanding of the flow of UITabBarController.

迟到的我 2025-01-03 03:15:59
override func viewDidLoad() {
    super.viewDidLoad()

    DispatchQueue.main.async {
        self.tabBar.items?[0].title = "Title"
    }

}

或者

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    self.tabBar.items?[0].title = "Title"

}
override func viewDidLoad() {
    super.viewDidLoad()

    DispatchQueue.main.async {
        self.tabBar.items?[0].title = "Title"
    }

}

OR

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    self.tabBar.items?[0].title = "Title"

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文