If I create a new project in Xcode 3 - a "Universal" window based project, I can't seem to instantiate the UISplitViewController outlet I am adding to the iPad's XIB.
Starting with a brand new "window-based" project, I select "Universal" from the drop down (iPhone, iPad, Universal). I then create an IBOutlet property in AppDelegate_iPad.h, synthesize the variable in the .m file and release it in the appropriate dealloc.
I open MainWindow_iPad.xib file and add a UISplitViewController object to it. I then connect the "splitViewController" outlet from AppDelegate_iPad to the UISplitViewController I just dragged onto the XIB.
Unfortunately, when I run this, splitViewController is never instantiated. Consequently, I can't add it to the window's view or set it as the window's root controller. I check this by putting a break point in
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
to view the splitViewController ivar - but unfortunately, it is always 0x0. Is there some special step I am missing? I've compared my code to the split view controller template in Xcode and I can't see any differences.
Related
Xcode with the project file and Info.plist locates the main.storyboard and finds out the initial ViewController. But I was hoping to see some boiler plate code in AppDelegate for the ViewController.
AppDelegate looks blank: (No reference to ViewController)
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
return YES;
}
...
#end
StoryBoard is xml:
Question
Does Xcode what magic code to refer storyboard xml and find the controller ?
After the compilation, main() brings up AppDelegate which should either have reference to ViewController directly or have a proxy storyboard object to get to the ViewController.
What am I missing ?
I think my book (which you already found) explains it fully. main calls UIApplicationMain() and it follows certain rules. If you have a main storyboard designated in your Info.plist — and you do — then:
UIApplication pulls the initial view controller instance (the one with the Entry Point arrow) out of the storyboard. That's the ViewController instance.
It also instantiates the UIApplication class.
It also instantiates the app delegate class (specified in the UIApplicationMain call), and assigns it to the UIApplication's delegate property.
It makes a window and assigns it to the app delegate's window property, then assigns that view controller to the window's rootViewController property.
Then it shows the window (and calls applicationDidFinishLaunching... on the app delegate`).
That completely explains the launch process.
A picture is worth a thousand words.. However, here's a short description: "It's in your project's settings/info.plist".
In the picture below "Main" is the name of my initial storyboard. Also, initial view controllers have a flag in the interface builder that says "Is Initial View Controller" to tell it that's the view controller you wish to run first.
P.S. Clicking on the images enlarges them :)
The short answer is that the dynamic nature of Objective-C allows UIApplicationMain() to bootstrap your application solely from the textual information stored in your application's info.plist file and your main NIB or main Storyboard. There's no need for the compiler to generate boilerplate code.
The longer answer:
UIApplicationMain does the following things to through the Objective-C runtime:
In all cases, UIApplicationMain() instantiates the NSPrincipalClass key in your plist, typically UIApplication but can be a custom subclass.
In an old-style NIB-less application, UIApplicationMain() will instantiate the AppDelegate class that was specified in main.m. It will wire the AppDelegate to UIApplication, and then hand off to the AppDelegate to create the app window, root view controller, etc.
In a NIB-based application, UIApplicationMain does not know the name of the AppDelegate. Instead, it loads your main NIB file NSMainNibFile, as specified in the plist. The NIB is responsible for instantiating your AppDelegate and wiring to UIApplication, along with creating your main window and root view controller.
In a Storyboard application, UIApplicationMain instantiates the NSPrincipalClass and the app delegate as specified in main.m, just like a NIB-less application. It then loads the main storyboard, which knows who the initial view controller is. It creates a window, instantiates the initial view controller, and assigns it as the root view controller for the window.
That's the reason behind it :-
and if you remove that mark You'll get a blank screen.
At the launching phrase of an App, which controller would it use as the rootviewcontroller for it's window (while they both exist)? the one declared in AppDelegate or the one used in Storyboard?
If nothing is declared in AppDelegate, would the App create a UIWindow for it's .window property by default and make the entry viewController in the designated storyboard it's rootViewController?
If nothing added in applicationDidLuanch: delegate Method for creating a new window, so your app create your UIWindow and make the root is the root in your storyboard file, but if you want to create custom starting point, add another storyboard or even a single .xib file as your staring point and let your appDelegate know your starting point.
I have a project that I originally started working on in Xcode 4. I then moved this project to Xcode 6. The project compiles and runs swimmingly. I then wanted to move this to use Storyboards.
Reading here and elsewhere I see people say they accomplished this by doing the following:
Create storyboard
Drag a viewcontroller onto the storyboard
Click on the white of the viewcontroller
Delete it Goto the original XIB and copy the entire view
Paste that into the viewcontroller on the new storyboard
Go into the appdelegate.m file and edit the didFinishLaunchingWithOptions section to look like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
return YES;
}
No errors in my code. Builds fine but instead of seeing the new Storyboard / ViewController, I just see a black screen. Im sure I am missing something somewhere but I cannot seem to find what it is.
If you would like to see my code, I have it here: https://github.com/martylavender/LittleToDoApp/tree/Storyboards
Thanks
You need to set your first view controller as the initial view controller in your storyboard.
You just need to check the is initial View Controller line on the right panel
I figured it out.
Looking at the build information of the project, I hadnt set the Main Interface to the MainStoryboard. That with the earlier suggestion fixed it!!
Thanks!!!
when you start your app using single view template, and you add the NSLog(#"self.window = %#", self.window); in your first line of the AppDelegate.m's application: didFinishLaunchingWithOptions: method, you can see that self.window exists in your app.
However, when you start your app using empty template, and tried to log the self.window to the console, the result returns null. Even if you add storyboard and a view controller, and set its view controller as the initial view controller, and attempt to log the self.window, the result is the same - its value is set to null.
And note that whichever way you take, you can find you declare #property (strong, nonatomic) UIWindow *window; in AppDelegate.h by default. So I wonder why in the first case, you can see that self.window is initialized and set the value but not in the latter case. Also, if self.window is already declared and initialized in the first case but NOT in the second case, how can I find the initialization code?
It looks like in both cases, the #property declaration is same - and in both cases, as I mentioned, I tried to log the value of self.window in the FIRST LINE of the AppDelegate.m's application: didFinishLaunchingWithOptions: method.
So anything that I'm missing? I don't know why those two cases act differently despite me not finding any differences in both code and storyboard.
I use iOS 7 and Xcode 5. Thanks.
OK, when you create a project with a Storyboard or Nib then the project settings will tell the project that the storyboard/nib is the "Main Interface".
This triggers the application to load that interface on start up. This is why the self.window is created in these cases.
When you create an empty application there is no interface to set as the main interface.
You then need to create the window yourself like this...
-(void)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIViewController *someController = [UIViewController... //create your initial controller
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
}
Something like this anyway. It's been a while.
Alternatively, if you create an empty application and then add a nib file that you want to use as the initial nib then you can select it in the project settings.
In the Target in General. In the section "Deployment Info" select the "Main Interface" from the nibs in your project. This will then load that nib when the application starts.
Xcode declares UIWindow as IBOutlet Object in Appdelegate and xcode itself hooks or connect it with the window of default ViewController.nib(Created by Xcode when you create non empty project).There is no need to initialise any object if you have declared it as Iboutlet and connected it with any UIController in nib.
Now in empty project if you want to create window declare it as outlet and connect it with window in exist in nib and make your AppDelegate as Files OWner.
I have some problem in creating View(must open at start of app) in already existing project.
I have project from https://github.com/emysa341/Login-with-gmail-google-g--using-oath-2.0-protocol , when i run this project it directly goes to login page but i need to change that to home screen. i.e when i click on button in first view then it has to go to login page.
I tried creating .h,.m and .xib file and tried to load these files first as soon as app starts but i am not getting that.
Just tell me process "How to create .xib file in an existing project and make that xib(created by me,not default) to load first?". I googled but no perfect solution.I tried changing code in appdelegate.m but no use.
In this method of appDelegate add this line
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
YourViewController *yourViewController=[[YourViewController alloc]initWithNibName:#"YourViewController" bundle:nil];
self.navController=[[UINavigationController alloc]yourViewController];
self.window.rootViewController=self.navController;
[self.window makeKeyAndVisible];
return YES;
}
If you are using storyboards, you set the first view there. Just open the .storyboard file in Xcode, and there should be an arrow that is faded out on one end, and the head of the arrow should be pointing to a view controller. Just click-and-drag that arrow to the view controller you want to show first, and you should be good to go.
Alternatively (still in the .storyboard file), select a view controller and in the Attributes Inspector there is a checkbox for "Is Initial View Controller" - check that box for the view you want to show up first.