加载新视图在iPhone中显示空白屏幕

时间:2012-08-22 04:22:00

标签: iphone objective-c xcode ipad

请注意我正在尝试学习iphone,对于这个问题,我无法找到任何解决方案。

我创建了一个名为“加载”的空视图。我在其Loading.xib中填充了一些图像。现在这是我正在使用的代码

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    Loading *firstView = [[Loading alloc]init];


    self.navigationController = [[UINavigationController alloc]init]; 
    self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
    [self.navigationController pushViewController:firstView animated:YES];
    // Override point for customization after application launch.
    [self.window addSubview:self.navigationController.view];
    [window makeKeyAndVisible];
    return YES;
}

问题是,当我运行代码时,它只显示空白屏幕。

如何加载加载屏幕。欢迎任何推荐的教程链接

@implementation Loading

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

最好的问候

5 个答案:

答案 0 :(得分:1)

- > “Loading.xib”< -

你永远不会加载它,所以除非“加载”的init:选择器的实例做了一些奇特的东西,否则这个视图是空的。

编辑:“你能解释一下我怎么称呼这个?”

覆盖init ... *

- (id)init
{
    if ((self = [super initWithNibName:@"SomeNib" bundle:nil])) {
        // ...
    }

    return self;
}

* 如果所有者和笔尖在IB中不匹配,您可以A)在IB中正确创建合理性或B)在代码中手动创建:

更新init:

[super initWithNibName:nil bundle:nil]

控制装置:

- (void)loadView
{
    [super loadView];

    UINib *nib = [UINib nibWithNibName:@"SomeNib" bundle:nil];
    [nib instantiateWithOwner:self options:nil];
}

答案 1 :(得分:1)

首先,Loading是一个视图而不是视图控制器。你不应该用导航控制器推它。

其次,将导航控制器的视图添加到窗口不是一个好主意。

请检查此thread,详细了解UIViewUIViewController

相反,您可能想要创建一个LoadingViewController,并根据需要更改其视图(XIB文件),然后使用这样的代码

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
LoadingViewController *lvc= [[LoadingViewController alloc]init];

self.navigationController = [[UINavigationController alloc] initWithRootViewController:lvc]; 
self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
// Override point for customization after application launch.
self.window.rootViewController = self.navigationController;
[window makeKeyAndVisible];
return YES;
}

答案 2 :(得分:0)

您需要从其nib文件加载视图。这是一个例子:

Loading *firstView = [[Loading alloc] initWithNibName:@"Loading" bundle:nil];

此外,当您分配UINavigationController时,您也可以设置其根视图控制器属性,而不必像这样推送视图:

UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController: firstView];

答案 3 :(得分:0)

您必须更改代码,

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    Loading *firstView = [[Loading alloc] initWithNibName:@"Loading" bundle:nil];
    self.navigationController = [[UINavigationController alloc]init]; 
    self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
    [self.window addSubview:self.navigationController.view];
    [window makeKeyAndVisible];
    [self.navigationController pushViewController:firstView animated:YES];
    return YES;
}

希望对你有所帮助。

答案 4 :(得分:0)

如果有人发现了这个帖子,但仍然无法弄清楚。试试这个。我忘了为该视图控制器添加目标成员资格。

enter image description here

相关问题