[英]Accessing TabBarController on Button Press
我已經以編程方式創建了具有視圖等的TabBarController。現在,我想在Button Press上顯示此TabBarController。 我怎么做? 目前,我正在以模態方式顯示它,但是它不起作用-拋出sigtrap錯誤。
這是我的TabBarController代碼
@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的Button Press事件訪問此tabBarController的代碼-
- (IBAction)quickBrowse:(UIButton *)sender
{
TabBarViewController * tabBarController = [[TabBarViewController alloc]init];
[self presentModalViewController:tabBarController animated:YES];
[tabBarController release];
}
如果不使用IB,並且要手動創建視圖,則僅應覆蓋loadView方法。 並且在執行此操作時, 必須將根視圖分配給UIViewController的view屬性。
我相信在您的情況下,您無需重寫此方法,可以使用viewDidLoad方法創建UITabBarController並將其存儲在變量中,因此,在調用事件時,您需要做的就是將變量傳遞給方法presentModalViewController:動畫化:
您的最終代碼如下所示:
- (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];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.