I am trying to learn this book but all the examples are without storyBoard, and when I tried to build the application I caught this error:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Application windows are expected to have a root view controller at the end of application launch'
I tried to fix it with this:
ViewController *myView = [[ViewController alloc] init];
[self.window addSubview:myView];
self.window.rootViewController = myView;
but then I received another error:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[HypnoView _preferredInterfaceOrientationGivenCurrentOrientation:]: unrecognized selector sent to instance 0x78f68730'
Can someone explain to me why is it happening to me?
Firstly you cannot add an
UIViewController
as a subview
The method
addSubview:(UIView *)
expects a UIview and you are adding a UIViewController.
Secondly adding subview was used in iOS 4 and earlier, Now you add a view controller as rootviewcontroller.
So simply add,
self.window.rootViewController = myView
In case you wish to use addSubview, you need to do this
[self.window addSubView:myView.view]
Goto AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window.rootViewController = self.viewController;
return YES;
}
It's done this way. Go to AppDelegate.m and go to - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
ViewController *myView = [[ViewController alloc] init];
self.window.rootViewController = myView;
[self.window makeKeyAndVisible];
If you want to add an UIView as a subview. Then do the following:
UIView *view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window makeKeyAndVisible];
[self.window addSubview:view];
Objective-c
@interface AppDelegate ()
@property (nonatomic, strong) ViewController *viewController;
@end
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self setViewController:[[ViewController alloc] init]];
[self setWindow:[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]];
[[self window] setBackgroundColor:[UIColor redColor];
[[self window] setRootViewController:[self viewController]];
[[self window] makeKeyAndVisible];
return YES;
}
Swift
var window: UIWindow?
let viewController = ViewController();
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window!.backgroundColor = UIColor.redColor()
self.window!.rootViewController = playerController
self.window!.makeKeyAndVisible()
return true
}
Try this code
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
_window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
ViewController *controller = [[ViewController alloc]initWithNibName:@"View" bundle:nil];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:controller];
[self.window setRootViewController:nav];
[self.window makeKeyAndVisible];
return YES;
}
You need to set:-
in AppDelegate.m file: _applicationDidFinishLaunchingWithOptions_
UIView *myView;
self.window.rootViewController = myView
myView= self.viewController;
[self.window makeKeyAndVisible];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.