0

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

4

3 回答 3

1

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

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

其中之一是使用 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];
于 2012-01-11T02:44:47.850 回答
0
-(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(这是给你的)

谢谢

于 2012-05-01T10:06:34.610 回答
0

这是为 Swift 设置你的按钮操作然后使用这个

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

例子:

    self.tabBarController?.selectedIndex = 3
于 2019-12-27T07:03:54.863 回答