2011年6月3日金曜日

Xcode: datePickerで日付を選択する





datePickerViewController.h
#import <UIKit/UIKit.h>

@interface datePickerViewController : UIViewController {

 UIDatePicker *selectTime;
 IBOutlet UILabel *label;
 IBOutlet UITextField *textField;
}

@property(nonatomic, retain)IBOutlet UIDatePicker *selectTime;

-(IBAction)buttonPressed;

@end

datePickerViewController.m
#import "datePickerViewController.h"

@implementation datePickerViewController

@synthesize selectTime;

-(IBAction)buttonPressed{

 NSDate *choice = [selectTime date];
 NSString *message = [[NSString alloc] initWithFormat:@"The date is %@", choice];
 
 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"The title" 
             message:message 
               delegate:nil 
            cancelButtonTitle:@"close" 
            otherButtonTitles:nil, nil];
 
 [alert show];
 [alert release];
 [message release];
 
 label.text = message;
 textField.text = message;
}

- (void)didReceiveMemoryWarning {
 // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
 
 // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
 // Release any retained subviews of the main view.
 // e.g. self.myOutlet = nil;
 self.selectTime = nil;
 [super viewDidUnload];
}


- (void)dealloc {
 [selectTime release];
    [super dealloc];
}

@end

参考

YouTube - XCode 4 Tutorial Date Picker - Geeky Lemon Development
http://www.youtube.com/watch?v=qW9nJgk9XwI&hd=1


0 コメント: