My Blog List

Showing posts with label objective-c. Show all posts
Showing posts with label objective-c. Show all posts

Tuesday, June 14, 2011

Study Notes: Syntax, Picking Apart the UIColor example

Study Notes: Syntax UIColor example



* Notice there are examples linked from frameworks class documentation. Studying these has been suggested in class and by Goldstein for learning
…..
What the following

   theTextField.textColor    =[UIColor blueColor];

seems to boil down to is

…. Set the textColor attribute (property) of my ‘theTextField’ ivar to be BLUE.


And here are all the details I went through to figure that out
….
I want to better understand this line of code from page 236

    theTextField.textColor =[UIColor blueColor];

“theTextField”

·    is a pointer I defined in the interface file associated with this implementation file.

@interface SettingsViewController : UIViewController {
    <SettingsViewControllerDelegate> delegate;
    float                       sliderValue;
    IBOutlet  UITextField   *theTextField;

·    The pointer is of type UITextField, a frameworks class
·    The part that follows theTextField uses dot notation
o     I THINK textColor is an instance method associated with UITextField



Let’s see if I can figure that out

·     option+ command on UITextField
·     follow link to ()delegate Protocol

NO DICE NO REFERENCE TO textColor


Overview
A UITextField object is a control that displays editable text and sends an action message to a target object when the user presses the return button. You typically use this class to gather small amounts of text from the user and perform some immediate action, such as a search operation, based on that text.
In addition to its basic text-editing behavior, the UITextField class supports the use of overlay views to display additional information (and provide additional command targets) inside the text field boundaries. You can use custom overlay views to display features such as a bookmarks button or search icon. The UITextField class also provides a built-in button for clearing the current text.
A text field object supports the use of a delegate object to handle editing-related notifications. You can use this delegate to customize the editing behavior of the control and provide guidance for when certain actions should occur. For more information on the methods supported by the delegate, see the UITextFieldDelegate protocol.


Go back up a level looking for textColor (some kind of method I think….)

It is a property of the UITextFieldClass. Don’t quite understand that. I know I use the property declaration to substitute for accesors. Well I least I see it that is something



OK back to

    theTextField.textColor =[UIColor blueColor];

I know that [] syntax means that I’m messaging an object. “Many methods in UIKit require you to specify color data using a UIColor object, and for general color needs it should be your main way of specifying colors. “
 So I’m messaging the UIColor class from the blueColor method.

I guess that I’m saying…. Set the textColor attribute (property) of my ‘theTextField’ ivar to be BLUE.





Understanding UIColor use


·     option+ command on UIColor to get documention

Overview
A UIColor object represents color and sometimes opacity (alpha value). You can use UIColor objects to store color data, and during drawing you can use them to set the current fill and stroke colors.
Many methods in UIKit require you to specify color data using a UIColor object, and for general color needs it should be your main way of specifying colors. The color spaces used by this object are optimized for use on iOS-based devices and are therefore appropriate for most drawing needs. If you prefer to use Core Graphics colors and color spaces instead, however, you may do so.
Most developers should have no need to subclass UIColor. The only time doing so might be necessary is if you require support for additional colorspaces or color models.


·     UIColor is the class
UIColor Class Reference
Inherits from
Conforms to
Framework
/System/Library/Frameworks/UIKit.framework
Availability
Available in iOS 2.0 and later.
Declared in
UIColor.h
UIInterface.h
·      
·     blueColor is a class method assoc. with UIColor
blueColor
Returns a color object whose RGB values are 0.0, 0.0, and 1.0 and whose alpha value is 1.0.
+ (UIColor *)blueColor
Return Value
The UIColor object.
Availability
·                       Available in iOS 2.0 and later.

Study Notes: On PROTOCOLS and DELEGATES



Finally, I am starting to get a better sense of the use of protocols and delegates.  The light that went on today was as a result of page 235 in Goldstein/Bove's iPad Application for Dummies, 2nd edition.

It helps that I'm picking through the example tiny bit by tiny bit, like I'm looking for gold dust in a big pan of water, rock and dirt. (Not a reflection on objective-c!)

"The methods that a class delegates are defined in a protocol..... (using).... the @protocol directive."

"Protocols declare methods that can be implemented by any class."

 .....

Previous helpful references from one of following page groups 204, 234-5, 318

* One object delegates the task of implementing one of it's methods, to another object

* Methods a class delegates are defined in a protocol


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;
}











Tuesday, May 31, 2011

Study Notes: Design Patterns: MVC (Part 3) Windows and Views (Part 4)


Study Notes 3: Design Patterns: MVC

P 141

·     The Model View Controller(MVC) is one of the basic design patterns around which frameworks are designed.
·     A design pattern is a commonly used template
·     MVC pattern is populated with 3 kinds of objects
o   Model Objects
§     Engine of the app, app’s data and logic
§     Like a particular t.v. program, independent of set brand and home wiring/cable/delivery method
o   View Objects
§     Displays things on the screen
§     Responds to user actions
§     Anything I see in an app is a kind of view object
§     Like a television screen, independent of program, just displays and responds to my touches on controls by sending info off to whomever it ought to go to
o   Controller Objects
§     Connects apps view objects TO it’s model objects
§     Like the circuitry that pulls the show off cable and sends it to the screen or requests a pay-per-view show

