Xcode 4.2 iOS Empty Application and storyboards - ios

I'm an Xcode newbie, and I'm trying to make my first training app. Since apparently Empty Application template is the only template that offers pre-integrated Core Data, I choose that. However, after that, I can't get UI to work (it remains empty).
What I did:
Empty Application template
New iPad Storyboard file
Splashed Tab Bar Controller onto it
Changed Main Storyboard in Project's Summary view
Hit ⌘R
Stared at pure-white iPad screen, without any tabs
I tried diffing against another project that I created as a Tab Bar Application (which does reflect my Storyboard changes), without any insight.

Comment out (or remove) the window creation and display code in AppDelegate.m as follows:
- (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;
}
When using a storyboard, a main UIWindow is created for you automatically. What is happening in your case is that you are creating another white window and putting it over the top of the tab UI.
ALSO - note that the Master/Detail template also gives you a core data option.

For an Empty Application project, you have to do two things, after you've added your Storyboard file...
Add a row to your project Info.plist file:
Key: Main storyboard file base name
Value: Storyboard
(or whatever you named your storyboard file)
Delete the contents of application:didFinishLaunchingWithOptions: except return YES;:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
return YES;
}

The Master-Detail and Utility project templates also offer Core Data as an option.
The Apple templates for Core Data are pretty horrible. They stuff far too much functionality into the app delegate and they use lazy loading unnecessarily, which just complicates things even further.
You're better off looking at the generated code and adding the functionality as a separate class in a project you start without Core Data.
To answer your immediate question though, the default empty template creates a window programmatically in the app delegate's application:didFinishLaunchingWithOptions: method. The story board sets a window up itself, so you need to remove that code from the app delegate. The only thing you need in that method is return YES;.

Related

UIWindow not filling screen for new devices?

I'm working with an old Xcode project which is roughly 7/8 years old and I've migrated it to Objective-C 2.0 and to support ARC. The project didn't support Auto-Layout (this was just before it was released) prior to me working on it. I've managed to clear up a lot of the errors/issues i was having with running it in a more modern Xcode IDE (10.2.1). For testing purposes I have implemented this code into the AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, UIScreen.mainScreen.bounds.size.width, UIScreen.mainScreen.bounds.size.height)];
[self.window setBackgroundColor:[UIColor redColor]];
self.window.rootViewController = [UIViewController new];
[self.window makeKeyAndVisible];
return YES;
}
which gives me this for some reason:
This is the result when i run on a iPhone X, iPhone XR or iPhone XS Max, it looks like the top and bottom margins (safe areas) of the screen are not being picked up on. When i debug the view hierarchy they aren't being picked up on either, it just shows the red space? When i throw this code in a new project it works fine and fills up all the space. I'm wondering that because this is a old project there might be a build setting which is restricting the UIWindow from expanding to allow support for new devices?
Also as a side note I've made sure the UIWindow object isn't being manipulated elsewhere.
Try to add "Launch screen interface file base name" (UILaunchStoryboardName) to your Info.plist
It seems that the presence of this key tells the system that you natively support new device types and size classes, so your window can fill all available area.
Don't mess with the storyboard.
Add UILaunchScreen dictionary entry to your .plist.
Include UIColorName string value with background color name defined in the Assets.xcassets.
<key>UILaunchScreen</key>
<dict>
<key>UIColorName</key>
<string>launchScreenBackground</string>
</dict>

iOS white screen debugging

Sometimes, when I run app, I got white screen. But I know app is still running (coz I need to play sound).
Somehow, the view hierarchy mess up I guess. Problem is that I never see white screen if I run from Xcode. If I see, I will know how is the hierarchy.
dispatch_async(dispatch_get_main_queue(), ^{ //need to run in main thread.
self.window.rootViewController = self.defaultVC;
[self.window makeKeyWindow];
self.window.hidden = NO;
});
Is it because of view hierarchy? Or any other reason which can produce white screen?
There is an awesome little tool called PonyDebugger which enables view-based debugging. You just throw it all in your project and start it in your AppDelegate and you can see at anytime exactly what view is displayed.
You can basically step through the hierarchy like in "chrome F12".
This helped me solve WhiteScreen-problems in the past.
Check your Copy Bundle Resources (found within Build Phases) and make sure that all of your .xib files are correctly listed (e.g. that they are indeed there).
Also is there a specific reason you are creating that window programmatically instead of using setRootViewController?

The "Empty Application" Is Missing In the Newer Xcode Versions (Objective-C)

