"Beginning iPhone development" Chap 9 Navigation??? Really not working - iphone

I have been in at the deep end for a week now learning from every source i can, resisting posting as i know how irritating it is for n00bs asking stupid questions on your expert subject.
However... after hours spent on this chapter 9 or Beginning iPhone Development book regarding a navigation app, i have surrendered and posted here.
what i have done so far is posted here (https://files.me.com/taylorsuk/jr6xj1)
i have got to two screens and then it will not go any further?
following the instructions implicitly i am unable to connect the NavController to the RootViewController? in the class option - the option isn't there.
trying to learn so would be great if you could guide me through this simple thing - all code should be there!
This is needed for a little project that i am doing for someone (which i will admit i am out of my depth, however if there is anyone who is willing to help me on a one to one basis i could send you some money as i am going to get paid for the job.)
Cheers all
Simon

As Adam points out, you can create a navigation controller programmatically but you can also do it in Interface Builder to cut down on code. I prefer this, it's analogous to instantiating the controller and setting some of its basic properties.
In Xcode, double click on your MainWindow.xib file. This will open it in Interface Builder.
In the library, find "Navigation Controller" among the objects list. It should be a black arrow tab pointing left on an orange background. Drag it into the tray or into the window representing MainWindow.
Click on the view of the navigation controller (the view should be highlighted). In the inspector, give a valid xib file name in the NIB name (you can leave off .xib"). This sets the root view. You might need to create a new UIViewController class in XCode with its xib just for the purpose of this exercise.
Again in the inspector, find "class name" and enter the name of the UIViewController class associated with your xib file from 3.
Back in XCode, in the header of your app delegate file, declare something like IBOutlet UINavigationController nc. You can make an associated property and synthesise it if you like.
Head back to Interface Builder, and in the tray right-click (i.e. CTRL+click) the navigation controller. Drag from "new referencing outlet" over to "File's owner". You should be able to choose nc from the list of outlets.
And that's that. Compile and run.
I've probably missed something. Anyone reading this, please feel free to correctly correct my answer. There are plenty of tutorials on YouTube that explain this far better than I did.

The link you gave reports it is no longer available? Anyway, you usually create the navigation controller with the method initWithRootViewController...
UIViewController myRootVC = blah blah...
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:myRootVC];
[myRootVC release];
instead of creating then assigning a root view controller.

Related

iPhone App Development - Few beginner questions

I've been tasked with creating an app, but I have zero experience with iOS development. I have general programming knowledge, particularly with Java, JavaScript and PHP (I'm more a web developer than a programmer). I have dabbled with C, Xcode and various other languages and IDEs in the past, but I remember very little.
I've been following Apple's Developer Library tutorials, and I'm at around the Language stage, where I'm come to a grinding halt. While I slowly progress through learning the basics of Objective-C, there are a few things I'm very confused about regarding development in Xcode that various tutorials seem to completely skip over or just imply that you know what to do, or some just stop right before the part I'm having trouble with.
1) Storyboard - yes or no?
Is it better to start with an empty application and work with the
files or create a template (in my case a tabbed application) and work
with the storyboard?
2) If using a storyboard, do I still need to have a .xib?
Are the User Interfaces more like global templates that the view controllers implement?
If I wanted a different layout for each tab of my app, would I create a .xib for each tab, or just edit the controllers in the storyboard? Am I correct in understanding that the storyboard can have multiple instances/relationships of the same controller, in which case having .xib's would make more sense?
3) If using storyboard, where do the implementation and source files come from?
This is probably a stupid question. I know you can just add them via File -> New, but I don't know how to associate those files with a view controller. Is there a way to have the files created automatically when adding a controller into the storyboard?
Since you're just starting, you should use Storyboards because it lets you link different view controllers(pages on your app, essentially) visually and outside of code. For example, you can link your UITabbedViewController (the part that manages the content of the other tabs) to the pages that represent the content of the different tabs. Basically, your storyboard would have the tabbed view controller in a parent-child relationship with the sub-controllers. You would have one instance of each -- the tabbed view controller, managing an instance of each of the tabs' content and controller. This is the same regardless of Storyboard or xib, but you can connect this more easily in the storyboard.
You can still use a .xib(nib) file for stuff like Custom Table cells or in cases where you want to separate a view element or controller from a storyboard where there are other constraints.
In the storyboard, you subclass the controller class on the sidebar in the visual editor by entering your subclass of say UITabbedViewController in 'Custom Class'. In your file associated with 'MyTabbedController', you implement your stuff.
Great book:
http://www.barnesandnoble.com/w/beginning-ios-6-development-david-mark/1113216077?ean=9781430245124
Good luck!
Storyboards can be appropriate for small applications, where you have ten or twenty screens. When your app contains more than that, you will just get lost in storyboard schema, where all your view controllers will visually look the same.
I prefer not to use storyboard, just separate xib files for each controller.
If you use storyboard, you can create xib files for other parts of application that is not related to SB, and view controllers that is involved in SB has their interface stored in SB, meaning you will have to design them in there, in this one huge storyboard file. I find this very uncomfortable.
As you are new to IB, I would like recommend you to take a look at Auto layout. There is no magic anymore :)
To answer on a point-by-point basis:
I typically use the "Single View" template. It provides everything you need for your first view and can take it from there. It's a clean slate but it already has that first view which will be exactly the same code in 99% of the applications you make.
No, the storyboard file is your xib. You used to have to make a new xib for each new layout, but then Apple introduced storyboards. A storyboard is basically all of your xib's in one file. Rather than make a new xib, drag a new ViewController object onto the document. You typically only have 1 storyboard file or 2 if you want to support both iPhone and iPad layouts.
I don't think you can have it create your source files automatically, but its fairly easy to connect them manually.
Select the ViewController that you want to connect to your source files by clicking on that black bar beneath it. Then go to the bar on the side and go to this panel:
There you enter the name of your custom ViewController subclass where I have put "MyViewController". Hope that helps!

