2012-11-03 30 views
0

我已經編程添加了一個UITabBarController(搜索,登錄),我想設置第二個tabbaritem(登錄)和它的視圖,當登錄憑據不正確時選擇。但我無法這樣做,所以..不明白在哪裏我錯了..?如何使第二個tabbarItem被選爲默認iPhone?

Search *first = [[Search alloc] initWithNibName:@"Search" bundle:nil]; 
    [email protected]"Search"; 
    Login *second=[[Login alloc]initWithNibName:@"Login" bundle:nil]; 
    [email protected]"Login"; 
    NSArray *viewArray= [NSArray arrayWithObjects:first,second, nil]; 
    tabBarController=[[UITabBarController alloc] init]; 
    [tabBarController setViewControllers:viewArray animated:NO]; 
    [tabBarController setSelectedIndex:2]; 
    [self presentModalViewController:tabBarController animated:NO]; 

但在這裏我的SearchViewController被選中並顯示爲默認值......我在哪裏出錯了?

回答

0

錯誤是在這一行:

[tabBarController setSelectedIndex:2]; 

開始計數不爲1,但是從0開始,所以你的情況,你必須將其更改爲:

[tabBarController setSelectedIndex:1]; 
+0

我想我們的代碼。但仍Imm將第一個ViewController設置爲默認值..我該怎麼做......? – Honey

+0

你是否在appDelegate中創建並初始化tabBarController?如果沒有,請僅在您的appDelegate中創建並初始化您的tabBarController。 – edzio27

+0

是的,我有這個Appdelegate.m - (BOOL)應用:(UIApplication的*)應用didFinishLaunchingWithOptions:(NSDictionary的*)launchOptions { 搜索*第一= [[搜索的alloc] initWithNibName:@ 「搜索」 束:無]; first.title = @「搜索」; Login * second = [[Login alloc] initWithNibName:@「Login」bundle:nil]; second.title = @「登錄」;我們可以通過下面的例子來說明一下:NSArray * viewArray = [NSArray arrayWithObjects:first,second,nil]; tabBarController = [[UITabBarController alloc] init]; [tabBarController setViewControllers:viewArray animated:NO]; [self.window addSubview:tabBarController.view]; – Honey

相關問題