对于 Iphone,如何从当前加载的视图中引用根视图控制器,以便可以替换/切换视图
Posted
技术标签:
【中文标题】对于 Iphone,如何从当前加载的视图中引用根视图控制器,以便可以替换/切换视图【英文标题】:For Iphone how to reference root view controller from currently loaded view, so view can be replaced/switched 【发布时间】:2009-10-29 17:51:25 【问题描述】:我正在尝试用新的视图切换当前加载的视图。
我有 3 个 xib 文件和 3 组 ViewControllerFiles。
RootViewController 正确加载,并且正确加载 HomeViewController。
当前出错的行是这个
[self.rootViewController.view insertSubview:gameController.view atIndex:1];
**error: request for member 'view' in something not a structure or a union**
如果有人能指出我正确的方向,那就太好了。我了解如何从根控制器切换视图,但我想从子视图交换/添加视图。有道理吗?
这里是根控制器头和实现:
#import <UIKit/UIKit.h>
@class HomeViewController;
@class GameViewController;
@interface RootViewController : UIViewController
HomeViewController *homeViewController;
GameViewController *gameViewController;
@property (retain, nonatomic) HomeViewController *homeViewController;
@property (retain, nonatomic) GameViewController *gameViewController;
- (IBAction)showHome:(id)sender;
- (IBAction)showGame:(id)sender;
@end
实现文件:
#import "RootViewController.h"
#import "HomeViewController.h"
#import "GameViewController.h"
@implementation RootViewController
@synthesize homeViewController;
@synthesize gameViewController;
- (void)viewDidLoad
homeViewController.rootViewController = self;
HomeViewController *homeController = [[HomeViewController alloc] initWithNibName:@"HomeView" bundle:nil];
self.homeViewController = homeController;
[self.view insertSubview:homeController.view atIndex:0];
[homeController release];
[super viewDidLoad];
- (void)didReceiveMemoryWarning
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
if (self.homeViewController.view.superview == nil)
self.homeViewController = nil;
else
self.gameViewController = nil;
- (void)viewDidUnload
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
- (void)dealloc
[homeViewController release];
[gameViewController release];
[super dealloc];
@end
这是正确加载的主视图文件:
#import <UIKit/UIKit.h>
@class RootViewController;
@interface HomeViewController : UIViewController
//GameViewController *gameViewController;
RootViewController *rootViewController;
//@property (retain, nonatomic) GameViewController *gameViewController;
@property (nonatomic, assign) RootViewController *rootViewController;
- (IBAction)showGame:(id)sender;
@end
实现文件:
#import "HomeViewController.h"
#import "RootViewController.h"
@implementation HomeViewController;
@synthesize rootViewController;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
[super viewDidLoad];
- (IBAction)showGame:(id)sender
GameViewController *gameController = [[GameViewController alloc] initWithNibName:@"GameView" bundle:nil];
self.rootViewController = gameController;
[self.rootViewController.view insertSubview:gameController.view atIndex:1];
[gameController release];
- (void)didReceiveMemoryWarning
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
- (void)viewDidUnload
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
- (void)dealloc
[super dealloc];
@end
【问题讨论】:
在哪里 [self.rootViewController.view insertSubview:gameController.view atIndex:1];被叫了? 在 HomeViewController 的实现文件中 - (IBAction)showGame:(id)sender GameViewController *gameController = [[GameViewController alloc] initWithNibName:@"GameView" bundle:nil]; self.rootViewController = 游戏控制器; [self.rootViewController.view insertSubview:gameController.view atIndex:1]; [游戏控制器发布]; 【参考方案1】:我对你的评论感到困惑。这是附加代码sn-p:
- (IBAction)showGame:(id)sender
GameViewController *gameController = [[GameViewController alloc] initWithNibName:@"GameView" bundle:nil];
self.rootViewController = gameController;
[self.rootViewController.view insertSubview:gameController.view atIndex:1];
[gameController release];
因此,您将 HomeViewController 的 rootViewController 设置为等于 gameController。然后将 gameController 视图插入到 rootViewController 的视图中。但是,gameController 是 rootViewController。我认为你这里有一个逻辑错误。
【讨论】:
【参考方案2】:幸运的是,这是一个简单的修复 - 您忘记包含 GameViewController 头文件。因此它不知道 GameViewController 实例的 view 属性是什么。
【讨论】:
【参考方案3】:我认为这是你的问题:
- (void)viewDidLoad
homeViewController.rootViewController = self;
HomeViewController *homeController = [[HomeViewController alloc] initWithNibName:@"HomeView" bundle:nil];
self.homeViewController = homeController;
在初始化homeViewController
之前,您将rootViewController
分配给homeViewController
。那时'self.homeViewController'只是空内存。
【讨论】:
以上是关于对于 Iphone,如何从当前加载的视图中引用根视图控制器,以便可以替换/切换视图的主要内容,如果未能解决你的问题,请参考以下文章
如何在 iPhone 上保存对特定照片的引用以便在没有选择器的情况下加载?
如何捕获当前视图屏幕截图并在代码中重用? (iPhone SDK)