2011-06-12 92 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

相关问题