在 Objective-C 中单击视图中的按钮后如何推送选项卡栏控制器?

发布于 2024-12-25 20:08:01 字数 42 浏览 3 评论 0原文

我在视图中有一个按钮,我想在单击按钮后添加选项卡栏控制器。我该怎么做?

I have a button in a view, I want to add tab bar controller after click the button. How can I do this?

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

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

发布评论

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

评论(3

瞎闹 2025-01-01 20:08:01

首先,我不认为将选项卡栏作为子视图作为一个好主意,

但如果您仍然想这样做,有很多方法可以解决

其中一个问题,首先使用 modalview

您必须添加这个创建按钮后的代码

[button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];

将事件侦听器附加到您接下来的按钮

,您使事件函数执行选项卡栏推送

-(void)buttonTapped: (UIButton *)sender
{
        YourTabBarClass *myTabBar = [[YourTabBarClass alloc]initWithNibName:nil bundle:nil];
        myTabBar.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;

        [self presentModalViewController:myTabBar animated:YES];
}

,并且不要忘记在 .m 中导入 tabbarcontroller 类头文件

#import "YourTabBarClass.h"

希望这有所帮助;)

编辑:如果你需要去从选项卡栏视图返回到上一个菜单,您可以添加一个按钮,为其提供一个事件侦听器,然后将此代码放入函数中

[self resignFirstResponder];
    [self dismissModalViewControllerAnimated:YES];

first of all, i don't think pushing a tab bar as a subview as a good idea

but if you still want to do this, there's a lot of way to work around

one of them is by using modalview

first you have to add this code after you make the button

[button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];

it attach an event listener to the button you have

next, you make the event function to do the tab bar pushing

-(void)buttonTapped: (UIButton *)sender
{
        YourTabBarClass *myTabBar = [[YourTabBarClass alloc]initWithNibName:nil bundle:nil];
        myTabBar.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;

        [self presentModalViewController:myTabBar animated:YES];
}

and dont forget to import the tabbarcontroller class header file in your .m

#import "YourTabBarClass.h"

hope this help ;)

edit : if you need to go back from the tab bar view into the previous menu, you can add a button, give it an event listener, and put this code inside the function

[self resignFirstResponder];
    [self dismissModalViewControllerAnimated:YES];
滿滿的愛 2025-01-01 20:08:01
-(IBAction)BtnPressed:(id)sender
{
    UIViewController *searchViewController = [[[SearchViewController alloc] initWithNibName:@"SearchViewController" bundle:nil] autorelease];
searchViewController.title = @"Search";

UIViewController *exploreViewController = [[[SearchViewController alloc] initWithNibName:@"ExploreViewController" bundle:nil] autorelease];
exploreViewController.title = @"Explore";

UIViewController *dialerViewController = [[[DialerViewController alloc] initWithNibName:@"DialerViewController" bundle:nil] autorelease];
dialerViewController.title = @"Dialer";

self.tabBarController = [[[UITabBarController alloc]init]autorelease];

self.tabBarController.viewControllers = [NSArray arrayWithObjects:searchViewController, exploreViewController, dialerViewController, nil];

[self presentModalViewController:tabBarController animated:YES];
}

不要忘记创建相应的 nib 文件(dialerViewController.xib、SearchViewController.xib、DialerViewController.xib)并将这些视图高度设置为 411px(这是给你的)

谢谢

-(IBAction)BtnPressed:(id)sender
{
    UIViewController *searchViewController = [[[SearchViewController alloc] initWithNibName:@"SearchViewController" bundle:nil] autorelease];
searchViewController.title = @"Search";

UIViewController *exploreViewController = [[[SearchViewController alloc] initWithNibName:@"ExploreViewController" bundle:nil] autorelease];
exploreViewController.title = @"Explore";

UIViewController *dialerViewController = [[[DialerViewController alloc] initWithNibName:@"DialerViewController" bundle:nil] autorelease];
dialerViewController.title = @"Dialer";

self.tabBarController = [[[UITabBarController alloc]init]autorelease];

self.tabBarController.viewControllers = [NSArray arrayWithObjects:searchViewController, exploreViewController, dialerViewController, nil];

[self presentModalViewController:tabBarController animated:YES];
}

Don't forget to create the corresponding nib files(dialerViewController.xib, SearchViewController.xib, DialerViewController.xib) and make these views hight to 411px(this is unto you)

thanks

揪着可爱 2025-01-01 20:08:01

这是针对斯威夫特的
设置按钮操作,然后使用以下

    self.tabBarController?.selectedIndex = (your viewcontroller) index

示例:

    self.tabBarController?.selectedIndex = 3

This is for Swift
Set your button action then use this one

    self.tabBarController?.selectedIndex = (your viewcontroller) index

Example:

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