【问题标题】:How to change rootViewController's view?如何更改 rootViewController 的视图?
【发布时间】:2012-03-02 05:16:09
【问题描述】:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil] autorelease];
    } else {
        self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil] autorelease];
    }

    CustomView1 *CustomView = [[CustomView1 alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
    self.viewController.view = CustomView;
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];

    return YES;
}

这出现在 AppDelegate.m 中

我想将rootViewcontroller的视图更改为CustomView,并尝试了上面的代码,但是背景颜色仍然是黑色并且不接受触摸事件。

哪里出错了?

【问题讨论】:

    标签: objective-c ios view viewcontroller


    【解决方案1】:

    我不确定问题出在哪里,但我有 2 条建议。

    1) 改变

    CustomView1 *CustomView = [[CustomView1 alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
    self.viewController.view = CustomView;
    

    CustomView1 *CustomView = [[CustomView1 alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.viewController.view addSubview:CustomView];
    

    2) 在您引用的两个 .xib 文件中,转到顶视图并将其更改为使用自定义类。然后你就可以让应用正常启动了

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
        // Override point for customization after application launch.
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
            self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil] autorelease];
        } else {
            self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil] autorelease];
        }
        self.window.rootViewController = self.viewController;
        [self.window makeKeyAndVisible];
    
        return YES;
    }
    

    旁注: 您应该避免命名以大写字母开头的变量。许多编码标准的类以大写字母开头,变量以小写字母开头。

    【讨论】:

      【解决方案2】:

      如果您需要两个不同的视图控制器来管理一个视图类,您应该让视图控制器在它们的loadView 方法或它们的 nib 文件中加载正确的视图类。我猜你代码中的控制器使用笔尖。如果是这样,请编辑 nib 以将视图类设置为 CustomView1

      CustomView1 是一个不幸的类名选择,恕我直言,因为它没有任何意义。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多