1天学习1个类 UIDatePicker 类 示例

示例:

1天学习1个类 UIDatePicker 类 示例_第1张图片


代码:

//
//  main.m
//  ControlDemo
//
//  Created by watsy0007 on 12-6-3.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController {
    UIDatePicker *_datePicker_;
}

@end


@implementation ViewController

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

- (void) segmentControlAction:(id) sender {
    UISegmentedControl *segControl = (UISegmentedControl *) sender;
    switch (segControl.selectedSegmentIndex) {
        case 0:
            _datePicker_.datePickerMode = UIDatePickerModeTime;
            break;
            
        case 1:
            _datePicker_.datePickerMode = UIDatePickerModeDate;
            break;
            
        case 2:
            _datePicker_.datePickerMode = UIDatePickerModeDateAndTime;
            break;
            
        case 3:
            _datePicker_.datePickerMode = UIDatePickerModeCountDownTimer;
            _datePicker_.countDownDuration = 1;
            break;
            
        default:
            break;
    }
}

- (UIBarButtonItem *) createNewItem {
    UIBarButtonItem *item;

    UISegmentedControl *segControl = [[UISegmentedControl alloc] initWithFrame:CGRectMake(0, 0, 300, 30)];
    segControl.segmentedControlStyle = UISegmentedControlStyleBar;
    
    [segControl insertSegmentWithTitle:@"Time" atIndex:0 animated:YES];
    [segControl insertSegmentWithTitle:@"Date" atIndex:1 animated:YES];
    [segControl insertSegmentWithTitle:@"DAndT" atIndex:2 animated:YES];
    [segControl insertSegmentWithTitle:@"Count" atIndex:3 animated:YES];
    
    item = [[UIBarButtonItem alloc] initWithCustomView:segControl];
    
    [segControl addTarget:self action:@selector(segmentControlAction:) forControlEvents:UIControlEventValueChanged];
    [segControl release];
    
    return [item autorelease];
}

- (UIDatePicker *) DatePickerDemo {
   _datePicker_ = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 200, 320, 50)];
    
//    datePicker.calendar = [NSCalendar currentCalendar];
    _datePicker_.date = [NSDate dateWithTimeInterval:1000 sinceDate:[NSDate date]];
    
    return _datePicker_;
}

- (void) loadView {
    [super loadView];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
    label.frame = CGRectMake(0, 0, 320, 200);
    label.numberOfLines = 3;
    label.textAlignment = UITextAlignmentCenter;
    label.text = @"watsy0007 \n QQ:258841679\n群:125807534";
    [self.view addSubview:label];
    [label release];
    
    self.title = @"UIDatePicker Class Reference Demo";

    [self.view addSubview:[self DatePickerDemo]];
    
    self.navigationItem.rightBarButtonItem = [self createNewItem];
}


- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return YES;
}

- (void) viewDidLoad {
    [super viewDidLoad];
}

@end


@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UIViewController *viewController;

@end

@implementation AppDelegate

@synthesize window = _window;
@synthesize viewController = _viewController;

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

- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    
    self.viewController = [[ViewController alloc] init];
    
    UINavigationController *controller = [[UINavigationController alloc] initWithRootViewController:self.viewController]; 
    self.window.rootViewController = controller;
    [controller release];
    
    [self.window makeKeyAndVisible];
    return YES;
}

@end

int main(int argc, char *argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}


你可能感兴趣的:(1天学习1个类 UIDatePicker 类 示例)