我有一個UIViewController「NavigationViewController」,它是作爲我的「FirstViewController」的子視圖添加的,它佔用了整個屏幕(即類似於iPad應用程序中的Flipboard Navigation控制器)。根據用戶的選擇添加和刪除許多視圖。我希望能夠從添加的「多個視圖」中將視圖控制器推送到「FirstViewController」navigationController。在這種情況下,「FeaturedViewController」是可以選擇的許多視圖之一。它繼承了定義委託協議的ContentViewController。多對一的UIViewController通信。導航控制器推送視圖。委託或NSNotificationCenter或類方法?
TL; DR我想要訪問第一個視圖中的導航控制器,以從添加的子視圖「FeaturedViewController」中推送視圖。
這裏是一個可視化表示:
這裏是我的代碼:
這是我當前的嘗試不起作用。請注意,我花了一些代碼了關於該「導航器」
/* First View Controller */
#import <UIKit/UIKit.h>
#import "ContentViewController.h"
@interface ViewController : UIViewController <BaseViewDelegate, ContentViewDelegate>
{
IBOutlet UINavigationController *navigationController;
ContentViewController *contentView;
}
@property (strong, nonatomic) IBOutlet UINavigationController *navigationController;
@property (strong, nonatomic) ContentViewController *contentView;
---------------------------------------------------------
#import "ViewController.h"
#import "NavigatorViewController.h"
#import "BinViewController.h"
@implementation ViewController
@synthesize navigationController, contentView;
static NSArray *viewArray = nil;
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.navigationController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[self.view addSubview:navigationController.view];
// Navigation View is used to navigate throughout the entire application
NavigatorViewController *navController = [[NavigatorViewController alloc] init];
contentView = [[ContentViewController alloc] init];
contentView.delegate = self;
// Add the views to the array (using ARC)
viewArray = [NSArray arrayWithObjects:navController, contentView, nil];
}
-(void)displayNavigator
{
// Get the right view controller
UIViewController *viewController = [viewArray objectAtIndex:0];
// Add the subview to the view
[self.view addSubview:viewController.view];
}
-(void)pushViewController:(UIViewController *)viewController
{
NSLog(@"First View Push View Controller called");
[self.navigationController pushViewController:viewController animated:YES];
}
@end
/* Content View Controller */
@class ContentViewController;
@protocol ContentViewDelegate <NSObject>
-(void)pushViewController:(UIViewController *)viewController;
@end
@interface ContentViewController : UIViewController
{
__weak id <ContentViewDelegate> delegate;
}
@property (weak) __weak id <ContentViewDelegate> delegate;
- (void)pushView:(UIViewController *)viewController;
@end
---------------------------------------------------------
#import "ContentViewController.h"
#import "BinViewController.h"
@implementation ContentViewController
@synthesize delegate;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)pushView:(UIViewController *)viewController
{
NSLOG(@"Push View from ContentViewController");
if ([delegate respondsToSelector:@selector(pushViewController)])
[delegate pushViewController];
}
@end
/* View that is added to the navigator view (it inherits ContentViewController where the delegate protocol is defined)*/
#import <UIKit/UIKit.h>
#import "ContentViewController.h"
@interface FeaturedViewController : ContentViewController <CustomPagingDelegate>
@end
---------------------------------------------------------
#import "FeaturedViewController.h"
#import "BinViewController.h"
@implementation FeaturedViewController
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void) touchUpInsideItemAtIndex:(NSUInteger)itemIndex
{
// [[[[[self view] superview] superview] superview] removeFromSuperview];
NSLOG(@"Touch up inside from featured view");
BinViewController *binViewController = [[BinViewController alloc] init];
[self pushView:binViewController];
}
@end
謝謝。明白了與NSNotificationCenter。 – morcutt 2011-12-24 09:40:22