2012-09-07 85 views
-1

在我的應用程序中,我有4個選項卡,在第一個選項卡上:我有4個視圖。在第二個視圖中,有一個按鈕將打開第三個標籤視圖。但我選擇的標籤索引是一個。我怎樣才能把它變成第三。更改所選的TabBar索引按鈕單擊

我對標籤的條形碼是的appdelegate如下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    UINavigationController *nav1 = [[UINavigationController alloc] init]; 
     UIViewController *viewController1 = [[[CarAccidentAppViewController alloc] 
    initWithNibName:@"CarAccidentAppViewController_iPhone" bundle:nil] 
    autorelease]; 
     nav1.viewControllers = [NSArray arrayWithObjects:viewController1, nil]; 

     //for steps tab... 

     UINavigationController *nav2 = [[UINavigationController alloc] init]; 
     UIViewController *viewController2 = [[[FirstSteps alloc] initWithNibName:@"FirstSteps" bundle:nil] autorelease]; 
     nav2.viewControllers = [NSArray arrayWithObjects:viewController2, nil]; 

     //for profiles tab... 

     UINavigationController *nav3 = [[UINavigationController alloc] init]; 
     UIViewController *viewController3 = [[[Profiles alloc] initWithNibName:@"Profiles" bundle:nil] autorelease]; 
     nav3.viewControllers = [NSArray arrayWithObjects:viewController3, nil]; 

     //for contact us tab... 

     UINavigationController *nav4 = [[UINavigationController alloc] init]; 
     UIViewController *viewController4 = [[[ContactUs alloc] initWithNibName:@"ContactUs" bundle:nil] autorelease]; 
     nav4.viewControllers = [NSArray arrayWithObjects:viewController4, nil]; self.tabBarController = [[[UITabBarController alloc] init] 
    autorelease]; 
     self.tabBarController.viewControllers = [NSArray arrayWithObjects:nav1,nav2,nav3,nav4 ,nil]; 

     self.window.rootViewController=self.tabBarController; 
     [self.window makeKeyAndVisible]; 
     return YES; 

} 

回答

1

在按鈕單擊

[self.navigationController pushViewController:YourView animated:YES]; 
[self.tabBarController setSelectedIndex:index]; 
+0

在我的應用程序 – piyush

+0

沒有工作你在你的代碼放置.. – Rajneesh071

+1

此作品在我的應用程序很好,我回答說:到我意識到Rajneesh071發佈相同的代碼片段之前的問題。 – Plasma

1
self.tabBarController.selectedViewController=[self.tabBarController.viewControllers objectAtIndex:3]; 

試試這個。這對你有幫助。

+0

它是工作,但我去第三個標籤的第一個視圖。我想要去第三個標籤的第二個視圖控制器。 – piyush

2

在應用程序內編程切換標籤最簡單的方法是這樣..

[self.tabBarController setSelectedIndex:3]; 

我在應用程序中的一個使用這個和它的作品非常好。

等離子

+0

但我想去第三個標籤的第二個視圖 – piyush

+0

可能還有其他的方法,但我會在某處設置一個變量,我會在該標籤的viewDidLoad上讀取它。所以你設置myVC = 2,然後切換到Tab 3,當它加載它會檢查myVc,你以前設置爲2並加載第二個視圖控制器。 – Plasma

1

斯威夫特 2,你可以叫:

self.tabBarController?.selectedIndex = 0