PDA

View Full Version : Custom Class Controller for iPhone.




larswik
Jul 15, 2011, 01:32 PM
I started to read the the Objc-C for Absolute Beginners book and got to the first iPhone programming part. It was designed for Xcode 3 and I am trying to implement it for 4. It says to type in the instance variables and the 2 methods, then open up IB. I jumped to IB first in the book and built the buttons and labels because I know I can just control click drag the IBOutlets and IBActions to the .h from the .xib

The book told me to to call the project 'RandomNumber' and to write out the methods in the 'RandomNumberViewController.m' where we would link them. But when I control drag buttons to the window in the RNVC .xib in the interface builder they fill the whole window area, weird thing happen? the Label snaps all the way to the left too? The program also created the RandomNumberAppDelegate.h and .m files and I can see the standard Window in the .xib file.

Is the book out dated? Should I be using the AppDelegate instead of the custom class I created 'RandomNumberViewControler' to link all the labels and buttons, or does it matter?

When I click on the .xib file in Xcode4 I can see the standard iPhone window to work with. So why is the book telling me to do everything in a custom class?

Thanks,

-Lars

-Lars



Sydde
Jul 15, 2011, 02:21 PM
...when I control drag buttons to the window in the RNVC .xib in the interface builder they fill the whole window area, weird thing happen?

Control-drag buttons to the window? Control-drag is for connecting outlets or actions, and should not be done out of the library (if I understand correctly what you are saying).

XCode 4 IB Overview (http://developer.apple.com/library/mac/ipad/#documentation/IDEs/Conceptual/Xcode4TransitionGuide/InterfaceBuilder/InterfaceBuilder.html) <-- read

dejo
Jul 15, 2011, 03:02 PM
Since the book is written based on Xcode 3, I would recommend downloading and installing it (separate from Xcode 4) and using that as you follow along with the book. There is nothing that spectacular in Xcode 4 that a beginner should need it and you'll find most learning resources out there are still based on Xcode 3.

larswik
Jul 15, 2011, 03:05 PM
Hi Sydde,

I am going through the tutorial 1 more time from the book. I have only used IB in Xcode4 and not 3. The book is written to use Xc3. Also I have never made an iPhone app and this book is just covering that topic now. So I am getting a little lost in 2 areas and I think that is causing my problems.

I am going to run through it slowly. When learning Cocoa and selecting Cocoa Application I only had 1 .xib file to work with, MainWindow.xib. in the iPhone app I have 2 the MainWindow.xib and the newly created one when I created my AppController .h and .m file, I also have a AppController.xib file. I did not know you could have more then 1 .xib file.

Thanks,

-Lars

jiminaus
Jul 15, 2011, 09:10 PM
I started to read the the Objc-C for Absolute Beginners book and got to the first iPhone programming part.

In an iOS app, the app delegate plays a lesser role than you probably use to. It would be correct to have the outlets and actions in the view controller, not the app delegate.

Here's the instructions for starting section 10.4 in XCode 4.


Create a New Project. Make sure you select a Single View Application for the iPhone.
Name your project "RandomNumber".
Expand your project and the RandomNumber group in the project navigator on the right.
Click on RandomNumberViewController.xib to open it.
Go into the Editor menu, then select Show Document Structure Area.
Go into the View menu, then the Utilties submenu, then select Show Object Library.
Drag a label from the object library to the view window.
Drag two rounded rect buttons from the object library to the view window.
Double-click the top button, type "Seed Random Number", and then hit return.
Double-click the bottom button, type "Generate Random Number", and then hit return.
Go into the View menu, then the Assistant Editor, then select Show Assistant Editor.
If RandomNumberViewController.h is not showing in the assistant editor, in the navigation bar above the assistant editor, click the popup button to right of the arrows, then select Automatic.
Control drag the label onto the assistant editor just above @end. When "Insert Outlet or Outlet Collection" appears, release the mouse button.
In the pop over, name the new outlet "randNumber", then click Connect.
Control drag the top button onto the assistant editor just above @end. When "Insert Outlet, Action, or Outlet Collection" appears, release the mouse button.
In the pop over, change the connection type to action, name the new action "seed", then click Connect.
Repeat for the bottom button to connect to a new action called "generate".


I'm using the XCode 4.2 beta, so sorry if some of this doesn't match up exactly.