My Blog List

Friday, June 3, 2011

Reading Notes: ViewController Initialized in this method

p 168 Goldstein says that the view controller is intialized with the applicationDidFinishLaunchingWithOptions method

That would seem to indicate the bold part below

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {   
   
      [self.window addSubview:viewController.view];
    [self.window makeKeyAndVisible];

    return YES;
}

...

Interpreting this...


[self.window addSubview:viewController.view];




the window object is receiving the message to addSubview. The parameter passed to the 'addSubview' method is viewController.view

For this paramter, the frameworks code, uses dot notation -  viewController is an object defined in the (DeepThoughts)AppDelegate.h generated code


The ".view" part though.....


Well.... when I look at the declartion addSubview in UIView.h, I see that there is a reference to something called 'view'


- (void)addSubview:(UIView *)view;


[self.window addSubview:viewController.view];

.... I don't understand this use of dot notation
.... Will need to return to this
 


 
 

Reading Notes: Is it Magic? Is it happening in the framework code? Initialization of Window

 Exactly how 'window' is being initialized is bugging me. I'm not seeing an obvious place where the object is instantiated. 

Maybe because it's happening in the framework?

Or maybe because all the business of @synthesize and @property doing the accessing for me?

The author says that DeepThoughtsAppDelegate.m is initializing window. Is initialization happening by using 
·      @implentation directive?
o    Or
·      with the @synthesize window ?

·      Searched on ‘alloc’ and ‘init’ in the code. I don’t see init

·      Come back to this

·       getting used. Is there some magic that @synthesize does?

The @synthesize directive automatically generates the setters and getters for us, so all we have to implement for this class is the dealloc method. ..Accessors will only be generated if they don't already exist, so feel free to specify @synthesize for a property, then implement your custom getter or setter if you want. The compiler will fill in whichever method is missing.
…. Snip from the wikipedia online tutorial
a mutator method is a method used to control changes to a variable. The mutator method, sometimes called a "setter", is most often used in object-oriented programming, in keeping with the principle of encapsulation. According to this principle, member variables of a class are made private to hide and protect them from other code, and can only be modified by a public member function (the mutator method), which takes the desired new value as a parameter, optionally validates it, and modifies the private member variable.
Often a "setter" is accompanied by a "getter" (also known as an accessor), which returns the value of the private member variable…..


Objective-C Online Tutorials

Here are some of the places where I look things up online. There are a number of things I find in the pile of books I have that are NOT in these online tutorials, but these are handy when I'm trying to remember basic syntax.

Reading Notes: Protocol Syntax (<, >) and Initialization




page 166 Goldstein goes through app initialization

I remembered what the < and > are for! They refer to protocol's. Protocols are a new concept for me. I keep thinking of C3PO in Star Wars telling Luke that he's a protocol droid. Which is REALLY not helpful.

A search on the syntax characters plus the word 'protocol' turned up the following in

http://en.wikipedia.org/wiki/Objective-C

- (void)setMyValue:(id)foo;
In the above statement, foo may be of any class.
- (void)setMyValue:(id<aProtocol>)foo;
In the above statement, foo may still be an instance of any class, but the class must conform to the aProtocol protocol.

.....

So when I look at the initialization code for the DeepThoughtsAppDelegate.h file and I see the line

@interface DeepThoughtsAppDelegate:NSObject <UIApplicationDelegate>

that must mean that MY AppDelegate class (DeepThoughtsAppDelegate), that I'm setting up here is 

* A subclass of NSObject AND
* Will conform to the UIApplicationDelegate protocol


Another basic syntax link
http://en.wikibooks.org/wiki/Objective-C_Programming/syntax
...
I NEED TO REMEMBER to double check on the use of "@class", which is not in the current book. 
I THINK it just means that I can use the DeepThoughtsViewController class from here

//  DeepThoughtsAppDelegate.h
//  DeepThoughts
//
//  Created by xxxxx on 5/30/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>

@class DeepThoughtsViewController;

@interface DeepThoughtsAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    DeepThoughtsViewController *viewController;
}
Other things I'm interpreting this code as doing is
declaring two instance variables window and viewController that will be part of the DeepThoughtsAppDelegate class. The window ivar is of type UIWindow and the viewController ivar inheirits from DeepThoughtsViewController (probably why I need to put that @class statement in)
* * *
A little less mystery about the code now



Reading Notes: Main Window Nib File, Interface Objects


Goldstein's iPad Apps Development Book page 164

Interface Builder has been a bit of a challenge to me. I'm working to start over with it and get a better idea of how to draw the connections between the objects I put into the View window and the various Main Window interface objects.

So what is in the Main Window?

  1. File's Owner, A Proxy Object
    1. class UIApplication
    2. created by UIApplicationMain object before the nib file is loaded
  2. First Responder, Proxy Object
    1. First entry in application responder chain
    2. points to object user currently interacting with
  3. Window
  4. Instance of (DeepThoughts*)AppDelegate set to be the app delegate
    1. Code that restores app after launch to it's previous state
    2. Performs any custom application initialization
  5. Instance of (DeepThoughts)ViewController set to be apps view controller
    1. Where I put code to control the view of my apps (ch 10)

