Connecting outlets in Window Based App, Objective-C - iphone

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.

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!

Where is the main viewcontroller that is displayed in your iPhone app specified and told to become visible?

I'm working on an iPhone app again and I'd like to setup one view to be seen before another. I've added the view to my window and have specified the viewcontroller code that I'd like to use, but I can't find where on earth xcode specifies which viewcontroller in the window is displayed first. I'm sure it must be something obvious. I thought that the appdelegate seemed logical, but I don't see it displayed there. Any help? :(
Usually it is in the UIApplication setup code. Also try looking in your xib file as to the linkages setup there!!!
Without seeing more code or your setup, this is the best I can do!!!
As you can open the MainWindow.xib file you can see the part on the windows that specifies that from which viewcontroller it is gona load. (Like "Loaded From "RootViewController""). From the inspector of the window there is an option called NIB Name that gives you a list of view controllers available in your project.Changing these will change the setting of from which view controller you want to begin your app.
Hope this will help you.

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

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.

Loading a View from IB

This should be a pretty easy fix, but I haven't figured out from reading the Apple documentation or other SO questions how to do this simple switch from creating my Interface programmatically to using Interface Builder.
I am basing my code around this framework:
http://www.pushplay.net/blog_detail.php?id=27
The only difference is that, where each View is programmatically created (View01.m, View02.m) in ViewDidLoad, I instead want to import from a nib (while still using this framework) for each view (each view has a unique IB design).
Thanks for the help.
Think of IB as an Object Creator and not a code generator. That really helps. What IB does is actually create instances of objects as they are dragged on to the desktop/view/XIB window. It then allows you to start creating various connections (with a control drag on the mouse) from one object to another object. You then instantiate the entire XIB by unarchiving it from your bundle. This is highly automatic and reading up on UIViewController should move you along a bit. Look at:
initWithNibName:bundle:
You basically have two types of connections:
Outlet: This is how you teach one object about the existence of another object. For example, you might have a controller object that needs access to a button. You create an outlet (either in XCode Text Edit in the controller.h file/property area or in IB by adding an outlet) in your controller and then control-click-drag from the outlet to the button.
Actions: This is how you trigger an event on one object to call a method on another object. Actions will have a prototype of:
- (IBAction) someMethod:(id) sender;
I think the ":(id) sender" is optional if your method does not need a link to the object causing the event.
Within IB, you can arrange objects and set various attributes like size, color, position, target/actions, user interactions, Files Owner...
That brings me to files owner. Big concept here. It tends to be the Controller that loads the NIB (OK: I have a custom window controller I have used for over 15 years but Apple has a really good one UIViewController that does all sorts of goodness.) and acts as a proxy in IB. It is not actually instantiated in IB but it will be when you alloc and request it to load the NIB (XIB files are XML files that are turned into NIB files by the compile process)

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.