how is view controller connected with App Delegate - ios

I was trying to understand the flow of this app: https://github.com/JoelMarcey/iOS-2048
but I am not sure how this app works (absolute beginner)
So we have main.m which happens to be a starting point of the app?
https://github.com/JoelMarcey/iOS-2048/blob/master/NumberTileGame/NumberTileGame/main.m
main.m imports AppDelegate.h
https://github.com/JoelMarcey/iOS-2048/blob/master/NumberTileGame/NumberTileGame/F3HAppDelegate.h
AppDelegate.m does not import ViewController? So how does this App show initial screen and screen thereafter?
Shouldn't there be some code in AppDelegate.m? Can someone please explain me the code?
Also, how do I see Main_iPhone.storyboard in my project and not in repo online?

If you look in the NumberTileGame-Info.plist file, you'll see that there's a reference to a Storyboard. A storyboard is a file that the system uses to load UI elements such as view controllers, including (sometimes) the connections between the controllers.
In this app's case, the storyboard loads F3HViewController as the initial view controller. That's why you don't see a reference to the controller in the app delegate. Regarding your question of code in the app delegate, it's not necessary to have a bunch of code in the app delegate -- it's an architecture decision that determines what code goes in view controllers, the app delegate, etc.
Regarding your comment about the storyboard, the storyboard is in the repo -- it's in Base.lproj.

AppDelegate.m does not import ViewController? So how does this App show initial screen and screen thereafter?
This is a storyboard-based app. The Info.plist specifies the storyboard to use, and that storyboard specifies the initial scene, which is basically a graph of objects including a view controller and its view hierarchy.
If you open the storyboard file, you'll see an arrow pointing to the first scene, which will likely have an instance of F3HViewController. I haven't actually downloaded the project, but that class seems like the obvious candidate since it only has a -playGameButtonTapped action that presumably is connected to a "Play" button. That action then instantiates and presents the next view controller, F3HNumberTileGameViewController, and gameplay begins.
Shouldn't there be some code in AppDelegate.m? Can someone please explain me the code?
Only if you want to customize what the application does. In this case the developer doesn't seem to want that.
Also, how do I see Main_iPhone.storyboard in my project and not in repo online?
It's in the Base.lproj subdirectory.

Related

How to make framework with storyboard and UIViewControllers

I made an app with two View Controllers and storyboard to them. At all everything works perfectly but at all I must to create framework from this objects.
The structure looks like this:
Framework:
FirstViewController.swift
SecondViewController.swift
Storyboard.storyboard
AppExample:
AppDelegate.swift
I want to present in AppDelegate first View Controller. After presented it user will make some stuff and after push the button SecondViewController will be pushed on the screen. This second View Controller could be dismiss by the user.
The problem is:
I move all swift files to the Framework folder with right target membership.
After init the First ViewController I have blank screen
I do not know how to connect storyboard with the views
This two UIView Controllers are only the tool that will be added to the few apps later. The point is that developer can put first View Controller wherever he want and after that he will have the answer from Second View Controller.
I will be glad for help.

How can I set the initial controller on storyboard contains WKInterfaceController programmatically?

