I don't understand this (MyViewController)

Discussion in 'iOS Programming' started by iPhoneAppDev, Sep 27, 2009.

  1. iPhoneAppDev macrumors newbie

    Joined:
    Sep 27, 2009
    #1
    I am going to "Your First iPhone Application" tutorial on Apple's web site. Can someone explain me this line, PLEASE:

    MyViewController *myViewController;

    Why is this there? What does this line mean: MyViewController *myViewController; ???

    Here is a full code:

    Code:
    #import <UIKit/UIKit.h>
     
    @class MyViewController;
     
    @interface HelloWorldAppDelegate : NSObject <UIApplicationDelegate> {
        UIWindow *window;
        MyViewController *myViewController;
    }
     
    @property (nonatomic, retain) IBOutlet UIWindow *window;
    @property (nonatomic, retain) MyViewController *myViewController;
     
    @end
    
    http://developer.apple.com/iphone/l...hone101/Articles/03_AddingViewController.html
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    That line creates an instance variable, named myViewController, of class MyViewController.
     
  3. PatrickCocoa macrumors 6502a

    Joined:
    Dec 2, 2008
    #3
    As the previous poster said:
    MyViewController *myViewController;

    creates an instance of the class MyViewController - that instance is named myViewController.

    Note that the class starts with a capital "M" and the instance starts with a lower-case "m". That's not required but is a convention so that you can look at your code and if a name starts with a capital letter, you know that it's a class name.

    Similarly (as further information for you), the preceding line:
    UIWindow *window;

    creates an instance of the UIWindow class - that instance is named window.

    If you don't know what an instance or a class is, you may want to get that straightened out before continuing.
     
  4. iPhoneAppDev thread starter macrumors newbie

    Joined:
    Sep 27, 2009
    #4
    Guys, I understand what you are saying and I thank you. But, what I don't understand is:

    I don't see what the purpose of that line is? If you take a look at this link: http://developer.apple.com/iphone/l...hone101/Articles/03_AddingViewController.html you will notice that everything else was explained 100% well except that single line. Why in the God's name do we need this line? I understand it line creates an instance variable, named myViewController, of class MyViewController but, what does it create in my application? I don't get it.

    @property (nonatomic, retain) MyViewController *myViewController;


    What does the line "do" to "HelloWorld" application?
     
  5. drivefast macrumors regular

    Joined:
    Mar 13, 2008
    #5
    in the header files (.h files) usually the lines dont "do" anything. they are merely declaration of variables, telling the compiler how it is supposed to interpret them. so at compile time, that line tells the *compiler* something like "i want to introduce to you the variable myViewController, which is of type MyViewController, and the variable window, which is of type UIWindow". if you want to see what the program *does* with these variables, look into the corresponding .m file.
     
  6. North Bronson macrumors 6502

    Joined:
    Oct 31, 2007
    Location:
    San José
    #6
    Think of the header file like a table of contents for a scholarly text. It kind of gives you a road map and an outline of where the text itself is going to take you and it can be helpful to switch back and forth between them.
     
  7. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    Try this: comment out the line and rebuild and see what happens. Then you'll start to see how important it is.

    Really, if you don't understand the importance of instance variables, you should spend some time learning about their usefulness. They are a key concept in the OOP world.
     
  8. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #8
    Do you know what a variable is?

    Do you know what the address of a variable is?

    Do you know what the name of a variable is?

    Do you know what a C data structure is?

    Do you know what a variable type declaration is?

    Do you know what a compiler is?

    Answer "no" to any of the above, and you need to go back to studying some basic concepts.
     

Share This Page