未显示子类 UIViewController
Posted
技术标签:
【中文标题】未显示子类 UIViewController【英文标题】:Subclassed UIViewController not shown 【发布时间】:2012-02-20 16:16:25 【问题描述】:我对 UIViewController 类进行了子类化,如下面的代码所示。 下面编码的视图控制器由 Storyboard 在 TabBarController 中实例化,但未显示其视图(我使用 Interface Builder 添加的标签、工具栏)。唯一显示的项目是 TabBarControllers 选项卡栏。
.h:
@interface FinstatViewController : UIViewController <SplitViewBarButtonItemPresenter,UISplitViewControllerDelegate>
@property (nonatomic, strong) UIBarButtonItem *splitViewBarButtonItem;
@property (weak, nonatomic) IBOutlet UINavigationBar *toolbar;
@end
.m:
#import "FinstatViewController.h"
@interface FinstatViewController ()
@end
@implementation FinstatViewController
@synthesize splitViewBarButtonItem = _splitViewBarButtonItem; // implementation of SplitViewBarButtonItemPresenter protocol
@synthesize toolbar = _toolbar; // to put splitViewBarButtonItem in
- (void)setSplitViewBarButtonItem:(UIBarButtonItem *)splitViewBarButtonItem
if (splitViewBarButtonItem != _splitViewBarButtonItem)
NSMutableArray *toolbarItems = [self.toolbar.items mutableCopy];
if (_splitViewBarButtonItem) [toolbarItems removeObject:_splitViewBarButtonItem];
if (splitViewBarButtonItem) [toolbarItems insertObject:splitViewBarButtonItem atIndex:0];
self.toolbar.items = toolbarItems;
_splitViewBarButtonItem = splitViewBarButtonItem;
- (void)awakeFromNib // always try to be the split view's delegate
[super awakeFromNib];
self.splitViewController.delegate = self;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
// Custom initialization
return self;
- (void)loadView
// If you create your views manually, you MUST override this method and use it to create your views.
// If you use Interface Builder to create your views, then you must NOT override this method.
- (void)viewDidLoad
[super viewDidLoad];
//self.splitViewController.delegate=self;
// Do any additional setup after loading the view, typically from a nib.
- (void)viewDidUnload
[self setToolbar:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
return YES; // (interfaceOrientation == UIInterfaceOrientationPortrait);
@end
我做错了什么?
谢谢!
【问题讨论】:
【参考方案1】:阅读该方法的注释,然后彻底删除该方法:
- (void)loadView
// If you create your views manually, you MUST override this method and use it to create your views.
// If you use Interface Builder to create your views, then you must NOT override this method.
您根本没有视图(即 self.view),因为您没有创建视图,因此您的 viewController 显示为空。当您想在代码中创建视图时,您可以使用- loadView
。但是你使用故事板来创建你的视图,所以你不能使用- loadView
这不是你的错,怪苹果。 Xcode 4.3 UIViewController 模板已更新。 当您取消选中“使用 XIB 用于用户界面”时,Apple 的一些聪明人认为您想在代码中创建您的视图。他可能完全错过了故事板公告。
删除该部分后,请考虑在http://bugreport.apple.com/ 提交错误
编辑:此错误已在 Xcode 4.3.2 中修复。该模板不再包含- (void)loadView
【讨论】:
谢谢你,马蒂亚斯。你的回答对我帮助很大!以上是关于未显示子类 UIViewController的主要内容,如果未能解决你的问题,请参考以下文章
iphone UIViewController 基子类未在 UINavigationController 中显示视图