iPhone navigation controller basic question - iphone

This is a basic question about navigation controller, however, I don't seem to get it right.
I am creating a basic navigation controller application and using my custom view instead of the default rootviewcontroller and rootviewcontroller.xib that are automatically generated.
Here is what I have done:
Opened the MainView.xib and deleted the rootviewcontroller
Then, Xcode->new file->UIViewController class (with xib) and named it test
Opened mainview.xib, clicked on the section where it was pointing to rootviewcontroller and in inspector, changed the xib to test
In test, I just added a button.
However, when I load the application, the test doesn't seem to load. I added a log statement for viewdidload just to verify and the statement is never logged. Please let me know the fundamental connection I am missing here.

You must change the class of the RootViewController to your own (Inspector -> CMD+4).

Related

ViewController for Second View - iOS 7

I'm a beginner to iOS development, so forgive me if this is really basic. It's probably answered somewhere, but I've looked for a long time, and I'm struggling.
I have a second View on my story board that I've successfully linked to the first view using a Navigation controller and stuff, and I'm able to navigate to it. I can also add actions/outlets from elements on my first view by Control-dragging to the .h file.
I have a label on my second view, and I want to be able to do the same: add actions and outlets from elements. But when I try Control-dragging, nothing happens. What am I doing wrong, and how do I fix it?
Hmm, well first of all welcome to Stack Overflow! And thanks for asking the question.
Let me know if I have this right - you have two view controllers to the right (linked with segues) of a navigation controller and currently you can navigate to the second from the first using a button at the top right? Then when on the second view controller there's a nav button at the top left with a little arrow by it? And this should take you back to the first. Is that right?
Now on the second view controller you want to create a button that performs an action, but when you right-click-drag (ctrl-drag) onto a .h or .m file nothing happens?
If that's the case I've seen a few reasons for that. You might try:
You need to make a button, a label can only recieve actions, not create them. Read this article on IBOutlet vs IBAction
Restart Xcode (I know, it's lame, but humor me)
Make sure you're dragging (if on the .h file) between "#interface" and "#end"
Make sure you're dragging (if on the .m file) between "#implementation" and "#end"
Do you have a custom view controller class for your second view? If so, select the second view controller in your storyboard and go to the identity inspector. Set the custom class to your custom view controller's class name. Now you will be able to control-drag IBOutlets and IBActions.

viewDidLoad nor init are being called at application start iphone

This is a novice error for sure. In Xcode 4 i have created a Window Based application in which i put a TabBarController as the root controller. When the application starts, the first tab of the tabbar is selected and thus the view corresponding to that tab is shown. The problem is that the View Controller that corresponds to that view in the first tab is not being read, i put some logs in the init, viewWillLoad, and viewDidLoad methods and none of them are being shown. I have linked the controllers correctly, plus Xcode links them automatic if you select that option when creating a new view controller. Here's how it looks like in my project.
This is how it looks like in the tab controller i have linked to the view.nib:
And this is the view, it is linked to the view controller:
And here's the View Connected to the File's Owner Outlet:
Everything looks fine for me so i don't understand why it's not calling those methods in the view controller when the application starts or when the tab corresponding to that view is being selected.
Thanks in advance for the help.
I fixed it. The problem was that the referencing class was UIViewController intead of the view controller i created for it. The answer was to go to MainWindow.xib then select View Controller - Home then go to Identity Inspector and select the View Controller i created for it in this case the HomeViewController.h

How to change the default View Controller that is loaded when app launches?

I have an application, say 'MyApp', which by default loads the view controller 'MyAppViewController' whenever the application launches. Later, I added a new view controller 'NewViewControler' to the project.
I now want the 'NewViewController' to be my default view controller which loads when the app launches.
Please let me know what changes I need to make in my project to achieve this.
Its easy, just:
Open your Storyboard
Click on the View Controller corresponding to the view that you want to be the initial view
Open the Attributes Inspector
Select the "Is Initial View Controller" check box in the View Controller section
Open MainWindow.xib and replace MyAppViewController with NewViewController.
In your app delegate class, replace the property for MyAppViewController with one for NewViewController. Connect NewViewController to its new outlet in Interface Builder.
In application:didFinishLaunchingWithOptions: add NewViewController's view to the window instead of MyAppViewController's view.
Most likely your main NIB file is still set to "MainWindow", check your *-Info.plist file.
If that's the case you can open the MainWindow.xib in Interface Builder. You should see a View Controller item. Bring up the inspector window and change the Class Identity to point to your new class. That should take care of instantiating your class.
As this feels like a "newbie" question (please pardon me if I'm mistaken) I would also highly recommend the following article:
iPhone Programming Fundamentals: Understanding View Controllers
Helped me understand the whole ViewController thing and the IB interaction..
As for me with xcode 4.3.3, all I had to do was simply replace all references of 'MyAppViewController' with 'NewViewController' in the AppDelegate h and m files.
Perhaps all the other steps have been taken out in the newer versions of xcode.
Hope this helps.

iPhone Dev: Tab View controller. Buttons on first view don't show actions in File's Owner

Hey all, I'm really new to iphone development and am getting really frustrated. I know ASP and Javascript and i'm trying to do this objective-c. I took a course on lynda and now i'm trying to hook up a tab view.
I placed buttons on the first view of the tab view controller. I then went into the header file and created the - (IBAction) for the 4 buttons on there and added the function in the implementation file. The problem is when i go back to Interface builder and try to connect the actions to the buttons, they don't show up. I have no actions listed in the File's owner so i can't do anything. Can anyone out there please help! thanks.
damien
You're doing it backwards (IMHO). create the Actions and Outlets (don't forget to save the source), then create the items in IB (note that you have to refresh IB so that it can re-read the source.
In tab view controller app make sure that class of view is your ViewController class and also in MainWindow.xib make sure all tabs are referencing respective ViewController

Using default tab-controller created project... first view controller doesn't respond to addsubview

I created a project using the default tab-controller project. I am using interface builder to edit the .xib file and add images and buttons. I hook them up to the FirstViewController object in interface builder (that I created and set it's class to the same as the code file). I hooked everything up using IBoutlets and IBActions.
Everything was working fine, but then I made some changes in interface builder (added a UILabel) and now a method that is run when clicked (I ran through it with the debugger) has a line that adds a subview to the view controller, and it acts as if it wasn't executed. The method (and code is run through) is executed with no errors (per the debugger) but the view is simply not being added. This happened after I made some change via interface builder.
Now, if I hook-up my button to "Selected First View Controller" by clicking on the appropriate tab and dragging the IBOutlet to the UILabel, that label now has multiple referencing outlets. Now, if I do the same thing for the button, the method (the IBAction) is executed twice but the subview is actually added and displayed. But, I get a memory access error because my IBAction (button) method access a property that stores something. I am guessing this has to do with somehow creating the memory in the First View Controller but trying to access it in the Selected First View Controller? If that makes any sense?
I have no idea why this is happening and why it just the button suddenly stopped working. I tried to explain this problem the best I could, it is sort of confusing. But if anyone has any tips or ideas I would love to hear what you guys think about this problem and how to solve it.
Are you sure the first outlet is actually hooked up. If you name an outlet such that it conflicts with some other property that is set while the nib is loading (via initWithCoder:) it can cause things to not end up being hooked up properly. You can check that by NSLog'ing out the value of the outlets in your awakeFromNib.
It also sounds - and feel free to correct me if I'm mistaken - that you're attaching actions in the view loaded by the tab bar to the tab bar's controller. The two entities are quite different and any data that you wish to access from the view should be referenced from the view's controller rather than the tab bar's controller (which should have a fairly light-weight job in loading and unloading your other view controllers). Similarly, you should not be adding a subview to the view controller, it has no idea about what to do with a subview - you should be using the view controller to add a subview to your view. While it seems like a matter of semantics a view controller is fundamentally different from a view. The former has the job of managing the contents and behaviors of a view and to respond to the view's actions where necessarily while the latter is simply a mechanism for displaying thing son the screen.