Generating a view programmatically in iOS - 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.

Related

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?

Storyboards in Xcode 6

I have recently started to learn iOS with obj-c from "iOS Programming The Big Nerd Ranch Guide 4th Edition". This edition was released in 2014 and is written with Xcode 5.
I am trying to make a simple app with two buttons and two labels. The labels are connected to two arrays and when a button is pressed an object from the corresponding array is shown in the corresponding text label (it's the Quiz app in chapter 1).
I created the project as a Single View app in Xcode 6, and put all my objects in the view controller class. I have two labels two buttons two arrays and an int to keep track of the object that has to be displayed from the array.
In the book it says that I should initialize the arrays in the initWithNibName method. I tried that but for some reason it never gets called. So I changed the initialization of the arrays to the init method. They initialize fine but when they are called from another method they are nil. Do you have any idea why this is happening?
The second issue I'm having is that I can't manage to get the contents of the storyboard on screen. It says that I'm supposed to make an instance of the ViewController inside the AppDelegate and make it the root window controller but all I get is a white window (or black in case I don't set the color).
UPDATE: I changed the intialization of the arrays from the init method to the viewDidLoad method and now they seem to be working fine. Nothing on the screen though.
It sounds like you're initializing your UIViewController from the app delegate AND a storyboard. If you create a new project in XCode, a "Single view application", you won't have to touch the app delegate at all in order to get something on the screen.
I believe both your problems are related to this, since it sounds like you're seeing an empty UIViewController on the screen (the one you create in the app delegate)
As for the initialization of your array, viewDidLoad is a popular place to do this.
If you are using storyboards, the method initWithNibNameOrNil will not be called. In the BNR book, it teaches you to use XIB files, which do use this method. If you are trying to follow the tutorials, I would suggest using XIB files.
For use of a book, I would suggest downloading whatever version of Xcode is being used for that book -- otherwise you will be running into a lot of confusing problems while learning.
If you would like to download previous version of Xcode, refer to this post:
How to download Xcode DMG or XIP file?

What's causing the massive changes in autorotation in 8.1 compared to 8.0.2?

I'm coding a video processing app and was just about to submit it to the app store when ios 8.1 came out. I updated my iPhone as well as XCode and all hell broke loose. In my simple single viewcontroller interface nothing is rotating anymore except for the statusbar, which also doesn't get automatically hidden anymore in landscape mode...
I figured it was because I was using the deprecated willAnimateRotationToInterfaceOrientation: for what little custom rotation actions I had, so I implemented traitCollectionDidChange: and viewWillTransitionToSize: to specs instead. However viewWillTransitionToSize never gets called in my app and traitCollectionDidChange: is only called once, at startup. The device simply isn't telling the app that the device has rotated.
After googling I've also tried using name:UIDeviceOrientationDidChangeNotification. At least my selector does get called for that one but I don't know how to manually handle all rotation.
My didFinishLaunching... and viewDidLoad are very simple. alloc UIWindow, storyboard, set my viewcontroller from there, make it rootviewcontroller, makekeyandvisible. All based on one of Apple's AVFoundation demo apps.
Then in didload I add some subviews and a toolbar etc, nothing out of the ordinary and obviously it did work on 8.0 and 8.0.2 on all kinds of devices as well as the 7.1 simulator etc. Still runs flawlessly on my iPad with 8.0.2... Reason I haven't posted any code is I'm 100% sure everything is correct on that end.
Main weird thing is I can't seem to find anyone with this problem.
No errors in console or elsewhere either.
Does anyone have any idea of what might be causing this? I didn't think a point release would make such massive differences and again, no one else seems to be having this. Could it be an issue/bug in the actual storyboard file?
And, mainly, since I can get rotation notifications through UIDeviceOrientationDidChangeNotification, how do I manually handle all rotation/resizing stuff? I have been looking all over for answers but to no avail and am out of time to spend on this project currently :(
Cheers!
alloc'ing UIWindow will be the problem.
First, Make sure your navigation controller (or whatever you're using) is set as "Initial View Controller" in your storyboard.
Secondly, in your AppDelegate.m file, remove any references to UIWindow and rootViewController that appear in application didFinishLaunchingWithOptions. In my case, removing the following two lines fixed my issues.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window makeKeyAndVisible];
You also don't need to set the window's rootViewController if using storyboards.
They're simply not needed when using storyboards, but until 8.1 there was never any harm using them. It took my 2 days to figure this out, so hopefully it will help you and others too.

Bringing In An Outside Project Made Through Code To An Xcode Project Made With Storyboard

I currently have an Xcode project that uses storyboard and I have an Xcode project in which I followed a tutorial and made a simple weather view controller. This Weather view controller uses CocaPods and requires that I use .xcworkspace whereas in my previous Xcode project (the one in which I am making via storyboard) I have been using the .xcodeproj file. I am trying to bring the weather view controller project into my storyboard Xcode project. Hopefully, I will have a button on one particular view controller link to the Weather View Controller that was initially created in another application.
I have asked a previous question in regards to a project like this but with no luck. The previous question can be seen here: combining-two-xcode-projects-one-with-code-and-one-without
In order to test the one answer I as given on that previous question I went about and created a blank Xcode Project from scratch, uploaded all of the .h and .m files, and then went into terminal (I am using mac) and programmatically the pods. The pods installed perfectly. I then put down a button and used the IBAction mentioned in the question I posted the link to above:
//When the button is tapped
-(IBAction)buttonTapped:(id)sender
{
//initialize the code only controller
WXController *WXController = [[WXController alloc] init]
//present the view
[self presentViewController:WXController animated:YES completion:nil];
}
I then ran the project and came across this error that highlighted this piece of code:
WXController *WXController = [[WXController alloc] init]
The error stated:
"No Visible #Interface for 'WXController' declares the selector alloc"
Just to make sure nothing else worked, I also tried to open the same project with .wcworkspace and came across the same error which I wasn't able to figure out.
I figured it must have been a button with the linking of the view controller with the button so not only did I post prior to this but I also did my own research and didn't have any luck as well. I checked out these posts:
How to combine two projects into one project
Loading a storyboard from a different Xcode project
Just to make the question I'm asking more clear, I included a link to the Weather Xcode Application I am trying to link to my storyboard application. You should be able to open the application here: Link
I was wondering: What exactly am I doing wrong that is providing me with the "No Visible #Interface for 'WXController' declares the selector alloc" error and even if this is fixed, am I missing something or should the view controller display properly.
Also, I have previously been using the .xcodeproj for my storyboard Xcode application. Is it necessary that once I bring this Weather view controller in, I must switch to .wcworkspace? and if so, what is the difference/ positive/ negatives of each?

UIStoryboard keeping old outdated references

In my iPad app that uses a UIStoryboard I used to have UITabBarController with a couple tabs and icons. Since then I have refactored my app to not use the UITabBarController (deleting the view controller in the storyboard. However, when I run my app I get the following error:
Could not load the "3dicon26better.png" image referenced from a nib in the bundle with identifier "com.xxxxx.yyyyyyy"
The icon in question was used as the tab bar icon in the old UITabBarController. Apparently my storyboard is keeping references to old deleted view controllers. Is there any way to clear them out?
Don't have a great solution, but I was able to stop the error by using a process I found in another SO post (can't remember where I found it).
Open my Storyboard in textedit. Search for references to the image in question. Comment them out XML style.
This stopped the errors, but it bothers me that my Storyboard has so many extra dead references in it. :/

Resources