<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Sealyu

    --- 博客已遷移至: http://www.sealyu.com/blog

      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
      618 隨筆 :: 87 文章 :: 225 評論 :: 0 Trackbacks

    Here is a great way to ask your users to input dates on your iPhone app.

    When you use UIDatePicker in your app you will a tumbler that lets users select a date that you may use in other parts of your app.

    In order to use UIDatePicker you will need to implement the UIPickerViewDelegate delegate and then add a UIPicker to your subview. To retrieve the date that the user inputs you can use the target-action pattern to assign a method to execute once the user has picked a date. See the video below:

    Here is the code from the example:

    Implement the UIDatePickerView Delegate

    The first thing we needed to do was indicate that our view controller would be acting as a delegate for the UITextField. To do this you simply need to declare it in the interface file:

    #import <UIKit/UIKit.h>
    //UIPicker requires an object that adopts the UIPickerViewDelegate protocol
    @interface DatePicker : UIViewController<UIPickerViewDelegate> {
    }
    @end

    Add the UILabel and UIDatePicker Objects

    This part is straightforward enough: you essentially create a label and a date picker and add them both to the view controller’s subview collection. This is the typical pattern used to build up a view in code.

    #import "DatePicker.h"
    @implementation DatePicker
    UILabel *label;
    UIDatePicker *datePicker;
    - (void)viewDidLoad {
    [super viewDidLoad];
    //Create label
    label = [[UILabel alloc] init];
    label.frame = CGRectMake(10, 10, 300, 40);
    label.textAlignment = UITextAlignmentCenter;
    //Use NSDateFormatter to write out the date in a friendly format
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    df.dateStyle = NSDateFormatterMediumStyle;
    label.text = [NSString stringWithFormat:@"%@",
    [df stringFromDate:[NSDate date]]];
    [df release];
    [self.view addSubview:label];
    [label release];
    // Initialization code
    datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 250, 325, 250)];
    datePicker.datePickerMode = UIDatePickerModeDate;
    datePicker.hidden = NO;
    datePicker.date = [NSDate date];
    [datePicker addTarget:self
    action:@selector(changeDateInLabel:)
    forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:datePicker];
    [datePicker release];
    }
    - (void)dealloc {
    [datePicker release];
    [label release];
    [super dealloc];
    }
    @end

    Implement Your Action Method

    This is the method that you assigned to the UIControlEventValueChanged event above using the target-action pattern.

    - (void)changeDateInLabel:(id)sender{
    //Use NSDateFormatter to write out the date in a friendly format
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    df.dateStyle = NSDateFormatterMediumStyle;
    label.text = [NSString stringWithFormat:@"%@",
    [df stringFromDate:datePicker.date]];
    [df release];
    }
    

    How this works is that when the user stops manipulating the tumbler an event called UIControlEventValueChanged occurs. Since we assigned the changeDateInLabel method as an action for this event the code in that method will execute updating the date in the label.

    Time To Take Action!

    Learning how to program is 5% reading and 95% action so the best way to learn is to do it yourself. Create a simple iPhone app that uses the date picker code.

    Let us know how it goes in the comments below!

    posted on 2010-10-04 22:26 seal 閱讀(502) 評論(0)  編輯  收藏 所屬分類: iPhone
    主站蜘蛛池模板: 在线观看免费无码视频| 精品国产免费一区二区| 亚洲国产日韩综合久久精品| 免费观看男人免费桶女人视频| 国产成人无码免费网站| 亚洲精品在线视频观看| 男人的天堂亚洲一区二区三区 | 性无码免费一区二区三区在线 | 亚洲另类无码专区丝袜| 亚洲午夜久久久久久久久久| 18国产精品白浆在线观看免费| 一级毛片免费在线观看网站| 亚洲国产美女福利直播秀一区二区| 四虎影视在线永久免费看黄| 啦啦啦完整版免费视频在线观看| 偷自拍亚洲视频在线观看| 亚洲精品人成电影网| 亚洲区小说区图片区| 免费a级毛片高清视频不卡| 免费a级毛片无码a∨免费软件 | 国产白丝无码免费视频| 亚洲精品无码专区在线| 91久久亚洲国产成人精品性色| 又大又粗又爽a级毛片免费看| 18禁美女裸体免费网站| 一级人做人a爰免费视频| 亚洲性色AV日韩在线观看| 99ri精品国产亚洲| 中文字幕亚洲一区二区三区| 日韩免费视频一区| 黄网站色在线视频免费观看| 精品国产免费一区二区三区香蕉 | 亚洲精品午夜国产va久久| 亚洲成年轻人电影网站www | 中文字幕日韩亚洲| 国产婷婷高清在线观看免费| 99在线视频免费观看视频| 性xxxxx大片免费视频| 成年免费a级毛片免费看无码| 亚洲av无码av在线播放| 亚洲va久久久久|