加载 .xib 文件并显示它
我试图在按下按钮时在应用程序中显示“关于页面”,但是当我按下 UIButton 时,会调用 NSLog(@"Button Pressed"),但视图不会加载。这是我的代码:
- (IBAction)pushAbout {
NSLog(@"Button pressed");
AboutView * aboutView = [[AboutView alloc] initWithNibName:@"AboutView" bundle:nil];
[self.navigationController pushViewController:aboutView animated:YES];
[aboutView release];
}
我在示例中看到了此代码,但它是在 UITableViewController 而不是普通 UIView 控制器中调用的。
类文件“AboutView”被创建为 UIViewController 子类。它们被创建并保持不变
I'm trying to display an "About Page" in my application when pressing a button, but when I press the UIButton, the NSLog(@"Button pressed") is called, but the view won't load. Here is my code:
- (IBAction)pushAbout {
NSLog(@"Button pressed");
AboutView * aboutView = [[AboutView alloc] initWithNibName:@"AboutView" bundle:nil];
[self.navigationController pushViewController:aboutView animated:YES];
[aboutView release];
}
I've seen this code in an example, but it was called in a UITableViewController instead of an ordianry UIView controller.
The class files "AboutView" were created as UIViewController subclass. They were created and left untouched
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
猜测:您的视图不在 UINavigationController 中,因此
实际上是
nil
,并且什么也没有发生。解决方案是将主视图放置在导航控制器中,并将导航控制器的视图添加到应用程序的主窗口中。
执行此操作的简单方法是:
在应用程序委托中,更改
为
“或”类似。请注意,这只是为了让您开始,也许不是最好的方法。 (我假设您刚刚在 Xcode 中创建了一个基于视图的应用程序。)
A guess: Your view is not in a UINavigationController, hence
is actually
nil
, and nothing happens.The solution would be to place the main view in a nav controller, and adding the nav controller's view to your application's main window.
A simple way of doing this:
In your app delegate, change
to
Or similar. Note that this is just to get you started, and maybe not the nicest way of doing it. (I assumed you've just created a view-based application in xcode.)
是的,您没有设置 UINavigationController。
添加
到您的操作中,您将看到它转储(空)。
但是,如果您输入以下代码,AboutView.xib 可以正常工作:
而不是
显示视图。在您的压缩示例中,AboutView.xib 不包含标签,因此不要怀疑它是否是一个白色页面。
使用来关闭所呈现的模式视图
您可以通过在 AboutViewController 中
。为了获得 UINavigationController 我建议使用基于导航的应用程序模板创建一个应用程序。
Yes you don't have a UINavigationController set.
Add
to your action and you'll see that it dumps (null).
However, the AboutView.xib works fine if you enter this code:
instead of
The view will show up. In you zipped example the AboutView.xib didn't contain a label, so don't wonder if it turns out to be a white page.
You can dismiss the presented modal view by using
in your AboutViewController.
To get your hands on a UINavigationController I suggest creating an app with the Navigation-Based Application template.