I’m also getting the errors when running in the simulator using Xcode 7. Is there anyway around this without having to create the extra ViewController and MyView classes?
I’ve been doing some research and managed to find two things that have worked out.
Firstly, there is an Empty Application template you can reinstate to Xcode 7. The link was mentioned in another thread but I’m adding it here for ease. Follow the instructions for installing.
Secondly, after doing this still didn’t work I read a little more about the error that is thrown and it would seem that in Xcode 7, all windows must have a rootViewController, which is obviously not mentioned in the book. A thread over at Stackoverflow (link below) mentioned this requirement providing the following code:
So between using the Empty Application template with Xcode 7 and manipulating the code setting the rootViewController I got this:
//Create and confirgure the UIWindow instance
// A CGRect is a struct with an origin (x,y) and a size (width,height)
CGRect winFrame = [[UIScreen mainScreen] bounds];
UIWindow *theWindow = [[UIWindow alloc] initWithFrame];
UIViewController* vc = [[UIViewController alloc]initWithNibName:nil bundle:nil];
theWindow.rootViewController = vc;
self.window = theWindow;
Hope this helps someone else and I’m sure I’ll be back on with my next issue shortly!
johnashmore–
The View Controller needs to have a view attached to it. That’s what the MyView class is there for. It’s attached in this line:
self.view = backgroundView;
The loadView method is a system callback. It’s called by the system when the view is first displayed. For more info on this, see the documentation for UIViewController.
This makes a bit more sense now. I thought loadView must be a method from one of the provided classes but didn’t know where. I need to get better at checking through the documentation.
[quote=“stormville”]johnashmore–
The View Controller needs to have a view attached to it. That’s what the MyView class is there for. It’s attached in this line:
self.view = backgroundView;
The loadView method is a system callback. It’s called by the system when the view is first displayed. For more info on this, see the documentation for UIViewController.[/quote]
Is there any reason why you created a new class and just didnt do:
Thank you for sharing . The error is fixed.
I delete MyView.h and MyView.m,“MyView *backgroundView = [[MyView alloc] initWithFrame:frame]” change to “UIView *backgroundView = [[UIView alloc] initWithFrame:frame]”. Because I think MyView class doesn’t work in this example.
But I don’t know why should do like this.Can U explain it。thank you.
I got stuck with a runtime error when I tried to run this app at XCode8.1 and the combination of your post with another one from MikeyWard here on the forum solved it for me. So here’s what I needed to do:
To get the app running I just needed to add the following bold line to the AppDelegate.m file:
…
UIWindow *theWindow = [[UIWindow alloc] initWithFrame:winFrame]; theWindow.rootViewController = [[UIViewController alloc]init]; // Crucial to make the app run
self.window = theWindow;
…
This is because apparently now Xcode demands a root view controller instead of just giving a warning.
The app will run, but none of it’s view objects will respond. To get them to respond I used the MikeyWard’s solution: