顯示具有 cocoa programming 標籤的文章。 顯示所有文章
顯示具有 cocoa programming 標籤的文章。 顯示所有文章

2008年9月13日 星期六

@property & @synthesize

@property :
define getter  & setter method
ex:
@property(readwrite, assign)  int test;

@synthesize:
implement getter & setter method
ex:
@synthesize test;

binding

ex:
binding Slider's value to a key, then slider's value will sync with key

How to do:
In slider's inspect, set its binding 
(1)set bind to class A
(2)set A's  test member as model key path

key value coding

ex:
.h檔

@interface AppController : NSObject {

int test;

}

@end


.m檔

// set test

[self setValue:[NSNumber numberWithInt:5]

forKey:@"test"];

// get test

NSNumber *n=[self valueForKey:@"test"];


// is called when valueForKey is called

-(int)test

{

NSLog(@"test is returning %d", fido);

return fido;

}


// is called when setValue is called

-(void)setTest:(int)x

{

NSLog(@"setTest is called with %d", x);

fido=x;

}



delegate

if B is delegate of A,  when something happens to A,  B's method is called

ex:
B is  TableView's delegate 
B implement following method:
-(void)tableViewSelectionDidChange:(NSNotification*)notification 
when the selection row is table is changed ,  this method is called

NSTableView

data source:
decide the content of the table
implement two methods:
-(int)numberOfRowInTableView:(NSTableView*)tv

-(id)tableView:(NSTableView*)tv
                           objectValueForTableColumn:(NSTableColumn*)tableColumn
                           row:(int)row

NSWindow

initialFirstResponder outlet:
this outlet decides which view should be receiving keyboard events when window appears 

NSButton, NSSlider

NSButton:
oval button, square button ,  check box

methods:
-(void)setEnabled:(BOOL)yn

-(int)state
if return NSOnState(1), the check box is on
if return NSOffState(0), the check box is off

-(void)setState:(int)aState

NSSlider:
-(void)setFloatValue:(float)x

-(float)floatValue

NSTextField:
-(NSString*)stringValue

-(void)setStringValue:(NSString*)aString

NSFormatter can convert a string into another type , and vice versa
-(NSObject*) objectValue

-(void)setObjectValue:(NSObject*)obj





2008年6月24日 星期二

memory management in objective-c

retain:
increase retain count

release:
decrease retain count

array:
when an object is added to the array, the object's retain count is increased
when the array is deallocated,  the object( in array) 's retain count is decreased 

dealloc:
when an object's retain count becomes 0, its dealloc method is called

autorelease:
the object is added to autorelease pool when it is sent message autorelease
when the pool is drained, it sends message release to all objects in the pool

objects created by alloc, new, copy or mutableCopy have a retain count of 1 & are not in the autorelease pool 

if the object is get from other method, it is usually in the autorelease pool. If you do not want it to be deallocated when the pool drain, you must retain it

memory control for setter:
retain , then release
ex:
-(void)setName:(NSString*)name
{
      [name retain];
      [myName release];
      myName= name;
}

2008年6月15日 星期日

NSObject

(id)init

(NSString*)description:
when NSLog print object, it will print the return value of description of the object


(BOOL)isEqual:(id)anObject
for NSString, isEqual will compare characters 
note: == compare if two objects are the same objects

NSString

start with @
ex:
NSString *test= @"hello";

NSString to C string
ex:
char *test = [ a UTF8String];

C string to NSString:
ex:
a= [NSString stringWithUTF8String:test];

NSLog

similar to printf in C
ex:
NSLog(@"hello");

token for NSLog:
%@: id

NSNumber

initWithInt


NSMutableArray & NSArray

NSArray:
objectAtIndex


NSMutableArray:
addObject:
ex:
[test addObject:dog1];

class and object

calling method:
ex:
[test run];
test is a object, run is its method

calling method with arguments:
ex:
[test run:1  secondArg:2];
1 is first argument,  2 is second argument, secondArg: is the description of second argument

create a object:
test = [NSMutableArray alloc];

create & init a object:
test = [  [NSMutableArray alloc] init];

TextField

setStringValue:
ex:
[textField setStringValue:@"hello"]

setIntValue:

setObjectValue:


import & objective-c keyword

#import:
similar to include. But it ensures the header is included only once.

keywords:
@end
@implementation
@class
@selector
@protocol
@property
@interface
@synthesize

type in objective-c

id:  a pointer to any type of object

BOOL::

YES: 1
NO: 0

IBOutlet:  a macro that evaluates to nothing. It is a hint for interface builder

IBAction:  the same as void,  a hint for interface builder

nil:  the same as NULL.  for pointers to objects
        In Objective-C, it is ok to send message to nil. The message is simply discarded.

interface builder and nib

the application's GUI is designed by interface builder

GUI is defined in the nib file.  

Use interface builder to open nib file & design gui

define class used in the nib
IBOutlet
instance variable points to other object is called outlet
ex:
IBOutlet  NSTextField *testField;
IBAction
the method is called by user interface objects
ex:
-(IBAction)seed:(id)sender;

create an object instance:
1. drag object from Library window to doc window
2. set its class to object's class name in the Identity Inspector

make connection:
connect two objects
ex:
a object has outlet textFiled of type TextField , then connect textFiled to  TextFiled GUI
ex:
when button b is pressed, it calls the method test in object c. Then connect b to object c's methods test

awakeFromNib:
all objects are sent the message awakeFromNib after the objects in nib file are brought to life
Hence, we can do something in awakeFromNib to init GUI