2012-01-01 54 views
3

我拼命嘗試2h現在在我的代碼中找到一個錯誤,這是負責不顯示我已經實現的SplitViewController。奇怪的是,沒有顯示編譯錯誤,並且所有鏈接和依賴關係似乎都是正確的。SplitViewController不顯示

第一次加載時屏幕上沒有任何東西顯示出來。但是當我旋轉設備時,我看到了正確的視圖。但是,即使所有視圖都已連接,但在啓動時根本沒有視圖。

我剛剛得到它的一部分工作...我發現與調試器,第一個視圖控制器未啓動時加載。所以視圖控制器陣列的拆分視圖它沒有填充一個初始視圖控制器...我該如何解決這個問題?

我檢查的第一部分是我的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 Entry時,一個新的視圖控制器應該被加載並顯示出來。所以我沒有在我的應用程序委託中實例化VC,而是在「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連接......我認爲沒有錯。但仍然只是一個白色的屏幕。

謝謝你這麼多 塞巴斯蒂安

+0

要在任何地方加載NIB文件嗎? – craig1231 2012-01-01 21:06:42

+0

以及我認爲是這樣的......我剛剛發現,splitviewcontroller.viewcontrollers數組最初沒有填充時加載....我該如何解決? 正如你所看到的,當我點擊一個tableview條目時,我更新了這個數組。但我記錄數組,同時調試顯示在NSLog中的對象....並有消息:NULL 所以我認爲應該有一種可能性,以「navcontroller和firstviewcontroller」初始化該數組... ...但我不知道如何實際 – konturgestaltung 2012-01-01 21:11:54

回答

0

這是正常現象。如果您沒有設置UISplitViewController的委託,那麼在縱向模式下主視圖將不可見。您的主視圖從不加載,您看到的白色屏幕是您的詳細視圖。

所以,你需要實現自己的UISplitViewController(創建一個新的UIViewController並將其更改爲UISplitViewControlle。不要忘記它的故事板中分配給您的拆分視圖)

另外在界面,使之實現並在viewDidLoad方法中將委託設置爲self。

該協議將告訴放置主表的位置。你想在分裂模式下分割窗口嗎?或者也許你想要一個按鈕(通常在一個tabbar),當你按下它的主窗口彈出窗口?

舉例來說,實現這個傢伙有主視圖始終可見:

接口:

#import <UIKit/UIKit.h> 

@interface SplitViewController : UISplitViewController <UISplitViewControllerDelegate> 

@end 

實現

#import "SplitViewController.h" 

@interface SplitViewController() 

@end 

@implementation SplitViewController 


- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self setDelegate:self]; 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return YES; 
} 

-(BOOL)splitViewController:(UISplitViewController *)svc shouldHideViewController:(UIViewController *)vc inOrientation:(UIInterfaceOrientation)orientation{ 
    return NO; 
} 


@end 

如果你不喜歡實現自己的UISplitViewController只是爲了處理委託,您還可以讓任何主控制器或細節控制器實現它並將其分配到故事板中(嘗試按Ctrl並拖動從分割視圖到任何控制器並將其設置爲委託,不要忘記首先實現協議)

相關問題