UINavigationController tutorial w/ multiple xibs and data sharing
A beginner's tutorial on the UINavigationController. Uses Interface Builder. Has multiple xibs. Shares data between xibs.
You can watch/download the hi-def version on vimeo. To download I believe you must be a vimeo member (it's free), and they hide the link down on the bottom right corner of the page. You can watch the hi-def version directly on vimeo. Here's the low-def version embedded (but probably too low def to be useful).
Thanks again for the tutorial, quite helpful. Quick question - using this method with IB, how would you add a button to open a third level screen (e.g., on the details screen, adding a button to the navigation bar to navigate to a sub-details screen)?
BTW....I totally glossed over/missed making a connection to an IBOutlet in this tutorial. You will get a blank screen unless you make the connection in interface builder.
Hi. I just finished your tutorial. I've been programming for a while. Now, here's my question: Would you reccomend sharing data through the app delegate, or can i just use NSUserDefaults?
launching a view inside a .xib file from a UINavigationItem
Thanks for the good starters on this new platform, it's been very helpful. I have run into one problem that I'm trying to figure out, and I think for those that have actually got an entire application working, this may be a stupid question.
I've got a MainWindow.xib, which contains the "HomeView" Window and a TabBarController with one UITabBar and 4 UITabBarItem on that. When the program loads, the MainWindow.xib is unpacked and displays the HomeView Window and the TabBar at the bottom. When you select the second button on the TabBar, it loads the SecondView.xib file, which has its File's Owner set to a custom class named 'SecondViewController'. The SecondView.xib file contains a View, Label & UINavigationBar at the top. The UINavigationBar contains one UINavigationItem, set to "Add" --> so it displays the Plus symbol from the SDK. So far the interface was built using IB and the switching of the "views" via the TabBar works great.
So then I defined a method inside the SecondViewController.h & .m to handle the interface when the Plus button is pushed. I get it to launch that method, by wiring up the Plus button to the 'addEntry' method I defined in the SecondViewController.h&.m files. Now what I need to do at this point, is launch a new 'AddEntry.xib' file I have which contains another view with some fields on it, that I will be able to enter text into. When i click done, obviously the data would be saved to a .sqlite datbase - that part I've got down. What I'm missing is how to actually launch the view that is embedded inside the 'AddEntry.xib' file so that new view shows up when you click the add button on the navigation bar at the top.
I'm new to the whole concept of iphone development and this hole in my knowledge is not helping me complete the basics of an appication. I've watched many tutorials, but something is missing alright, and I don't know how to make that final connection from the 'addEntry' method defined in the SecondViewController files, and get that method to display the view embedded inside the AddEntry.xib file.
Can someone shed some light on this? Thanks in advance!
Hi Justin,
I'm fairly new to this thing too but I think I might be able to help. I'm trying to explain this from memory so pardon the sparse use of correct terminology.
You can try making AddEntryViewController class files. Make sure the .xib file's owner is of this class. Then in your applicationDelegate, or whichever controller is handling views at the moment (perhaps your second view controller), you can create your view using:
AddEntryViewController *addEntryView = [[AddEntryViewController alloc] initWithNib etc etc];
Then remove your current view from the super view,
then add the new view (addEntryView) as a subview of the superview,
then release addEntryView.
Sorry, I think I made a mistake.
Upon rereading what you wrote, I think there are several ways that you can handle this.
The first is to create multiple full-screen views within the SecondView.xib file. You can then hide your addView until it is needed. This is not the most efficient for memory usage but it sounds like this won't be a problem for your application.
The second is to make a new .xib file as you suggested. You will then need to remove SecondView from the window, and then add AddView to the window. You can have this handled in your application delegate. Check out Nick Myers' downloadable source code in the thread for his multiple .xib tutorial.Here
Third, you can create all your views programmatically and then control adding them and removing them from your windows or views as needed, and not have to worry about loading and retaining a xib into memory when it won't be used. An added benefit of using this method is that you would be able to control the views associated with your SecondView in a viewController and not have to clutter your application delegate. The downside to this is that it takes a lot more coding.
Finally, you can merge the second and the third methods. You can load the SecondView.xib and then programmatically add and remove an addView as needed. This has the drawback of keeping the SecondView.xib in memory when it isn't being used, but it has the advantage of allowing you to control all of your views associated with SecondView in its view controller, and not in the application delegate.
Thank you so much for the videos. I have been painfully rereading Apple's docs, and my three Cocoa books trying to use IB with separate XIB files for my code. I wish I had discovered your videos earlier!
If you ever have the motivation to do another video tutorial, I think many would appreciate a video that shows how one can use IB to make a tab bar controlling separate navigation views, with at least one of those navigation views having a view that contains a small table view and some other graphical element. (For some reason, it was easy for me to have a tab bar with a table view that took the whole screen, but when I wanted to drag a table view to a VIEW to add other elements like buttons, it seemed considerably harder to wire it all together with separate XIB files.)
Great stuff James!
Just wondering if the Interface builder will make me learn thing fast at the beginning and later show that I haven´t actually learned any Obj C at all. Hmm anyone who has switched from code to James approach? Or vice verse?
Thanks squidbot! I find it a lot easier learning from code than videos
You know, generally I'm with you, but one of the problems I've had learning iPhone development is how to leverage Interface Builder to greater extent. I'd have never figured out how to set up a navigation controller, root view in a separate xib and navigation items correctly in IB without watching the videos. I've been doing everything in code up to now, and this video really illuminated how much code I save by learning how to use IB. Anyway, I guess this is a thanks to the original video author, this video has taught me more than all the documentation on IB I've read so far!
Comments, criticisms? I'm particularly interested in criticisms (other than production aspects, as it is free)....surely I did something poorly.
Well, just finished viewing the first part of this tutorial... Technically, the screen quality is not quite there... a little bit too small... But also, we see what you are doing but it seems there is not enough explanation on why you do what you do... How the actions you do in IB relates to the actual UI and obj-c code...
__________________
_____________________________________________
Becoming an iPhone apps developer, one line of code at a time.
My Application: Ultimate Password Manager - A complete password manager integrated with a password generator
Well, the second part of this tutorial is better technically (better screen resolution, better sound, etc.) On the content sidem it's ok but still missing some information for newbie like me...
__________________
_____________________________________________
Becoming an iPhone apps developer, one line of code at a time.
My Application: Ultimate Password Manager - A complete password manager integrated with a password generator
Comments, criticisms? I'm particularly interested in criticisms (other than production aspects, as it is free)....surely I did something poorly.
Great tutorial James. Can you tell me how to instead of the NavItem Button directing the control of the view change to have a Round Rect Button do the same thing? I'm rather new at this so if you do have help for me please tell me exactly where I need to change and/or modify the code. Thanks.
Great tutorial James. Can you tell me how to instead of the NavItem Button directing the control of the view change to have a Round Rect Button do the same thing? I'm rather new at this so if you do have help for me please tell me exactly where I need to change and/or modify the code. Thanks.
Hi, I am new to iPhone dev. I am creating an application which load the first view with two buttons, clicking on one button direct you to the second view xib file which has the navigation bar with back button, and a search bar. clicking on search bar will lead us to another view which contains the search results.
my question is although I dont need navigation bar in the first view (view with only two regular buttons) should I set it as the root view controller for navigation controller?
I watched the videos and found that they really helped me make sense of what I was trying to do.
At one point, James mentions that he's not sharing data between the xib's the 'correct' way... but doesn't elaborate. So, my question is, what *is* the correct way to do it?
In my app, I'm switching views similar to the example, but I have a table on my secondary view. I want to select something in the table, and have that selected value show up back on my first xib.
Right now, since I was trying to avoid what James did in the tutorial, was use my reference to my second view controller, and just get the value from the NSArray I have backing the table.
I watched the videos and found that they really helped me make sense of what I was trying to do.
At one point, James mentions that he's not sharing data between the xib's the 'correct' way... but doesn't elaborate. So, my question is, what *is* the correct way to do it?
In my app, I'm switching views similar to the example, but I have a table on my secondary view. I want to select something in the table, and have that selected value show up back on my first xib.
Right now, since I was trying to avoid what James did in the tutorial, was use my reference to my second view controller, and just get the value from the NSArray I have backing the table.
Any tips would be great ,
thanks!
Realize these videos are *way* old...
The "correct way" I'm afraid is exactly like you would do it in your day job - if your a programmer - or how Stanford presents it. Basically, you don't want your application delegate to be tied to any one view. Moreover, you don't wanna junk up your delegate. The "correct" way, IMHO is exactly like Stanford says to do it, individual view controllers are responsible for its data, if it shares data with other view controllers, simply set the other controller's variables. It's a pain in the butt, but it keeps data more cleanly separated. Hopefully I'll have some time to do another tutorial. BTW, check out my latest tutorials on Vimeo. They don't have sound because they match written steps in my book, but I share data the "correct" way.
The "correct way" I'm afraid is exactly like you would do it in your day job - if your a programmer - or how Stanford presents it. Basically, you don't want your application delegate to be tied to any one view. Moreover, you don't wanna junk up your delegate. The "correct" way, IMHO is exactly like Stanford says to do it, individual view controllers are responsible for its data, if it shares data with other view controllers, simply set the other controller's variables. It's a pain in the butt, but it keeps data more cleanly separated. Hopefully I'll have some time to do another tutorial. BTW, check out my latest tutorials on Vimeo. They don't have sound because they match written steps in my book, but I share data the "correct" way.
BTW...this tutorial is outdated now that 3.0 is out