加载 .xib 文件并显示它

发布于 2024-11-05 07:34:29 字数 491 浏览 3 评论 0原文

我试图在按下按钮时在应用程序中显示“关于页面”,但是当我按下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尘世孤行 2024-11-12 07:34:29

猜测:您的视图不在 UINavigationController 中,因此

self.navigationController

实际上是 nil,并且什么也没有发生。

解决方案是将主视图放置在导航控制器中,并将导航控制器的视图添加到应用程序的主窗口中。

执行此操作的简单方法是:

在应用程序委托中,更改

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.

    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions
(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    UINavigationController* navController = [[UINavigationController alloc] 
        initWithRootViewController:self.viewController];

    self.window.rootViewController = navController;
    [self.window makeKeyAndVisible];
    return YES;
}

“或”类似。请注意,这只是为了让您开始,也许不是最好的方法。 (我假设您刚刚在 Xcode 中创建了一个基于视图的应用程序。)

A guess: Your view is not in a UINavigationController, hence

self.navigationController

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

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.

    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

to

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions
(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    UINavigationController* navController = [[UINavigationController alloc] 
        initWithRootViewController:self.viewController];

    self.window.rootViewController = navController;
    [self.window makeKeyAndVisible];
    return YES;
}

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.)

物价感观 2024-11-12 07:34:29

是的,您没有设置 UINavigationController。

添加

NSLog(@"nav? %@", self.navigationController); 

到您的操作中,您将看到它转储(空)。

但是,如果您输入以下代码,AboutView.xib 可以正常工作:

[self presentModalViewController:aboutView animated:YES];

而不是

[self.navigationController pushViewController:aboutView animated:YES];

显示视图。在您的压缩示例中,AboutView.xib 不包含标签,因此不要怀疑它是否是一个白色页面。

使用来关闭所呈现的模式视图

[self dismissModalViewControllerAnimated:YES];

您可以通过在 AboutViewController 中

。为了获得 UINavigationController 我建议使用基于导航的应用程序模板创建一个应用程序。

Yes you don't have a UINavigationController set.

Add

NSLog(@"nav? %@", self.navigationController); 

to your action and you'll see that it dumps (null).

However, the AboutView.xib works fine if you enter this code:

[self presentModalViewController:aboutView animated:YES];

instead of

[self.navigationController pushViewController:aboutView animated:YES];

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

[self dismissModalViewControllerAnimated:YES];

in your AboutViewController.

To get your hands on a UINavigationController I suggest creating an app with the Navigation-Based Application template.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文