How do I know which xib is the first one to load? - ios

I am researching a huge Xcode project, how do I know which xib is the first xib be loaded by IOS?
In main.m, I can only know the main delegation file, but how to find the main.xib.
BTW, I search the {project}-info.plist, but found nothing.
Addtion, I know which file is the rootviewcontroller.

From your description the project doesn't use storyboard(s), so the initial window and root view controller are created by the app delegate. This may or may not use an XIB file (you'd need to check the implementation if the view controller class).

Related

how is view controller connected with App Delegate

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.

UIViewController defined in a xib file doesn't load in the storyboard

I'm trying to load a subclass of a UIViewController with its views defined in a xib file into a storyboard. Let's call it a NibViewController.
The point of this approach is to reuse the same ViewController in multiple screens of the app.
I know it's possible to do it manually in the code, but I'm looking for a solution in the storyboard. I've tried suggestions from other topics like this one, but nothing worked. The ViewController is correctly displayed in the simulator but not in the storyboard. Here is the code: https://github.com/srstanic/NibViewControllerInStoryboard
and here is the screenshot:
Am I mistaken to expect the contents of the NibViewController to appear in the storyboard?
Am I mistaken to expect the contents of the NibViewController to appear in the storyboard?
Yes, you are mistaken. Your app is working perfectly so you should stop worrying and just proceed.
By deleting the view from the view controller in the storyboard, you have specifically instructed the storyboard: "Do not make a view for this view controller. At runtime, the view should come from the xib file, not from you."
And that is exactly what does happen at runtime. So just design your interface in the xib file and all will be well.

How are .xib files being automatically associated and loaded with my ViewController?

I'm using Xcode 8 and Swift, not using Storyboards.
I hit Add->Cocoa Touch Class and check the box to generate a XIB.
I now have FooViewController.swift and FooViewController.xib. The xib has the class set under File Owner. As far as I can tell this is the only association between the XIB and the class. The generated class doesn't contain anything that loads a XIB.
I can create and display this view controller like so (from some other ViewController):
let foo = FooViewController();
self.navigationController!.pushViewController(foo, animated: true);
This works and it loads complete with the UI. What I don't understand is why. Most guides and tutorials online suggest that the ViewController needs to manually load a XIB.
What is happening in the default initializer for FooViewController?
Where is the XIB loading happening?
What if I want to override it and load the XIB myself? (e.g. maybe I want to use different XIBs for different devices. Not the best practice, I know, just an example.)
Back from the day in Xcode 4.5 when we used to create a Universal Application i.e.
for
iPhone
iPad
There used to be code in didFinishLaunchingWithOptions in AppDelegate Class to load different XIBs for target devices, so it did the same as you asked above. But given the evolution of Xcode and Cocoa Framework. It has come a long way from that where the compiler does a lot for us automatically.
Here in this specific scenario:
we have provided the file owner to XIB file, so when you are trying to present it, it goes ahead and searches in navigator/file available if a XIB is available for it, if it finds such a file, it loads the UINib from the bundle and loads it
if it doesn't it goes to the code to find if there is any
programatic view being created while we do super.viewDidLoad()
once it doesn't find anything, it generates a blank view for the same view controller.
If we look closely in the XIB file, there is a view outlet attached with main view available in the XIB but we have created no such outlet, so its connecting an autogenerated outlet for VC Source file with the view in XIB. If it makes sense.
Also please find some code below for reference from tableView Context:
let newAccountTableCellNib:UINib = UINib(nibName: "AccountTableCell", bundle: nil)
tableView.registerNib(self.newAccountTableCellNib, forCellReuseIdentifier: "AccountTableCell")
This should solve your query. Cheers!!

View Controller Loads Twice - How Do I Fix It?

The Problem Cause
When I added a new Swift class file to my iOS project, I decided I was indecisive and renamed it, deleted it, and created a new file. I think that I was actually so indecisive that I renamed it a few times.
The Problem
Now that viewController loads twice. I can literally see the transition happen twice and, during testing, I can see my viewDidLoad run twice in my console.
Common Fix
I know the first solution to check for is duplicate segues / segue calls. Check your document outline to see if there's more than one segue. In my case, it is definitely not a segue problem in the storyboard or the viewController.
My Uncommon Situation
I found someone who had the same load-twice problem and it was because of the rename:
They said it was the renaming I did that is at the root of my problem and it is not easy to fix because the duplication is now in my XML file. But where is this XML file and how do I edit it to erase the original instance of the class? I have no idea.
So how do I solve this loads-twice issue? I just want my viewController to have a normal segue!
Resolved my issue!
If you messed around with a class's name (or it's file name) and have the same "loads twice" issue that I was having, you might be having a hard time solving it. The only suggested solutions I found said to create a new project from scratch and copy stuff over. BUT, instead...
Here's the simplest solution ever:
Rename the class of the viewController that's loading twice! Don't worry about the file, but the view controller class within it. Your XML file probably has duplicates of the original class name, but rename your class to something entirely new and you should be okay.
Delete Segue and declare it the right way.
I faced the same problem. The reason was I ctrl dragged from the TableViewCell to target ViewController.
So I deleted the segue simply by clicking on it and pressing the delete key on my keyboard.
Then ctrl dragged from the ViewContoller of the first view to the second ViewController.
The wrong way to declare Segue
The Right way to declare Segue
It happened to me also. My case was for Creating a segue from my UIButton and performing it from source code.
If it matches to your case then just create the segue direct from the ViewController to your desired ViewController.
The perform the segue. It works.
I recently created the same problem where my view was loading twice. I have a tabbed app. The view controllers for each tab uses a Content View with a segue. For some reason I had set the Custom Class for the View Controller and the Content View Controller to the same custom class.
I don't know how both got set, probably something I did and forgot. If you are seeing a view controller load twice and the above answers didn't help, check to see if you have the parent view and the content view using the same custom class.

Example of iPad SplitView where there is an .xib file specified for Root Controller?

Is it possible to specify a xib file for the RootController of an iPad's splitview?
Open the MainWindow-iPad.xib that is autogenerated with your project. You should be able to expand the SplitViewController to display the viewControllers that are housed in it. You'll notice that the Detail side says something about "Loaded by DetailViewController.xib". You'll have to click the RootViewController icon and specify a nib name and make the necessary connections in Interface Builder.
I'm sure you found an answer already, but for folks who are reading this and want a good source, the Apple Sample COde "MultipleDetailView" uses a SplitView that goes to new nibs for the RootController...just use that as a guide!

Resources