【问题标题】:Blank screen on device; simulator working fine设备上的黑屏;模拟器工作正常
【发布时间】:2011-06-12 19:42:08
【问题描述】:

我正在编写一个带有导航控制器的 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;

【问题讨论】:

    标签: iphone ios


    【解决方案1】:

    这里有几个问题。首先,您的 AppDelegate 标头应为:

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

    然后,在实现中,将根添加到导航控制器,将导航控制器添加到窗口,如下所示:

    #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;
    }
    

    【讨论】:

    • 谢谢。这清理了我的代码,对此我非常感激。但是,它并没有清除我设备上显示的黑屏,顶部只有一个状态栏。
    • 那我建议你把rootViewController的代码贴出来。具体来说,viewDidLoad 和 viewWillAppear(如果适用)。问题可能就在那里。
    • 对原帖进行了修改。怀疑他们是否会有所帮助。
    • 试试self.navigationItem.title = @"Main Menu"。只是为了好玩,还可以尝试更改背景颜色 (self.view.backgroundColor = [UIColor redColor];) 看看会发生什么。我的猜测是 IB 中没有正确连接。
    • self.navigationItem.title = @"Main Menu" 不创建任何更改,并且 self.view.backgroundColor = [UIColor redColor];不会产生任何变化。也就是说,模拟器中的一切看起来都很正常(不是红色),但是设备有一个黑色的屏幕和一个白色的状态栏。我的所有链接在界面生成器中都是一致的。 aboutCell、configureCell 和 newsCell 链接到表视图单元格。视图链接到视图。 tableView 链接到表视图。表视图的数据源和委托链接到文件的所有者。这是正确的,不是吗?
    【解决方案2】:

    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 文件有问题。解开所有东西,然后重新连接,看看问题是否仍然存在。如果是这样,请检查以确保所有内容都正确命名。祝你好运!

    【讨论】:

    • 好的,我重建了 IB 文件(RootViewController 和 MainWindow),但没有成功。我看不出 IB 会如何导致设备独有的问题,而不是模拟器的问题。无论如何,我只是从头开始重建整个项目,现在一切都可以在模拟器和设备上运行。没有学到任何东西,但它有效。
    • 奇怪,一定是非常罕见的东西。有时,“清理”项目(产品 > 清理)将有助于解决类似的问题。很抱歉,它让你这么头疼,但我很高兴它现在可以工作了。
    猜你喜欢
    • 2018-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多