Why do I need to control-drag some kind of UI-widgets into yellow sphere representing the view controller?

I'm a newbie and am learning the iOS-dev through reading the official tutorial Your First iOS App .
While I don't quite understand the behind meaning of control-dragging the text-filed into the view controller represented by a yellow sphere icon.
I mean it's easy to follow but hard to know the behind reason.
I checked the code after the operation and found nothing is added.
Could someone help to figure out why? TIA.
well, I posted some description the tutorial says to help my question could be understood exactly.
"In the view, Control-drag from the text field to the yellow sphere in the scene dock (the yellow sphere
represents the view controller object).
When you release the Control-drag, you should see something like this:
[sorry image's not allowed to upload for now].
Select delegate in the Outlets section of the translucent panel that appears."
The best way to think about it is, that what you create in Interface Builder has to be linked to your code. A .xib file is really just a complicated XML document. By linking the .xib to the controller it will write code in the XML linking it to the code. otherwise when the .xib and your source code is compiled, the compiler won't link the two entities.
Hope this helps
By control-dragging, we create connections between objects/views in the nib (.xib) file to an action (IBAction) or an outlet (IBOutlet) in View Controller.
Nib files (.xib) has responsibility to establish all connections (actions and outlets).
There will be nothing added to your Controller code to reflect the connections.

Loading an external view controller, from a button click on another view controller

Im officially getting annoyed with objective-c and xcode now. Programming in PHP and Java is so much easier haha.
Anyway I could do with some help.
I have created a tab bar application with three tab items for the iPhone, on one of the items it loads a nib named mapView, this contains a button that I want to use to load up another nib named OverlayViewController.
Ive been following this tutorial this tutorial
to create a camera overlay. I understand how it works, but I don't understand how to run the view controller from a button or direct from the tab bar. I can only get the overlay to work if I load it like in the example on application launch in the app delegate. If I try and load it from the tab bar item I just get a grey screen, looks like the blank view controller is loaded and the code hasnt been run to show the overlay.
If anyone can suggest how I would go about loading the overlay from the button click, or even direct from the tab bar item I would be really grateful.
Thanks Alex
p.s. Heres the link to the project if you wish to view the files
#AlexApps I took a look through your project and have several pieces of feedback.
I think before you get too into trying to get the OverlayViewController working you should back up a bit and give some of the Apple docs a read, especially the View Controller Programming Guide. The Apple docs are for the most part well written and should help you gain a better understanding of views and view controllers than what is evident in your code.
Another suggestion is to grab some of the freely available source code from a book such as Beginning iPhone Programming which has a good example of how to lay out a tabBarController based app. I am sure there are other good samples out there that will show you how to organize your views and view controllers to load them into the different tabs.
I think that if you follow this advice that by the time you have restructured your app by what you learn you will have less problem doing what you are trying to do.
BTW, you may want to consider using a NavigationViewController for what you are trying to do with loading the OverlayViewController with a button press but take a few hours, slow down and do some focused reading. It will make a world of difference. Then if you have more specific questions, Stack Overflow (and Google) are your friends.
One last tip, you do not need to put IBOutlet in your instance variables AND in your properties, just one or the other, and really, you don't even need the instance variables at all anymore. I usually just use properties for everything.
I have done it with storyboard and the solution is to create a segue between the tab bar view controller and the view controller you want to load, then you have to put an identifier to that segue. Finally, in the method called when the button is pressed you have to put this:
[self performSegueWithIdentifier:#"segueId" sender:sender];
I have done it right now and it works perfect!
First import uiviewcontroller class in frist page like below:
#import "page2viewcontroller.h"
on button click event code below:
page2viewcontroller *page2 =[page2viewcontroller alloc] ;
[self presentModalViewController:page2 animated:NO];
[page2 release];
after back page2viewcontroller to page1viewcontroller same as like below:
#import "page1viewcontroller.h"
on backbutton click event code below:
page1viewcontroller *page1 =[page2viewcontroller alloc] ;
[self presentModalViewController:page1 animated:NO];
[page1 release];
That's all....!

Connecting outlets in Window Based App, Objective-C

I'm reading through a beginners iPhone text book and just finished writing all the code for a route tracker app that uses Map Kit and Core Location. I have the app running with no errors on my iphone 4 device but when I tried interacting I realized that none of my IBOutlets were connected to anything. When I referenced the beginning of the tutorial in the text, all it says is to "connect the appropriate outlets".
Here's why this usually simple task has me confused. The tutorial says to create a Window Based Project, so there is no ViewController. Then, in Interface Builder, the view is built in MainWindow.xib. The only IBOutlets of the project are located in Controller.h / Controller.m files that you create and which contain mostly all of the code for the app.
I usually ctrl-drag from File's Owner to the UI in Interface Builder, but in this project there seems to be no way for me to access the IBOutlets in Controller.h / .m from the MainWindow.xib file.
I'm frustrated because it seems like this should be such an easy fix but I'm totally stumped.. any help is really appreciated. Thanks
As I see it, you have two choices:
Add the outlets to the app delegate. This is probably not the best plan.
Put an instance of your controller class in the .xib file.
I think option 2 is what you want. If you check the Controllers section of the library, you'll see a component called "Object". Drag one of those into your xib, then inspect it. Select the info pane in the inspector (the circle with a white i in it) and change the Class to the name of your controller class.

Who's responsible for creating the MainViewController instance in the iPhone NavBar example

I'm exploring the NavBar example from the iPhone dev center.
More specifically, I'm trying to understand where the MainViewController object is instantiated and I simply can't find where this is happening.
The only references to the MainViewController class is in AppDelegate, where there is a forward declaration and #import. However, removing these sentences doesn't affect the program whatsoever.
The object is probably created in the nib file, but again, I can't find it.
I really like the Xcode development environment. What I don't like, however, is that there are some many different places where objects can be created/connected that it makes it really hard to understand somebody else's code.
Any help will be appreciated.
If you look in the MainWIndow.xib file and click the disclosure triangle next to "Navigation Controller", you'll see an instance of MainViewController. This was created by dragging a "View Controller" object from the Library and then changing its Class.