我陷入了教程,试图用工具栏切换视图。我的代码构建正常,我发出一些警报,以检查工具栏是否正常工作,它是。但是我插入的子视图根本就没有显示出来。有人可以帮忙吗?
这是我的主控制器代码..
MainViewController.h是
#import @class FlavourPickerView; @class ShakeView; @class ResultsView; @interface MainViewController : UIViewController { IBOutlet FlavourPickerView *flavourPickerView; IBOutlet ShakeView *shakeView; IBOutlet ResultsView *resultsView; } @property (retain,nonatomic) FlavourPickerView *flavourPickerView; @property (retain,nonatomic) ShakeView *shakeView; @property (retain,nonatomic) ResultsView *resultsView; -(IBAction)loadFlavourPickerView:(id)sender; -(IBAction)loadShakeView:(id)sender; -(IBAction)loadResultsView:(id)sender; -(void)clearView; @end
这是MainViewController.m
#import "MainViewController.h" #import "FlavourPickerView.h" #import "ShakeView.h" #import "ResultsView.h" @implementation MainViewController @synthesize flavourPickerView; @synthesize shakeView; @synthesize resultsView; -(IBAction)loadFlavourPickerView:(id)sender{ UIAlertView *alertDialog; alertDialog = [[UIAlertView alloc] initWithTitle:@"Flavour selected" message:@"Seems to work" delegate:nil cancelButtonTitle:@"OK!" otherButtonTitles:nil]; [alertDialog show]; [alertDialog release]; [self clearView]; [self.view insertSubview:flavourPickerView.view atIndex:10]; } -(IBAction)loadShakeView:(id)sender{ UIAlertView *alertDialog; alertDialog = [[UIAlertView alloc] initWithTitle:@"Shaker selected" message:@"Seems to work" delegate:nil cancelButtonTitle:@"OK!" otherButtonTitles:nil]; [alertDialog show]; [alertDialog release]; [self clearView]; [self.view insertSubview:shakeView.view atIndex:0]; } -(IBAction)loadResultsView:(id)sender{ UIAlertView *alertDialog; alertDialog = [[UIAlertView alloc] initWithTitle:@"Results selected" message:@"Seems to work" delegate:nil cancelButtonTitle:@"OK!" otherButtonTitles:nil]; [alertDialog show]; [alertDialog release]; [self clearView]; [self.view insertSubview:resultsView.view atIndex:0]; } -(void)clearView{ if(flavourPickerView.view.superview){ [flavourPickerView.view removeFromSuperview]; }else if(shakeView.view.superview){ [shakeView.view removeFromSuperview]; }else{ [resultsView.view removeFromSuperview]; } } // Implement viewDidLoad to do additional setup after loading the view, typically from a nib. - (void)viewDidLoad { [self loadFlavourPickerView:nil]; [super viewDidLoad]; } - (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 { [flavourPickerView release]; [shakeView release]; [resultsView release]; [super dealloc]; } @end
以下是其他一个视图的示例,从FlavourPickerView.h
开始#import @interface FlavourPickerView : UIViewController { } @end
和FlavourPickerView.m
#import "FlavourPickerView.h" @implementation FlavourPickerView - (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 { [super viewDidUnload]; // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; } - (void)dealloc { [super dealloc]; } @end
还有一个用于flavourPickerView的笔尖,我已将子视图添加到我的mainView中,其中Class设置为FlavourPickerView,nib名称为FlavourPickerView.xib。
答案 0 :(得分:1)
将视图添加到此UIViewController视图时,flavourPickerView
,shakeView
和resultsView
是否已正确初始化?
另一件事:避免调用UIViewController只是'XXXView',而是尝试'XXXViewController'