* 본 포스트는 Blog.MissFlash.com에서 작성한 것으로, 원문 저작자의 동의없이 마음대로 퍼가실 수 없습니다. 포스트의 내용이 마음에 드시면 링크를 이용해주시면 감사하겠습니다.
> Head First iPhone Development #2
Chapter 2. iPhone app patterns - Hello @twitter!
App develop process- Determine app layout
- Build the GUI
- Figure out how to use the controls
- Handle the data
- Send output to Twitter
The life of a root view
- Like in most other languages, main(...) gets called first.
- Main kicks off a Cocoa Touch Application.
- MainWindow.xib contains the connections for our application.
- The Cocoa Touch framework creates our custom view from the InstaTwitViewController.xib.
- When events occur with components, methods are invoked on our controller instance.
Development process
- First, declare that the controller conforms to both protocols(UIPickerViewDataSource, UIPickerViewDelegate).
- Next, add Mike's activities and feelings to the implementation file.
- The datasource protocol has two required methods(numberOfComponentsInPicerView:pickerView, pickerView:numberOfRowsInComponent).
- Connect the datasource just like actions and outlets.
- There's just one method for the delegate protocol(pickerView:titleForRow:forComponent).
- Add the IBOutlet and property to our view controller.
- Connect the picker to our outlet.
- Use our picker reference to pull the selected values(selectedRowInComponent).
@ vs. * : The "@" before those strings tells the compiler to make them NSString instead of char*. NSStrings are real Objective-C classes, as opposed to a simple C-style character pointer.
Added/Modified contents in .h file
@interface InstaTwitViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>
{
IBOutlet UIPickerView *tweetPicker;
NSArray* activities;
NSArray* feelings;
}
@property (nonatomic, retain) UIPickerView* tweetPicker;
- (IBAction)sendButtonTapped: (id)sender;
Added/Modified contents in .m file
@implementation InstaTwitViewController
@synthesize tweetPicker;
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if(component == 0)
{
return [activities count];
}else{
return [feelings count];
}
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger) component
{
switch (component)
{
case 0:
return [activities objectAtIndex:row];
case 1:
return [feelings objectAtIndex:row];
}
return nil;
}
- (IBAction)sendButtonTapped: (id)sender
{
NSString* themessage = [NSString stringWithFormat:@"I'm %@ and feeling %@ about it.", [activities objectAtIndex:[tweetPicker selectedRowInComponent:0]], [feelings objectAtIndex:[tweetPicker selectedRowInComponent:1]]];
NSLog(themessage);
//NSLog(@"Tweet button tapped!");
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://ID:PW@twitter.com/statuses/update.xml"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody:[[NSString stringWithFormat:@"status=%@", themessage] dataUsingEncoding:NSASCIIStringEncoding]];
NSURLResponse* response;
NSError* error;
NSData* result = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error];
NSLog(@"%@", [[[NSString alloc] initWithData:result encoding:NSASCIIStringEncoding] autorelease]);
}
- (void)viewDidLoad
{
activities = [[NSArray alloc] initWithObjects:@"sleeping", @"eating", @"working", @"thinking", @"crying", @"begging", @"leaving", @"shopping", @"hello worlding", nil];
feelings = [[NSArray alloc] initWithObjects:@"awesome", @"sad", @"happy", @"ambivalent", @"nauseous", @"psyched", @"confused", @"hopeful", @"anxious", nil];
[super viewDidLoad];
}
- (void)dealloc
{
[tweetPicker release];
[activities release];
[feelings release];
[super dealloc];
}
'PDA&Mobile' 카테고리의 다른 글
아이폰 앱 개발 팁(13) : Head First iPhone Development #4 (0) | 2010.04.08 |
---|---|
아이폰 앱 개발 팁(12) : Head First iPhone Development #3 (1) | 2010.03.30 |
아이폰 앱 개발 팁(10) : Head First iPhone Development #1 (0) | 2010.03.18 |
아이폰 앱 개발 팁(9) : Stanford Lecture #10 (0) | 2010.02.16 |
아이폰 앱 개발 팁(8) : Stanford Lecture #9 (0) | 2010.02.12 |