在启动时加载新的.nib

时间:2015-04-02 20:09:18

标签: ios objective-c xcode

在我的项目中,我添加了一个新的Cocoa Touch类及其.nib文件。我调用了文件ViewController。结果三个文件被添加到我的项目中 ViewController.hViewController.mViewController.nib。现在我的项目已经有一个默认的.nib文件,我删除了。然后我去了属性设置并将main interface部分更改为我的新nib文件的名称,即ViewController.xib。然后我将一些内容添加到我的nib文件中,但是在运行模拟器时,我的nib文件内容不会显示。关于我可能做错的任何建议。这些是当前项目中的文件。我正在使用Xcode 6.

文件名:ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end

文件名:ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

FileName:AppDelegate.h

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end

FileName:AppDelegate.m

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end

FileName:main.m

#import <UIKit/UIKit.h>
#import "AppDelegate.h"

int main(int argc, char * argv[]) {
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

最后我得到了这个

Application windows are expected to have a root view controller at the end of application launch

我不确定这是不是一个错误。关于如何解决这个问题的任何建议?

3 个答案:

答案 0 :(得分:0)

在您的applicationDidFinishLaunching

中尝试以下内容
self.window = [[UIWindow alloc] initWithFrame:
[[UIScreen mainScreen] bounds]]; 
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] 
 initWithNibName:@"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;

答案 1 :(得分:0)

您需要在AppDelegate.m中将ViewController设置为rootViewController。首先,您需要导入ViewController.h:

self.window.rootViewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    return YES;

答案 2 :(得分:0)

在从头开始重建之前,您是否从模拟器或设备中删除了应用程序?通常,时间.xib更改在您执行之前不会生效。