Beginning iphone sdk programming

Discussion in 'iOS Programming' started by mymac1, Jul 28, 2009.

  1. mymac1 macrumors newbie

    Jul 28, 2009
    Hello all,
    I have bought a book on iPhone SDK and the very first tutorial doesnt even work (have since read review on it which turned out not so good) so have ditched it and decided to go it alone with tutorials on the internet.

    I have just joined this forum and I have a couple of questions just to my feet wet :D

    Regarding the drawrect method, there is one of these in the downloaded example code which works, however I cannot see it being called anywhere in the program. Am I correct in assuming this is automatically called, and if so, what object/function is calling it?

    There is an object declared of UIWindow *testwindow.

    I assume that the 'frame' is declared within this object, is this set to a default size?

    I have seen the method '(void)dealloc' being specified twice, one for each of the two implementations in the example code.
    I am wondering why two are needed and also are these called automatically too?

    Thanks in advance.
  2. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    How much Objective-C do you know? If very little, learn that first and then come back and learn more about iPhone development.
  3. mymac1 thread starter macrumors newbie

    Jul 28, 2009
    OK Thanks,

    What platform is best to learn Objective-C if I am to avoid iphone?
    Preferably one which has UIKit.
  4. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    You can learn Objective-C using Mac OS X. It doesn't have UIKit, but does have AppKit / Cocoa, which is a sibling to UIKit / Cocoa Touch.

    Why do you need to avoid the iPhone?

    Also, I'm curious. Which book where you reading that even the very first tutorial didn't work?
  5. moral-hazard macrumors regular

    Jul 27, 2009
    Palo Alto, CA
    Yes - it gets called when the view first draws itself in the window.
    You will never call drawRect directly.
    If you DO need your view to redraw because you have change something, the correct syntax would be:
    [view setNeedsDisplay]
    This tells the view that it needs to redraw itself, and will (ultimately) lead to drawRect being called again.

    Not sure entirely what you mean here. Every UIView has a frame (CGRect). Generally if you want to instantiate a view of a particular size, you could do something like:
    [[UIView alloc]initWithFrame:CGRectMake(x,y,width,height)];
    This initializes the view with a rectangular frame beginning at pixel with coordinates (x,y) of width "width" and height "height".

    I believe the window's size is set automatically, since iPhone applications always fill the full screen and the screen size never changes. Your app should only have one window, in which you can can have multiple views & viewcontrollers.

    You need to learn about how Obj-C does memory management. You need to "clean up your own mess" in a way.

    -dealloc is NEVER called directly, much like drawRect. Each objective C object has a retain count - basically an integer value. The invariant to follow here is that any time some entity is "using" that object for some purpose, the retain cound should increase by one. When the object is done using it, it should decrease by one. IF you follow this paradigm, there will be a point during execution where the object's retain count is 0. At this point, dealloc will be called and the object released. Note that for many objects, this will not happen til the end of execution (this would be the case with the view in a single view application).

    For custom object classes, you DO need to make sure to modify -dealloc. It's important to make sure that any objects "associated" with the class (most commonly retained properties) get released at this point.

    [object retain]   //increment the retain count (because you need to use object for something)
    [object release] //decrement the retain count (because you are done using it)
  6. firewood macrumors 604

    Jul 29, 2003
    Silicon Valley
    A few books were written before the SDK was finalized. While useful perhaps as a reference for advanced programmers, I recommend beginners ignore these early books.

    One basic theme of iPhone programming is that you don't call routines in many cases. You make a bunch of subroutines (methods) that wait for the OS to call these routines on its own good time. Then do nothing except wait to be called.

    drawRect is a good example.
  7. mymac1 thread starter macrumors newbie

    Jul 28, 2009
    Thanks your response guys, I really appreciate it.

    The code I have declares a UIView object called say 'testview' and looks something like:
    UIView *testview = [[testview alloc] initWithFrame:[testwindow frame]];
    the testwindow was declared previously as UIwindow type called *testwindow

    I will go and get into revising Objective-C memory management, thanks again for your help.

  8. mymac1 thread starter macrumors newbie

    Jul 28, 2009
    methods called but not in program

    I am wondering about three methods in an example program which are necessary but dont seem to be called from within the program:


    I have read that the layoutsubviews is invoked by a 'layoutifneeded' method, however there isnt one called from within the program. Is there anything else which would invoke the 'layoutSubviews' automatically?

    The initWithCoder is a bit of a mystery, the first line of the method looks like:
    -(id)initWithCoder: (NSCoder*)coder

    Not sure where the 'coder' parameter is coming from, also is this automatically invoked?

    then there is the 'layerclass'
    I believe it is there to substitute a different layer class, however this doesnt get called within the program, is that too automatic?


Share This Page