Confused about [self window] as receiver


#1

I’m on the Setting Up Views portion of chapter 27. On page 185 we added the following lines of code:


// add our threee UI element to the window
    [[self window] addSubview:taskTable];
    [[self window] addSubview:taskField];
    [[self window] addSubview:insertButton];
    
    //Finalize the window and put it on the screen
    [[self window] setBackgroundColor:[UIColor whiteColor]];
    [[self window] makeKeyAndVisible];

The receiver of the message is [self window]. Im not sure what this means and what the code is doing. I get that “self” is the instance of the the current class, but we also have “window”, which is an instance of UIWindow. So I think I have a message being sent to two different things.

Can someone help me interpret this?

Thanks!


#2
// add our threee UI element to the window
    [[self window] addSubview:taskTable];
    [[self window] addSubview:taskField];
    [[self window] addSubview:insertButton];
    
    //Finalize the window and put it on the screen
    [[self window] setBackgroundColor:[UIColor whiteColor]];
    [[self window] makeKeyAndVisible];
  1. [self window] returns the window object.
  2. [[self window] addSubview:taskTable] adds the taskTable (a view) to the window object. (The window object itself is a special view object; it holds other views.)
    UIWindow *theWindow = [self window];
    [theWindow addSubview:taskTable];

#3

I don’t get this either. “self” inherits from UIResponder, and UIResponder doesn’t have a method called “window”.

So how does “self” even understand the message (“window”) being passed?

Any help clarifying this is greatly appreciated.


#4

Any given object responds to every message declared in its class, in that class’ superclass, in that class’ superclass, etc.

By being an instance of a class that inherits from NSWindow, your object can respond to any message declared by its own class, and any message declared by NSWindow.

Revisit chapter 18 on Inheritance, and think about what it has to say, but in the context of your current question. I think it will become clearer.


#5

I think I’m getting it, “window” is a global instance object of the class UIWindow as declared in the .h of our class, and as such inherits from UIView.

So when we say “self” we are sending theses messages to our global instance “window”

Where the methods “makeKeyAndVisible” is declared/implemented in the class UIWindow, and the method “setBackgroundColor” is declared/implemented in the class UIView.

Since our instance object (our global object “window”) is of the UIWindow class it responds to UIView methods by way of inheritance.


#6

I would be careful with some of the terminology - some of the words you’ve used have distinct and special meanings.

I don’t want to be nitpicky, but I certainly don’t want you to get confused later if you run into them later in their proper context:

A global variable is different from an instance variable.

A global variable doesn’t belong to any object, it’s available to every object that knows it exists. That’s kind of an oversimplification, but it’s enough for now.

An instance variable, on the other hand, is owned by a specific object.

But it sounds like you’ve got it! [[self window] setBackgroundColor:blarg] indeed means “tell my window (which, being an instance of UIWindow, can do anything any of UIWindow’s superclasses (such as UIView) can do) to change its background color.”


#7

if I get this right, and in the case of our instance variable(which is an object) “window”.

the object “window” is global only as far as the scope of an instance of our class “iTaDoodle” since it is a property of “iTaDoodle”.

i.e. “window” is global to the instance of “iTadoodle” for which it belongs to.

Right? or am I off?


#8

I’d recommend not using the word global to describe window at all. I would call window an instance variable of your BNRAppDelegate instance.

As an aside, in addition to your app delegate object, every UIView instance also has a window instance variable that refers to the window on which the view sits.


#9

I guess I’m a bit unclear with the term “Global”.

Am I to understand that only global variables are declared in main?


#10

Global variables are truly global in your program - they’re declared outside of any function, usualy at the top of the file.

Consider this Something.h:[code]
#import <Foundation/Foundation.h>

int someInteger; // this is a global variable

@interface Something : NSObject {
int someOtherInteger; // this is an instance variable
// …
}

  • (void)someMethod;
    @end
    [/code] and this Something.m:[code]
    #import “Something.h”

@implementation Something

  • (void)someMethod {
    int yetAnotherInteger; // this is a local variable
    // …
    }
  • (void)someOtherMethod {
    // …
    }
    @end
    [/code]

yetAnotherInteger is a local variable. It only exists inside of the someMethod method. Code in the someOtherMethod method, for example, has no idea what yetAnotherInteger is. So we say that yetAnotherInteger is local to someMethod.

someOtherInteger is an instance variable of the Something class. That is, any method in the whole class can access someOtherInteger.

someInteger is a global variable. Notice that it’s been declared outside of the Something class interface or implementation areas, in Something.h. This means that every method in every class in every file that imports this Something.h file can access someInteger.


#11

The term global pertains to the visibility of a variable throughout an entire program.

If a variable is truly global, it can be accessed anywhere in the program.

See: viewtopic.php?f=135&t=5581


#12

I went to your link, and left you a response there regarding your examples.


#13

Go back, read my explanation and post back here if anything is unclear.


#14

Ok I think I got it,

two strings placed side by side (no white space between) become one.

i.e. “A”“B” becomes “AB”,
or if A = “c” and B = “d” then AB = “cd”,
or if P1 = “my pi” then "thats "P1 = “thats my pi”.

Also PRETTY_FUNCTION generates a string which contains the name of the function or method that called it.

But I wonder if the above way of connecting strings is standard, do programers rely on this behaviour?

Hmm I know theres a chapter on C strings coming up, I’ll have check it.

Thanks for all your help ibex10


#15

Zero or more white spaces are allowed between them.

Yes.

Yes, if A and B are defined as string literals “c” and “d”, respectively.

Yes, if P1 is defined as a string literal (as in the macro argument).


#16

Actually I have a problem with the second one, how does the compiler know that the two variables A and B, when placed side by side as AB, isn’t really another variable that is called AB.

Im not sure but this doesn’t seem like good coding style.