I want to have few view controllers on the Watch app storyboard. The issue is that I want to load different initial view controller depending the target I'm building for.
I can see there is a single value called "is initial controller" on the WKInterfaceController but I can't find a way to set the initial view programmatically. I tried using other methods supplied but all are pushing the second view on top of the original initial which is not what I want (you have a back button on top bar).
If you have any way you can think of supporting this that's great. Creating a different storyboard file is last option.
Actually, the solution is really simple. It is true that the pages are being built before everything I had to rebuild all of it at run time. More that the flag (Is initial Controller) couldn't be changed at run time but you sure can build all from scratch.
I set the identifier of the WKViewController to "exampleViewControllerName" (on the StoryBoard) and add this code to awakeWithContext:context
NSArray *array1=[[NSArray alloc] initWithObjects:#"exampleViewControllerName", nil];
[WKInterfaceController reloadRootControllersWithNames:array1 contexts:nil];
You sure can use the context and make that more complicated but that's the basic of it, and good enough for the simplest answer possible.
What you are actually asking is how can you control the initial view controller of a watch kit app.
From the Apple Watch Programming Guide
All interface controllers in a page-based interface are created and
initialized before the interface is displayed, but only one interface
controller at a time is displayed. Normally, WatchKit displays the
first interface controller in the sequence initially. To change the
initially displayed interface controller, call the becomeCurrentPage
method from its init or awakeWithContext: method.
If you are using a hierarchical interface rather than a page-based interface then your app will always start with the same initial view.

How does system choose between application:didFinishLaunchingWithOptions: or a storyboard to set up window?

I'm comparing the templates for a Single View Application and an Empty Application in Xcode 4.6.3. The Single View Application uses a Storyboard, and has a storyboard file that indicates which view to put in the window when the app starts. The Empty Application sets up and assigns the view to the window in the application:didFinishLaunchingWithOptions: method.
So why is the application:didFinishLaunchingWithOptions: method empty in the delegate for the templates that use a storyboard? In this same method, I would expect to see the code telling it to refer to the storyboard to build and assign the initial window and view, since it is the same method used in the other template. These delegates otherwise look the same.
I'm asking because I want to know how I can switch to using a storyboard later if I need to and vice versa.
If you are using storyboards, it will automatically instantiate the view controller that is assigned as the "root" controller. This is evident by an arrow on the left side of a controller screen pointing to that screen. As to your question about what determines whether the setup occurs, there is a key in your APP-Info.plist file that will tell iOS to look for a storyboard.
Main storyboard file base name - "MainStoryboard"
So, therefore, to add storyboards to your project without the template, you need to create a .storyboard file and then set this key-value pair in your app's plist file.
I hope this clears things up a bit.

Mapview in tabbed application in iOS

I would like to ask something, because as a beginner I do not have any idea anymore
how it could work.
First of all I made a new project, a single view one and added a simple map view.
This worked very well (following a tutorial of course)
Now I tried to add this simple map view to my inital project, which is a tabbed based on with navigation controller.
In this tabbed application I already added two more views with view controllers, and I called them thirdview Controller and forthview controller. The vc's I gave the class thirdview and forthview controller.
So, now I wanted to make the mapview on the thirdview. I did exactly what I did in my "trial project" but it is not working. Well, when I run the simulator I see a map. But it does not show the location as I have specified it inn the thirdview controller.m
Because it was working in the singleview project, I guess I have something to add or to connect in my inital tabbed application?
I would be greatful for any help on this
Thanks a lot
What you have there, as I can see from what you say, is a very simple and litle forget made by everyone who begins with xCode :)
Try to link your viewControllers and your map (in the xib or Storyboard) to your file's owner' view and delegate.
by default, these aren't linked. Just right click on it and drag your View and delegate to your file's owner.
Also, be sure that each of your viewControllers' classes are really thirdview and forthview in the properties (don't be confused with xib identifier!)
Hope this helped you! Feel free to ask if anything is missing :)

What's the proper way to use storyboards View Controllers and scene in iOS application?

I have been working on an iOS application for sometime now, and I think I am not using the View Controllers I have created properly with the storyboard file / scenes I have created for my application.
As it stands, I have two storyboard files, one for the iPhone, and another for the iPad. In the AppDelegate implementation file there is a method called,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
I have been loading a ViewController from within this method to start the load process for my application.
Since I have two storyboard files now, I sourced some conditional code to load a specific storyboard file based on the device that is running the application.
What is the proper way to load the storyboard file?
After I load the story board file do I initialize the root view controller?
Once my root view controller is loaded should it load the elements specified in the root scene?
If I choose to load another scene from the root scene by clicking on a button in the root scene should I dismiss the root scene, and load the new scene, or is the new scene a subclass of the parent scene? What is the proper way to transition between scenes?
Basically I have a separate view controller for every scene in my application, and I have specified a view controller associated with each scene in IB.
Right now I have a button that can be pressed from the beginning / root scene to load another scene which is associated with another view controller. In this new scene I have a done button which segues back to the root view controller.
If I were click these two buttons repeatedly by clicking on the buttons over and over, am I creating new objects of my view controllers or I am reusing the objects that were already created?
I know this is more than one question but I am trying to get a good grasp on view lifecycles, and how view controllers relate to scenes, and the proper way to load the first view controller, which should load the first scene in an application I presume.
What is the proper way to load the storyboard file?
Click on your project in the class navigator. It is the uppermost thing you can click in there, and it has the name of your application on it. Some config files will open right where your source code used to be, and you need to find the one where you can specify a storyboard for different devices. I think it is the second tab from the left, but i cannot check that right now. You just specify a storyboard to use for iphones and a storyboard to use for ipads, and youll be fine.
EDIT: you also need to specify an initailView for both storyboard files. This can be done by simply opening the identity inspector (i think) in storyboards with the view in question selected and checking the box next to "is initial View". Try all four inspectors if my memory fails me. It'll be there, i promise ;)
What is the proper way to transition between scenes?
in most cases, you will use a segue. those thigs are new in storyboard, and they are the bomb. Theres a method you can implement called
-(void)prepareForSegue:(UIStoryboardSegue *) sender:(id)sender
where you can check for the identifier of the segue and then manipulate the destinationViewController-properties directly. Heres a good tutorial:
http://www.appcoda.com/storyboards-ios-tutorial-pass-data-between-view-controller-with-segue/
If you cant use a segue, however, you will need a rootViewController for anything that displays more than a modal view. Basically, rootViewControllers manage a stack of views, and its not your business how they do it. Depending on your UI you might need a NavigationViewController or a TabBarViewController, just google for both and see what suits you best.
Feel free to ask any questions you might have, ill be glad to help you.
Have fun

Resources