My examples:
M       A dairy
V       A home order form for milk that I fill out
C       The milk woman who looks at my order, puts the milk into her truck and delivers it to my home

M       My home furnace
V       My thermostat
C      
* Ductwork for my furnace that sends heat in
* also whatever mechanism the thermostat triggers that sends a message to the furnace to heat up some air and pump it into the ductwork

Study Notes 4: Windows and Views

·     Single window on iPad an instance of the UIWindow class, created by UIKit
·     A window is a specialized kind of window
·     I add views to the window
·     I don’t directly close or manipulate an iPad window
·     A view is an area on top of the window
·     View is main way that app interacts with users
o   Display content
o   Handle touch events
·     View Hierarchy
o   Content view(s) added as subview(s)
o   A view with a subview is a superview
o   One view 0 to N subviews, 1 superview
o   Subviews displayed on top of their superviews/parents
o   Controls: buttons, text fields, etc are (become) subviews
o   Drawing and Event Handling lowest level subview first rendered. Each subview returns control to its superview after it is rendered
o   UIKit framework handles the view hierarchy relationships
      
·     Kinds of Views (p 148) (are all of these views container views?
o   Container Views
o   Display Views
o   Text and Web views
o   Alert views and action sheets
o   Navigation views
o   (The window) Root container for all other views

Monday, May 30, 2011

Study Notes: Frameworks Part 2


* Frameworks are SIMILAR to software libraries but ALSO implement a flow of control

* Frameworks order things like which messages re sent to objects, launch order, and when a user can touch a buttton

* ORDER IS PART OF THE FRAMEWORK
* The programmer says how to act (really it's the framework that says how to act - I think)

* The programmer adds specific functionality to the framework. She adds

     * content
     * controls
     * views

* DESIGN PATTERNS  behind the frameworks are what I need to understand.

* A really crucial design pattern to understand is the one for UIKIT

Study Notes: Frameworks Part 1



 
P 138/ Chapter 7

* IOS operating system for iPad (iPhone)

* Framework       Common code that provides generic functionality

·     IOS frameworks provide
o    Event handling support
o    Drawing support
o    Windows
o    Views
o    Controls

* Are the IOS frameworks the same as Cocoa?



Cocoa (API)
Snip from
From Wikipedia, the free encyclopedia

Cocoa
Website
Cocoa is one of Apple Inc.'s native object-oriented application programming interfaces (APIs) for the Mac OS X operating system and—along with the Cocoa Touch extension for gesture recognition and animation—for applications of iOS on Apple's iPhone, iPad, and iPod touch product lines.
Cocoa applications are typically developed using the development tools provided by Apple, specifically Xcode (formerly Project Builder) and Interface Builder, using the Objective-C language. However, the Cocoa programming environment can be accessed using other tools, such as Clozure CL, LispWorks, Object Pascal, Python, Perl, Ruby, and AppleScript with the aid of…..

What is Cocoa Touch

Snip from
http://developer.apple.com/technologies/ios/cocoa-touch.html

The Cocoa Touch frameworks that drive iOS apps share many proven patterns found on the Mac, but were built with a special focus on touch-based interfaces and optimization. UIKit provides the basic tools you need to implement graphical, event-driven applications in iOS. UIKit builds on the same Foundation framework infrastructure found on the Mac OS X, including file handling, networking, string building, and more.

Study Terms: Interface Builder

Am currently working with the Neil Goldstein/Tony Bove book iPad Application Development for Dummies, Version 2. Some ideas to reinforce from today's reading.

(page 101)


* The View Controller Window is the table of contents for the nib file. It represents an instance of each object. Also it always contains two proxy objects.

Proxy Objects

What is a proxy object? I know that a proxy is a substitute for an object and that an objective-C class can be accessed through a proxy object. They are something I give directions to to assume control for me. I think that proxy objects create things on the fly as opposed to my hard-coding them. I'd like to increase my understanding of proxy objects, because I think it would help me to better understand some of the ways that the SDK works.

      * File's Owner
             * is the controller object
             * is responsible for contents of the nib file
             * (in this example) is the View Controller

      * First Responder
              * first entry in the apps dynamically constructed responder chain
              * is the object, with which, the user is currently interacting

(Regular) Objects (in book example p 101)

       * View
              an instance of the UIView class






Wednesday, May 25, 2011

Staying Tuned: Reflecting on Objective-C



I switched gears in my work on Objective-C, and stopped blogging because of that. I realized I needed to become more of an objective-oriented programmer in my thinking and stopped off to take a class in C++. Now I plan to return to my self-study of Objective-C.

I'll be working with some other books.

Stay Tuned.

Tuesday, November 16, 2010

Over the Edge of Time: Accessing Nil!

    
OR

Why you never want to complain about waiting in the airport 
while they do a
little maintenance

The Well-Behaved Version

There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin".tty /dev/ttys000
Loading program into debugger…
Program loaded.
run
[Switching to process 31280]
Portal Pilots Maintenance Flight. Scanning for UNIQUE Time Portals
portals[0] = bridge
portals[1] = creek
portals[2] = path
portals[3] = alley
Running…

Debugger stopped.
Program exited with status value:0.

But Wait!

HERE’S WHAT HAPPENS WHEN THE ARCH-ENEMY OF PORTAL PILOTS MANAGES TO GET HIRED ONTO THE DEVELOPMENT STAFF


(just tought I'd try to access nil and see where it took me!)

// If I ever want to totally gum up the works, I'll make sure to try to access past the end of the array!


Portal Pilots Maintenance Flight. Scanning for UNIQUE Time Portals
portals[0] = bridge
portals[1] = creek
portals[2] = path
portals[3] = alley
2010-11-16 12:14:54.431 encounterPortals[31239:a0f] *** __NSAutoreleaseNoPool(): Object 0x10010cce0 of class NSCFString autoreleased with no pool in place - just leaking
2010-11-16 12:14:54.433 encounterPortals[31239:a0f] *** __NSAutoreleaseNoPool(): Object 0x1001107d0 of class NSCFString autoreleased with no pool in place - just leaking
2010-11-16 12:14:54.433 encounterPortals[31239:a0f] *** __NSAutoreleaseNoPool(): Object 0x1001023b0 of class NSException autoreleased with no pool in place - just leaking
2010-11-16 12:14:54.434 encounterPortals[31239:a0f] *** __NSAutoreleaseNoPool(): Object 0x1001108e0 of class _NSCallStackArray autoreleased with no pool in place - just leaking
2010-11-16 12:14:54.434 encounterPortals[31239:a0f] *** __NSAutoreleaseNoPool(): Object 0x100110a90 of class _NSCallStackArray autoreleased with no pool in place - just leaking
2010-11-16 12:14:54.448 encounterPortals[31239:a0f] *** __NSAutoreleaseNoPool(): Object 0x100110e30 of class NSCFString autoreleased with no pool in place - just leaking
2010-11-16 12:14:54.495 encounterPortals[31239:a0f] *** __NSAutoreleaseNoPool(): Object 0x100111440 of class NSCFString autoreleased with no pool in place - just leaking
2010-11-16 12:14:54.496 encounterPortals[31239:a0f] *** __NSAutoreleaseNoPool(): Object 0x100111890 of class NSConcreteMutableData autoreleased with no pool in place - just leaking
2010-11-16 12:14:54.497 encounterPortals[31239:a0f] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFArray objectAtIndex:]: index (4) beyond bounds (4)'
*** Call stack at first throw:
(
       0   CoreFoundation                      0x00007fff884d8cc4 __exceptionPreprocess + 180
       1   libobjc.A.dylib                     0x00007fff86f250f3 objc_exception_throw + 45
       2   CoreFoundation                      0x00007fff884d8ae7 +[NSException raise:format:arguments:] + 103
       3   CoreFoundation                      0x00007fff884d8a74 +[NSException raise:format:] + 148
       4   Foundation                          0x00007fff801db084 _NSArrayRaiseBoundException + 122
       5   Foundation                          0x00007fff8013db59 -[NSCFArray objectAtIndex:] + 75
       6   encounterPortals                    0x0000000100000dfd main + 417
       7   encounterPortals                    0x0000000100000c54 start + 52
       8   ???                                 0x0000000000000001 0x0 + 1
)
terminate called after throwing an instance of 'NSException'
Program received signal:  “SIGABRT”.
sharedlibrary apply-load-rules all
kill
quit


// encounterPortals
// PortalPilots encounter a variety of Time Portal Types
// based on accesselements page 166, Holzner

#import <Foundation/Foundation.h>

// IMPORTANT STUFF
//  1) End arrays with the nil object
//  2) Use @ to differentiat OBJECTIVE C strings from C-style strings

int main (int argc, const char * argv[]) {
    NSArray *portals = [[NSArray alloc] initWithObjects:@"bridge", @"creek",@"path", @"alley", nil];
    printf("Portal Pilots Maintenance Flight. Scanning for UNIQUE Time Portals \n");
    printf("portals[0] = %s \n", [[portals objectAtIndex: 0] cString]);
     printf("portals[1] = %s \n", [[portals objectAtIndex: 1] cString]);
    printf("portals[2] = %s \n", [[portals objectAtIndex: 2] cString]);
     printf("portals[3] = %s \n", [[portals objectAtIndex: 3] cString]);
   
    // Thought it would be interesting to try to access nil
    // Wow! Leaking - What IS leaking? And all kinds of neat errors
  
    //printf("portals[4] = %s \n", [[portals objectAtIndex: 4] cString]);

    return 0;
}