问题
I have a button in a view, I want to add tab bar controller after click the button. How can I do this?
回答1:
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];
回答2:
-(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
来源:https://stackoverflow.com/questions/8805215/how-can-i-push-tab-bar-controller-after-click-a-button-from-a-view-in-objective