2012-06-15 44 views
0

我創建了一個包含4個viewcontroller及其.h,.m文件的應用程序...在我的firstviewcontroller中,按鈕被按下到secondviewcontroller,在第二個viewcontroller中有兩個按鈕,其用於切換回firstviewcontroller和另一按鈕將變爲thirdviewcontroller.here是我firstviewcontroller.m通過選擇一個按鈕刪除視圖控制器

[[NSBundle mainBundle] loadNibNamed:@"SecondViewController" owner:self options:nil]; 

,在我secondviewcontroller的第一個按鈕

[[NSBundle mainBundle] loadNibNamed:@"FirstViewController" owner:self options:nil]; 

和另一個按鈕的代碼

[[NSBundle mainBundle] loadNibNamed:@"ThirdViewController" owner:self options:nil]; 

當我在firstviewcontroller選擇按鈕,它加載secondviewcontroller但在第二視圖控制器如果我選擇任何按鈕,我得到Sigabart警告...

任何人都可以有這個想法。我已經試過這麼多的方法..

+0

此 –

+0

使用導航控制器有導航這樣具體的要求,否則你可以使用的UINavigationController作爲Safecase建議。 – Nitish

+0

使用導航控制器作爲您的需求是這樣的 –

回答

4

您可以使用下面的方法來執行這些任務:

方法1:

在FirstViewController.m寫在b該代碼utton點擊:

SecondViewController *secondVC = [[SecondViewController alloc]initwithNibName:@"SecondViewController" bundle:nil]; 
[self.view addSubView:secondVC.view]; 

這將secondviewcontroller添加到當前視圖

在SecondViewController.m添加第三種觀點,你可以寫

ThirdViewController *thirdVC = [[ThirdViewController alloc]initwithNibName:@"ThirdViewController" bundle:nil]; 
[self.view addSubView:thirdVC.view]; 

,並刪除第二個觀點,你可以這樣寫:

[self.view removeFromSuperview]; 

方法2:

使用導航控制器。這樣

FirstViewController *FVC = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil]; 

//For Push  
[self.navigationController pushViewController:FVC animated:YES]; 

//For Pop 
[self.navigationController popViewControllerAnimated:YES]; 

:)

+0

非常感謝很多人...它的工作原理... – Dany

+1

如果代碼爲您工作,然後接受答案並喜歡它。 – Rajkumar

2

使用的代碼一般都在堆棧中一個接一個地存儲在我們推一個視圖控制器到一個又一個新的視圖控制器。並查看這些控制器在同一個反向合成器中。所以如果u要推新的視圖控制器使用這樣的:

SecondViewController *秒= [[SecondViewController的alloc] initWithNibName:@ 「SecondViewController」 束:無];

[self.navigationController pushViewController:second animated:YES];

和如果u想刪除當前視圖控制器使用:

`[自我。navigationController popViewControllerAnimated:YES];

如果u希望從任何視圖控制器的跳轉到烏爾第一視圖控制器

[self.navigationController popToRootViewControllerAnimated:YES];

你可以得到由這行代碼在該堆疊呈現的viewcontrollers

的NSArray * viewArr = [self.navigationController viewControllers];

NSLog(@「%@」,viewArr);

[self.navigationController popToViewController:[viewArr objectAtIndex:1] animated:YES];

`

1

在iPhone應用程序編程viewControllers

1
//For going in forword direction you can use this block 
{ 
FirstViewController *firstVC = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil]; 
[self.navigationController pushViewController:firstVC animated:YES]; 
[firstVC relese]; 
} 

//For returning back you can use this block 
{ 
    [self.navigationController popViewControllerAnimated:YES]; 
} 


//But before using this line of code you have to alloc and make the property of navigationController in your appDelegate 

希望這將幫助你

相關問題