2012-06-04 53 views
0

我正在構建一個iphone應用程序。iPhone應用程序 - uitabbarcontroller總是選擇uinavigationcontroller中的第一個視圖?

我有一個tabbarcontroller有2個選項卡項目。每個tabitem鏈接到不同的導航控制器。每個navigationcontroller鏈接到tableviewcontrollers的層次結構。 當用戶單擊選項卡1時,然後單擊表中的項目,然後單擊選項卡2,然後單擊選項卡1,應用程序將顯示他在單擊選項卡2之前正在查看的表格。

如何讓應用程序在每次點擊選項卡1時顯示選項卡1的第一個表格,而不是在離開tab1之前顯示他正在查看的最近表格?

我寧願使用程序化解決方案,而不使用xcode storyboard。但如果不存在,那麼故事板解決方案也可以。

回答

0
我appdelegate.h文件

,我改變

@interface wscAppDelegate : UIResponder <UIApplicationDelegate> 

@interface wscAppDelegate : UIResponder <UIApplicationDelegate,UITabBarControllerDelegate> 

然後在我的viewDidLoad中功能CustomTabBarController我添加了這些行線:

wscAppDelegate *appDelegate = (wscAppDelegate *)[[UIApplication sharedApplication] delegate]; 
self.delegate = appDelegate; 

,則在appdelegate.m文件,我添加了這個功能

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController 
{ 
    for(int c=0; c<[tabBarController.viewControllers count]; c++) 
    { 
     UINavigationController * navcontroller = [tabBarController.viewControllers objectAtIndex:c]; 

     [navcontroller popToRootViewControllerAnimated:YES]; 
    }  

    return YES; 

} 
0

當TabBarController更改正在顯示的選項卡時,請在NavigationController上調用popToRootViewControllerAnimated:

0

試試這個基本樣本創建中的UITabBar一樣,併爲UINavigationController的從頭每個UItabBarItem:在所謂的「功能

@interface AppDelegate : UIResponder <UIApplicationDelegate,UITabBarControllerDelegate> 

在你的頭文件

(appdelegate.h),添加此委託在此功能

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 

UINavigationController *navController=[[UINavigationController alloc] init]; 
m_ViewController1 = [[ViewController1 alloc] initWithNibName:@"ViewController1" bundle:nil]; 
[navController pushViewController:m_ViewController1 animated:NO]; 

UINavigationController *navController2=[[UINavigationController alloc] init]; 
m_ViewController2 = [[ViewController2 alloc] initWithNibName:@"ViewController2" bundle:nil]; 
[navController pushViewController:m_ViewController2 animated:NO]; 

UITabBarController *mtabBarController = [[UITabBarController alloc] init]; 
mtabBarController.view.frame = CGRectMake(0, 0, 320, 460); 

// Set each tab to show an appropriate view controller 
[mtabBarController setViewControllers: [NSArray arrayWithObjects:navController1,navController1,navController2, nil]]; 

self.window.rootViewController = mtabBarController;  
mtabBarController.delegate = self; 
[self.window makeKeyAndVisible]; 

return YES; 
} 

然後,不要忘記添加popToRootViewControllerAnimated FUNC中:didFinishLaunchingWithOptions」,增加的這部分代碼重刑:

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController 
{ 
    [m_ViewController1.navigationController popToRootViewControllerAnimated:YES]; 
    [m_ViewController2.navigationController popToRootViewControllerAnimated:YES]; 
    return YES; 
} 
+0

該文件執行功能' - (BOOL)tabBarController:(*的UITabBarController)tabBarController shouldSelectViewController:(UIViewController的*)viewController'出現? – John

相關問題