2011-06-12 95 views
4

我正在用導航控制器編寫iOS應用程序。當我在模擬器上打開它時,它運行良好。當我在設備上運行它時,狀態欄下方會顯示一個空白屏幕。另外我無法弄清楚我的RootViewController被設置爲默認視圖(我懷疑這是我的問題的根源)。設備上的空白屏幕;模擬器正常工作

@class RootViewController; 

@interface MyAppDelegate : NSObject <UIApplicationDelegate> { 
    UIWindow *window; 
    RootViewController *viewController; 

    UINavigationController *navigationController; 
} 

@property (nonatomic, retain) IBOutlet UIWindow *window; 
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController; 
@property (nonatomic, retain) IBOutlet RootViewController *viewController; 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 

    // Override point for customization after application launch. 

    // Set the navigation controller as the window's root view controller and display. 
    self.window.rootViewController = self.navigationController; 
    [self.window makeKeyAndVisible]; 

    // ... 

    return YES; 
} 

RootViewController.m

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.title = @"Main Menu"; 
} 

否viewWillAppear中,viewDidAppear等

顯示0個元素的表。

- (UITableViewCell *)tableView:(UITableView *)tv 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    tv.backgroundColor = [UIColor whiteColor]; 

    UITableViewCell *cell; 
    if (indexPath.row == 0) 
     cell = newsCell; 
    else if (indexPath.row == 1) 
     cell = configureCell; 
    else if (indexPath.row == 2) 
     cell = aboutCell; 

    return cell; 
} 

- (NSInteger)tableView:(UITableView *)tv numberOfRowsInSection:(NSInteger)section 
{ 
    return 0; 
} 

#pragma mark UITableViewDelegate Methods 


- (CGFloat)tableView:(UITableView *)tv 
heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    return 84; 
} 

- (void) tableView:(UITableView *)tv 
didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (0 == indexPath.row) 
    { 
    } 
    else if (1 == indexPath.row) 
    { 
    } 
    else if (2 == indexPath.row) 
    { 
    } 
} 


#pragma mark - 
#pragma mark Memory management 

- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Relinquish ownership any cached data, images, etc that aren't in use. 
} 

- (void)viewDidUnload { 
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand. 
    // For example: self.myOutlet = nil; 
} 


- (void)dealloc { 
    [super dealloc]; 
    [tableView release]; 
    [newsCell release]; 
    [configureCell release]; 
    [aboutCell release]; 
} 

RootViewController.h

@interface RootViewController : UIViewController 
<UITableViewDataSource, UITableViewDelegate> 
{ 
    UITableView *tableView; 
    IBOutlet UIView *displaySplashScreen; 
    IBOutlet UITableViewCell *newsCell, *configureCell, *aboutCell; 
} 

@property (nonatomic, retain) IBOutlet UITableView *tableView; 
+0

+1雙根 – PengOne 2011-06-12 20:12:39

回答

3

這裏有幾個問題。首先,你的AppDelegate頭應改爲:

@class RootViewController; 

@interface MyAppDelegate : NSObject <UIApplicationDelegate> { 
    UIWindow *window; 
    RootViewController *rootViewController; 
} 
@property (nonatomic, retain) IBOutlet UIWindow *window; 

@end 

然後,在執行中,根添加到導航控制器和navController到窗口是這樣的:

#import "RootViewController.h" 

@implementation MyAppDelegate 

@synthesize window; 

- (BOOL)application:(UIApplication *)application 
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  

    rootViewController = [[RootViewController alloc] init]; 
    UINavigationController *navController = [[UINavigationController alloc] 
              initWithRootViewController:rootViewController]; 

    [window addSubview:[navController view]]; 
    [self.window makeKeyAndVisible]; 

    return YES; 
} 
+0

謝謝。這清理了我的代碼,對此我非常感激。但是,它並未清除設備上顯示的黑屏,只有狀態欄在頂部。 – Scott 2011-06-12 20:23:26

+0

然後我建議你發佈rootViewController的代碼。具體來說,viewDidLoad和viewWillAppear(如果適用)。問題可能在那裏。 – PengOne 2011-06-12 20:38:51

+0

對原始帖子進行修改。懷疑他們會有任何幫助。 – Scott 2011-06-12 21:21:09

1

PengOne的答案是正確的,但我會做一個小小的改變。做到這一點:

#import "RootViewController.h" 

@implementation MyAppDelegate 

@synthesize window; 

- (BOOL)application:(UIApplication *)application 
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    rootViewcontroller = [[RootViewController alloc] init]; 
    UINavigationController *navController = [[UINavigationController alloc] 
             initWithRootViewController:rootViewController]; 

    self.window.rootViewController = navController; 

    [self.window makeKeyAndVisible]; 
    return YES; 
} 

這是一個更好的方式來顯示導航控制器。正如PengOne所說,它必須是Interface Builder文件的問題。解除一切,然後重新連接,看看問題是否存在。如果確實如此,請檢查以確保所有內容都正確命名。祝你好運!

+0

好的,我重建了IB文件(RootViewController和MainWindow),但沒有成功。我沒有看到IB如何導致設備獨有的問題,而不是模擬器。無論如何,我只是從零開始重建整個項目,現在一切都可以在模擬器和設備上運行。沒有學到什麼,但它有效。 – Scott 2011-06-13 01:11:59

+0

奇怪,一定是非常罕見的東西。有時候,「清理」項目(產品>清潔)將有助於解決這樣的問題。對不起,它讓你頭痛,但我很高興它現在有效。 – 2011-06-13 03:39:52

相關問題