1

我对 UIViewController 类进行了子类化,如下面的代码所示。下面编码的视图控制器由 Storyboard 在 TabBarController 中实例化,但未显示其视图(我使用 Interface Builder 添加的标签、工具栏)。唯一显示的项目是 TabBarControllers 选项卡栏。

。H:

@interface FinstatViewController : UIViewController <SplitViewBarButtonItemPresenter,UISplitViewControllerDelegate>
@property (nonatomic, strong) UIBarButtonItem *splitViewBarButtonItem;
@property (weak, nonatomic) IBOutlet UINavigationBar *toolbar;
@end

米:

#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

我做错了什么?

谢谢!

4

1 回答 1

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

于 2012-02-20T20:34:32.880 回答