Iphone 按按钮访问Tabbar控制器
我已经通过编程创建了一个带有视图等的TabBarController。现在我想在按下按钮时显示这个TabBarController。我该怎么做?目前,我正在以模式呈现它,但它不工作-抛出sigtrap错误 这是我的TabBarController代码Iphone 按按钮访问Tabbar控制器,iphone,objective-c,ios,ios4,uitabbarcontroller,Iphone,Objective C,Ios,Ios4,Uitabbarcontroller,我已经通过编程创建了一个带有视图等的TabBarController。现在我想在按下按钮时显示这个TabBarController。我该怎么做?目前,我正在以模式呈现它,但它不工作-抛出sigtrap错误 这是我的TabBarController代码 @implementation TabBarViewController - (void) loadView { HomeViewController * homeViewController = [[HomeViewController
@implementation TabBarViewController
- (void) loadView
{
HomeViewController * homeViewController = [[HomeViewController alloc]initWithNibName:@"HomeViewController" bundle:nil];
UITabBarController *tabBarController = [[UITabBarController alloc] init];
tabBarController.view.frame = CGRectMake(0, 0, 320, 460);
// Set each tab to show an appropriate view controller
[tabBarController setViewControllers:[NSArray arrayWithObjects:homeViewController, homeViewController, nil]];
[self.view addSubview:tabBarController.view];
[homeViewController release];
[tabBarController release];
}
这是我从mainViewController中的按钮按下事件访问此tabBarController的代码-
- (IBAction)quickBrowse:(UIButton *)sender
{
TabBarViewController * tabBarController = [[TabBarViewController alloc]init];
[self presentModalViewController:tabBarController animated:YES];
[tabBarController release];
}
只有在不使用IB并且希望手动创建视图的情况下,才应重写方法loadView。当您这样做时,必须将根视图分配给UIViewController的视图属性 我相信在您的情况下,您不需要重写此方法,您可以使用viewDidLoad方法创建UITabBarController并将其存储在变量中,因此当调用事件时,您只需将变量传递给方法presentModalViewController:animated: 您的最终代码如下所示:
- (void) viewDidLoad
{
[super viewDidLoad];
HomeViewController * homeViewController = [[HomeViewController alloc]initWithNibName:@"HomeViewController" bundle:nil];
// you can't pass the same view controller to more than one position in UITabBarController
HomeViewController * homeViewController2 = [[HomeViewController alloc]initWithNibName:@"HomeViewController" bundle:nil];
// local variable
self.modalTabBarController = [[UITabBarController alloc] init];
// Set each tab to show an appropriate view controller
[self.modalTabBarController setViewControllers:[NSArray arrayWithObjects:homeViewController, homeViewController2, nil]];
}
- (void)viewDidUnload
{
self.modalTabBarController = nil;
[super viewDidUnload];
}
- (IBAction)quickBrowse:(UIButton *)sender
{
[self presentModalViewController:self.modalTabBarController animated:YES];
}