* DeepThoughts in current book tutorial


Thursday, June 2, 2011

Reviewing Syntax in framework generated code

Another place to review this is chapter 9 , page 247 in Stevenson's Cocoa and Objective-C, Up and Running
 
Interpreting framework generated code

* application didFinishLaunchingWithOptions is one of the key methods in the MVC design pattern. I thinnnnnkkk it’s the Controller part

* application is a method, it’s passing a parm an object named UIApplication 

* NSDictionary is a paramater being passed to the method application didFinishLaunchingWithOptions

* It returns a Boolean value

I don’t quite recall why/how the signifigence of packing all the method calls together, but I remember that there’s something similar done in C++, so look back there

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  
  


The “window” object receives the “addSubview” message

    [self.window addSubview:viewController.view];

    [self.window makeKeyAndVisible];

    return YES;
}


Searched in help on “addSubView”

Ended up in UIView class reference

The UIView class defines a rectangular area on the screen and the interfaces for managing the content in that area. At runtime, a view object handles the rendering of any content in its area and also handles any interactions with that content. The UIView class itself provides basic behavior for filling its rectangular area with a background color. More sophisticated content can be presented by subclassing UIView and implementing the necessary drawing and event-handling code yourself. The UIKit framework also includes a set of standard subclasses that range from simple buttons to complex tables and can be used as-is. For example, a UILabel object draws a text string and a UIImageView object draws an image.
….
Managing the View Hierarchy
      superview  property
      subviews  property
      window  property
        – addSubview:

addSubview:
Adds a view to the end of the receiver’s list of subviews.
- (void)addSubview:(UIView *)view
Parameters
view
The view to be added. This view is retained by the receiver. After being added, this view appears on top of any other subviews.
Discussion
This method retains view and sets its next responder to the receiver, which is its new superview.
Views can have only one superview. If view already has a superview and that view is not the receiver, this method removes the previous superview before making the receiver its new superview.

In the viewController.m generated code, the class is declared.

The class inherits from NSObject

@class DeepThoughtsViewController;

@interface DeepThoughtsAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    DeepThoughtsViewController *viewController;
}


·   create a pointer to an object of type UIWindow names “window”
·   create a pointer to an object “viewController” with a data type “DeepThoughtsViewController”

Next question to remember the answer to (I know I was able to interpret this just a couple of weeks back!)

-    The role of the < and >
- I remember that the interface is where we give the preview for the methods
@interface DeepThoughtsAppDelegate : NSObject <UIApplicationDelegate> {









Wednesday, June 1, 2011

Study Notes: Back to Syntax - [, ] and self



Click on the illustration above to fully enjoy the detail
About a month ago I was making connections between Objective-C and C++
Then I went on vacation for a few weeks
Time to rebuild the dendrites and remember what does what and who is who


* [ and ]  is all over the place in the Foundation and apps coding


One explanation I like (and it took me an hour last night to find it!) is on page 153 of Goldstei's Objective C for Dummies (I dislike the name of this series but the books usually work for me)


Quoting Goldstein here
[receiver message : arguments];


The receiver of a message can be either an object or a class.... (in) Objective-C you can send a message to a class..... (unlike C++) .... Class methods enable you to implement behavior that is not object-specific, but applicable to an entire class.


* The 'self' keyword is another item that I run across a lot in the Foundation template code. Sometimes it makes sense to me, and other times there's something about it that bothers me. Here's what Goldstein says about 'self' on page 269 of the same book


if (self = [super initWithAmount:theAmount forBudget:aBudget])


Here, Goldstein explains, we are "... assigning what (we) get back from (the) superclass's init method to self..... self is the hiden variable accessible to methods in an object that points to its instance variables.


Back on page 155 Goldstein explained the concept of self in greater detail.


"... how does (the code for a method) get to the object's ivars, which are sitting in some place in memory..... When you send a message to Objective-C, a hidden argument called self, a pointer to the object's instance variables, is passed to the receiving object.


example
[europeBudge spendDollars:numberDollarsInEuroland]


the method passes europeBudget as its self argument


..... As you create objects, you get a new pointer for each one, and when you send a message to a particular object, the pointer associated with that object becomes the self argument.


* Of course I still have to pickup self.


Quoting Goldstein on page 271


...the self = stateent ensures that self is set to whatever object I get back from the superclass initializer. After the code blocks that initialize the variables...


return self; 


* Furthermore the use of the self keyword, pageg 312/313 where Goldstein in a section titled "Accessing the instance variables from within the class"

".. you can access them from other objects or from main..... (how to access properties) from within the object walls.


[self setCountry:theCountry];


You can also use the dot notation similar to other object-oriented languages)


self.country = theCountry;


* So let's think about how to interpret the following code snip generated by the Framework for an appDelegate.m file


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {   
   
    // Override point for customization after app launch.
    [self.window addSubview:viewController.view];
    [self.window makeKeyAndVisible];


    return YES;
}