In old versions of Xcode, an "Empty Application" would be a project with all the information to build and everything, but no files except the App Delegate would be added.
In the newer versions of Xcode, the "Empty Application" option is no longer the same. Instead of creating a project with no files but the App Delegate, it creates nothing at all! You either have to drag in other files pre-made, or you have to create all the files, including the App Delegate and all of it's once "default" functions.
How would I get the previous version of "Empty Application"? "Single View Application" is a little different, but nothing else seems to be similar.
How would I get the previous version of "Empty Application"
You would have to make it for yourself by paring down the Single View template-based project.
So, for Objective-C:
Delete the storyboard.
Edit the target and delete "Main" where it asks for the storyboard name. Hit Tab to make this change "stick".
Delete the view controller files.
Make a new view controller and (optionally) a .xib paired with it, to give yourself a starting place. Let say it's called MyViewController.
Delete everything in the App Delegate implementation of applicationDidFinishLaunching and replace it with:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = [MyViewController new];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;

Xcode 5 Simulator Blank White Screen

I just installed Xcode 5 (or the latest version) and created a new project. I created a storyboard, and added a label, but when I open my application in the iPhone simulator, I simply get a blank white screen with a status bar. What am I doing wrong?
I have OS X 10.9.1 Mavericks.
I know I'm quite late to this, but the solution to this problem is quite simple and is even shown in one of Apple's own tutorials.
Assuming that you started off with an "Empty Project", created a storyboard from scratch, and set your storyboard as the main interface, you need to remove a few lines in the first method of AppDelegate.m.
This:
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;
Should become just this:
return YES;
I'm going to assume that you started an empty project, which doesn't start with a storyboard and then after creating the project, you created a storyboard file.
You have to tell your app which storyboard to load.
In the below screen shot, you'll want to click the "Main Interface" drop down and select the storyboard you want to start your app with.
This is the "Deployment Info" section of the "General" tab of your targets.
You also need to add a couple lines of code to your AppDelegate.m. It should look like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"YOUR_STORYBOARD_NAME" bundle:[NSBundle mainBundle]];
UIViewController *vc =[storyboard instantiateInitialViewController];
self.window.rootViewController = vc;
[self.window makeKeyAndVisible];
return YES;
}
Your project does not have a view controller. When you created your project, you should have started with a "Single view project", which would have created a view controller for you. In that case, you would have been able to see your label.
EDIT TO FIX:
If you would like to fix this, remove the property "Main nib file base name." This can be found in the "info" tab of your target.
The problem:
It seems that when you created your application, you selected the "Empty Application" template. Then you added the Storyboard from the user interface section. When you added the label and ran the application, you can't see the "Hello, World" label, because the application does not have a root view controller at the end of the application launch.
Try to create a "Single View Application".
Just wanted to chime in on this thread and shed some light on the matter. I myself was working on my first project and was getting the white screen. I found this article and it helped and it didn't, but this is what worked for me. Everyone is right on what they posted, but I was running 9.2 simulator when in fact I only have 9.1 installed (if you need help with this go to Xcode> Preferences>Downloads and download the appropriate package. Once I did that I ran the simulator and got the white screen. Now my white screen had the carrier and batt icon at the top. To fix this issue in Simulator: Hardware>Device>9.1 and a reboot occurred and then it works...
Hope this helped.
SithAdmin
I had the same issue (blank screen) for a stupid simple error. Actually, the elements appeared and soon fade away.
My mistake was that I was not creating in in Main.storyboard (but LaunchScreen.storyboard in place, so that it obviously disappeared couple of seconds after start).
Stupid but at least, quick to check: create views in Main, not LaunchScreen...
Click "Main.storyboard" -> on right prat "Interface Builder Document" uncheck "Use Auto Layout, "Use Size Classes" & "Use as Lunch Screen"

Generating a view programmatically in iOS

Update:
Thanks for all the tips, everyone. The tutorial mentions that a XIB file is provided (which I don't have) so I'm doing something wrong in how I'm creating the the project.
I am following Apple's Core Data Tutorial for iOS. This tutorial has not been updated for ARC—apparently for Xcode 4, since it asks to "create a new project using the Window-Based Application template in the iOS section."
Since that option doesn't exist under Xcode 4.4.1, I looked around Stack Overflow and read that I should create an empty application. As per the tutorial instructions, I created no Storyboard or NIB file.
Other than updating the code for ARC (using strong in place of retain and not implementing the provided dealloc method), I'm confident that the code in my project matches that of the tutorial up to the end of the chapter "The Table View Controller." At this point, the tutorial says I should be able to run the project and get a view.
Instead, I get a blank, black screen.
Maybe my problem is too vague to solve here, but should I perhaps be using a different project template? Which one?
I have only two classes: a RootViewController and an AppDelegate. AppDelegate imports RootViewController and contains a UIWindow property. Again, there is no Storyboard or NIB in the project.
I can provide any code too if there's someplace specific to look.
If you want to check if your setup is correct do the following:
add a background color to your window
self.window.backgroundColor = [UIColor whiteColor];
make sure you tell the window to display itself
[self.window makeKeyAndVisible];
make sure your view controller is the window's rootViewController
self.window.rootViewController = myViewControllerInstance;
Choose Single View Application, and uncheck 'Use storyboards" field. The rest should go the same.

Resources