繁体   English   中英

在Objective-C中单击视图中的按钮后,如何按下标签栏控制器?

[英]How can I push tab bar controller after click a button from a view in Objective-C?

我在视图中有一个按钮,我想在单击按钮后添加标签栏控制器。 我怎样才能做到这一点?

首先,我不认为将标签栏作为子视图推送是一个好主意

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

其中之一是使用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];
-(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(这是给你的)

谢谢

设置你的按钮动作然后使用这个

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

例子:

    self.tabBarController?.selectedIndex = 3

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM