有兩種方法可以使用。
- 的UINavigationController
- 代表
從你的問題似乎是一個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,但仍然需要一個孩子與其父母交談,你可以設置一個委託,這樣纔有可能。
要顯示7個視圖,請查看'UINavigationController',或者甚至是'UIPageViewController'。這可能會有所幫助。 – matsr 2012-08-07 19:01:43
除了顯示的數據之外,7個視圖中的每一個都是相同的嗎?你使用什麼對象來顯示這些數據,文本視圖,Web視圖,還有其他的東西?另外,你是否希望能夠在所有視圖之間來回移動,或者只是從主視圖到其他7個視圖中的任何一個,然後再返回到主視圖? – rdelmar 2012-08-07 22:04:19
我還沒決定結構。這是一個演示項目,所以我想在旅途中學習和實施。主要是,用戶應該在視圖之間跳轉到另一個視圖,或者像你說的那樣來回跳動。正如matsr所建議的那樣,我開始使用'UINavigationController'。 – Sobiaholic 2012-08-08 20:55:20