2012-08-07 31 views
-1

我有一個按鈕,打開另一個viewController (familyView)點擊。 在familyView有另一個按鈕,它假設帶我回到mainViewController(ViewController.xib)但我不知道如何調用主視圖控制器。使用UIButton在不同的viewControllers之間跳轉?

我的方法調用familyView

UIViewController* familyView = [[UIViewController alloc] initWithNibName:@"familyView" bundle:[NSBundle mainBundle]]; 
[self.view addSubview:familyView.view]; 

我希望你能不能幫上如何調用主ViewController?我必須使用相同的方法來調用它嗎?我的意思是:

UIViewController* mainView = [[UIViewController alloc] initWithNibName:@"viewController" bundle:[NSBundle mainBundle]]; 
[self.view addSubview:mainView.view]; 

如果是,是否有更好的實現方法?在我的演示項目中,我試圖讓7個視圖充滿數據和一個按鈕來回。

編輯: 如果我使用UIView會最適合我,而不是使用不同的viewControllers與他們的實現和接口文件? 我的項目將有視圖,並且每個視圖都有從不同的html頁面解析的數據。

+0

要顯示7個視圖,請查看'UINavigationController',或者甚至是'UIPageViewController'。這可能會有所幫助。 – matsr 2012-08-07 19:01:43

+0

除了顯示的數據之外,7個視圖中的每一個都是相同的嗎?你使用什麼對象來顯示這些數據,文本視圖,Web視圖,還有其他的東西?另外,你是否希望能夠在所有視圖之間來回移動,或者只是從主視圖到其他7個視圖中的任何一個,然後再返回到主視圖? – rdelmar 2012-08-07 22:04:19

+0

我還沒決定結構。這是一個演示項目,所以我想在旅途中學習和實施。主要是,用戶應該在視圖之間跳轉到另一個視圖,或者像你說的那樣來回跳動。正如matsr所建議的那樣,我開始使用'UINavigationController'。 – Sobiaholic 2012-08-08 20:55:20

回答

1

有兩種方法可以使用。

  1. 的UINavigationController
  2. 代表

從你的問題似乎是一個UINavigationController是最好的選擇,但我會告訴你們倆。


的UINavigationController

當您從您的應用程序委託加載mainViewController您將需要包裝在一個導航控制器,像這樣:

AppDelegate.h

@property (strong, nonatomic) UINavigationController *navController; 

AppDelegate.m

@synthesize navController = _navController; 

//在didFinishLaunchingWithOptions:

UIViewController *mainViewController = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil]; 

navController = [[UINavigationController alloc] initWithRootViewController:mainViewController]; 

self.window.rootViewController = nav1; 
[self.window makeKeyAndVisible]; 

現在您的MainViewController你的UINavigationController的說服。

當你要推到一個孩子從家長,你可以簡單地做:

ChildViewController *child = [[ChildViewController alloc]...]; 
[self.navigationController pushViewController:child animated:YES]; 

如果您在ChildViewController並想回到簡單地做:

[self.navigationController popViewControllerAnimated:YES]; 

這是「向下鑽取」技術。

(我知道「向下鑽取」具有更多的意義不是簡單地說,但它提供了一個很好的參考幀)。


代表

現在,另一種方法,你有是在類之間設置代理。所以如果你在childView中並且需要打電話給你的父母,你將有一個頻道來這樣做。

在你MainViewController.h設置它像這樣:

#import <UIKit/UIKit.h> 

//This is our delegate 
@protocol TalkToParentDelegate <NSObject> 

//This is our delegate method 
- (void)helloParent; 
@end 

@interface MainViewController : UIViewController <TalkToParentDelegate> 
... 
.. 
@end 

在你MainViewController.m請確保添加的委託方法。

- (void)helloParent { 
    NSLog(@"Hello child, let me do something here"); 
} 

在你ChildViewController.h設置它像這樣:現在

#import <UIKit/UIKit.h> 

//Add header of class where protocol was defined 
#import "MainViewController.h" 

@interface ChildViewController : UIViewController 

//Create a property we can set to reference back to our parent 
@property (strong, nonatomic) id <TalkToParentDelegate> delegate; 

@end 

,在你MainViewController.m,只要您出示您的ChildViewController做到這一點:

ChildViewController *child = [[ChildViewController alloc]...]; 

//Set the delegate reference to parent 
child.delegate = self; 

//present the view 

最後但並非至少,不,當你在你的孩子,你可以調用你的父母(MainViewController)像這樣的方法:

[self.delegate helloParent];


因此,您可以使用兩種方法。

但是我想說明一下,你可以一起使用這些。假設你有一個UINavigationController,但仍然需要一個孩子與其父母交談,你可以設置一個委託,這樣纔有可能。

  • 希望這會有所幫助。
+0

真棒!非常清楚的解釋。我很感激你的時間。有一件事,什麼是'nav1.view'?在我回到我的代碼後,我將在明天開始遵循你的解釋。 – Sobiaholic 2012-08-08 21:00:21

+0

我很抱歉,應該是「nav1」而不是「nav1.view」。 :P我將帖子固定爲類似更改。很高興我能夠幫助! – random 2012-08-08 21:14:22

+0

遵循你的指導。雖然,您可以從界面構建器中完成大部分工作,但編程實現它真的很棒。感謝您的時間和答案科裏:) – Sobiaholic 2012-08-11 02:32:22