对于Iphone,如何从当前加载的视图引用根视图控制器,因此可以替换/切换视图 [英] For Iphone how to reference root view controller from currently loaded view, so view can be replaced/switched
问题描述
我正在尝试用新视图切换当前加载的视图.
I'm trying to switch a currently loaded view with a new one.
我有3个xib文件和3套ViewControllerFiles.
I have 3 xib files and 3 sets of ViewControllerFiles.
RootViewController正确加载,并且正确加载HomeViewController.
RootViewController loads correctly, and correctly loads HomeViewController.
当前出错的行是这个
[self.rootViewController.view insertSubview:gameController.view atIndex:1];
**error: request for member 'view' in something not a structure or a union**
如果有人能指出我正确的方向,那就太好了.我了解如何从根控制器切换视图,但是我想从子视图交换/添加视图.如果说得通?
If anyone could point me in the right direction that would be great. I understand how I could switch views all from the root controller but I want to swap/add views from a subview. If that makes sense?
这是根控制器头和实现:
Here is the root controller header and implementation:
#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
这是正确加载的主视图文件:
And here is the home view files which load correctly:
#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
推荐答案
幸运的是,这很简单-您忘记了包含GameViewController头文件.因此,不知道您的GameViewController实例的view属性是什么.
This is luckily an easy fix - you forgot to include the GameViewController header file. Hence it has no idea what the view property of your GameViewController instance is.
这篇关于对于Iphone,如何从当前加载的视图引用根视图控制器,因此可以替换/切换视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!