我已经拼命地花了2个小时来查找我的代码中的一个错误,这个错误导致我的SplitViewController没有显示出来。奇怪的是,没有显示任何编译错误,并且所有的链接和依赖关系似乎都是正确的。
第一次加载时屏幕上没有任何东西。但是当我旋转设备时,我看到了正确的视图。但是在启动时没有任何视图,即使所有的视图都已连接。
我只是让其中的一部分工作了......我通过调试器发现,第一个视图控制器在启动时没有被加载。因此,分割视图的视图控制器数组没有填充初始视图控制器... 我该如何解决这个问题?
我首先检查的是我的AppDelegate:
第一次加载时屏幕上没有任何东西。但是当我旋转设备时,我看到了正确的视图。但是在启动时没有任何视图,即使所有的视图都已连接。
我只是让其中的一部分工作了......我通过调试器发现,第一个视图控制器在启动时没有被加载。因此,分割视图的视图控制器数组没有填充初始视图控制器... 我该如何解决这个问题?
我首先检查的是我的AppDelegate:
#import "AppDelegate.h"
#import "RootViewController.h"
@implementation AppDelegate
@synthesize window = _window;
@synthesize splitViewController = _splitViewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSLog(@"%s %d", __PRETTY_FUNCTION__, __LINE__);
[[self window] addSubview:self.splitViewController.view];
[self.window makeKeyAndVisible];
return YES;
}
@end
第一个视图控制器
#import "FirstViewController.h"
@implementation FirstViewController
@synthesize toolbar;
#pragma mark -
#pragma mark View Lifecycle
- (void)viewDidUnload
{
[super viewDidUnload];
self.toolbar = nil;
}
#pragma mark -
#pragma mark Managing the Popover
- (void)showRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem
{
NSMutableArray *itemsArray = [toolbar.items mutableCopy];
[itemsArray insertObject:barButtonItem atIndex:0];
[toolbar setItems:itemsArray animated:NO];
}
- (void)invalidateRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem
{
NSMutableArray *itemsArray = [toolbar.items mutableCopy];
[itemsArray removeObject:barButtonItem];
[toolbar setItems:itemsArray animated:NO];
}
#pragma mark -
#pragma mark Rotation support
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
@end
我检查的第二部分是我的RootViewController。问题是,当你点击一个tableView条目时,应该加载并显示一个新的视图控制器。因此,我不在我的应用程序委托中实例化VCs,而是在“did select row”表视图委托方法中实例化。
这是rootviewcontroller:
#import "RootViewController.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
@implementation RootViewController
@synthesize popoverController, splitViewController, rootPopoverButtonItem;
#pragma mark -
#pragma mark View Lifecycle
- (void)viewDidLoad
{
NSLog(@"%s %d", __PRETTY_FUNCTION__, __LINE__);
[super viewDidLoad];
self.contentSizeForViewInPopover = CGSizeMake(310.0, self.tableView.rowHeight * 2.0);
}
- (void)viewDidUnload
{
NSLog(@"%s %d", __PRETTY_FUNCTION__, __LINE__);
[super viewDidUnload];
self.splitViewController = nil;
self.rootPopoverButtonItem = nil;
}
#pragma mark -
#pragma mark Rotation support
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
NSLog(@"%s %d", __PRETTY_FUNCTION__, __LINE__);
return YES;
}
#pragma mark -
#pragma mark SplitViewController delegates
- (void)splitViewController:(UISplitViewController *)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)pc
{
NSLog(@"%s %d", __PRETTY_FUNCTION__, __LINE__);
barButtonItem.title = @"PolyCube Prototypes";
self.popoverController = pc;
self.rootPopoverButtonItem = barButtonItem;
UIViewController <SubstitutableDetailViewController> *detailViewController = [splitViewController.viewControllers objectAtIndex:1];
[detailViewController showRootPopoverButtonItem:rootPopoverButtonItem];
}
- (void)splitViewController:(UISplitViewController *)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
{
NSLog(@"%s %d", __PRETTY_FUNCTION__, __LINE__);
self.popoverController = nil;
self.rootPopoverButtonItem = nil;
UIViewController <SubstitutableDetailViewController> *detailViewController = [splitViewController.viewControllers objectAtIndex:1];
[detailViewController invalidateRootPopoverButtonItem:rootPopoverButtonItem];
}
#pragma mark -
#pragma mark Table View Data Source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(@"%s %d", __PRETTY_FUNCTION__, __LINE__);
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%s %d", __PRETTY_FUNCTION__, __LINE__);
static NSString *CellIdentifier = @"RootViewControllerIdentifier";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if (indexPath.row == 0) {
cell.textLabel.text = @"PolyCube 1";
}
else {
cell.textLabel.text = @"PolyCube 2";
}
return cell;
}
#pragma mark -
#pragma mark - Table View Selection
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%s %d", __PRETTY_FUNCTION__, __LINE__);
NSUInteger row = indexPath.row;
UIViewController <SubstitutableDetailViewController> *detailViewController = nil;
if (row == 0) {
FirstViewController *newDetailViewController = [[FirstViewController alloc] initWithNibName:@"FirstDetailView" bundle:nil];
detailViewController = newDetailViewController;
}
if (row == 1) {
SecondViewController *newDetailViewController = [[SecondViewController alloc] initWithNibName:@"SecondDetailView" bundle:nil];
detailViewController = newDetailViewController;
}
NSArray *vcs = [[NSArray alloc] initWithObjects:self.navigationController, detailViewController, nil];
splitViewController.viewControllers = vcs;
if (popoverController != nil) {
[popoverController dismissPopoverAnimated:YES];
}
if (rootPopoverButtonItem != nil) {
[detailViewController showRootPopoverButtonItem:self.rootPopoverButtonItem];
}
}
@end
我希望能得到帮助,因为我不知道该去哪里寻找答案。我已经检查了所有的IB连接,但是似乎没有问题。然而屏幕依然是白色的。
非常感谢! Sebastian