Custom Class Controller for iPhone.

Discussion in 'iOS Programming' started by larswik, Jul 15, 2011.

  1. larswik macrumors 68000

    Sep 8, 2006
    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?



  2. Sydde macrumors 68020


    Aug 17, 2009
    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 <-- read
  3. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    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.
  4. larswik thread starter macrumors 68000

    Sep 8, 2006
    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.


  5. jiminaus, Jul 15, 2011
    Last edited: Jul 15, 2011

    jiminaus macrumors 65816


    Dec 16, 2010
    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.

    1. Create a New Project. Make sure you select a Single View Application for the iPhone.
    2. Name your project "RandomNumber".
    3. Expand your project and the RandomNumber group in the project navigator on the right.
    4. Click on RandomNumberViewController.xib to open it.
    5. Go into the Editor menu, then select Show Document Structure Area.
    6. Go into the View menu, then the Utilties submenu, then select Show Object Library.
    7. Drag a label from the object library to the view window.
    8. Drag two rounded rect buttons from the object library to the view window.
    9. Double-click the top button, type "Seed Random Number", and then hit return.
    10. Double-click the bottom button, type "Generate Random Number", and then hit return.
    11. Go into the View menu, then the Assistant Editor, then select Show Assistant Editor.
    12. 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.
    13. Control drag the label onto the assistant editor just above @end. When "Insert Outlet or Outlet Collection" appears, release the mouse button.
    14. In the pop over, name the new outlet "randNumber", then click Connect.
    15. Control drag the top button onto the assistant editor just above @end. When "Insert Outlet, Action, or Outlet Collection" appears, release the mouse button.
    16. In the pop over, change the connection type to action, name the new action "seed", then click Connect.
    17. 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.

Share This Page