<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 閱讀(513) 評論(0)  編輯  收藏 所屬分類: iPhone
    主站蜘蛛池模板: 中文字幕专区在线亚洲| 免费毛片在线视频| 日本免费一区二区三区最新vr| 亚洲国产精品一区二区第一页| 一本到卡二卡三卡免费高| 国产精品亚洲精品日韩动图 | 国产在线精品一区免费香蕉| 男女拍拍拍免费视频网站| 四虎免费久久影院| 国产亚洲福利精品一区二区| 处破痛哭A√18成年片免费| AV在线亚洲男人的天堂| 免费国产草莓视频在线观看黄| 一本久久综合亚洲鲁鲁五月天 | 永久免费观看的毛片的网站| 亚洲欧美成人综合久久久| 卡一卡二卡三在线入口免费| 亚洲国产精品一区二区三区在线观看| 免费可以在线看A∨网站| 亚洲国产综合无码一区二区二三区| 久久精品国产亚洲av影院| 蜜桃AV无码免费看永久| 国产亚洲精品无码专区| 免费播放在线日本感人片| 亚洲成AV人片在线观看ww| 1000部拍拍拍18勿入免费凤凰福利| 亚洲A∨精品一区二区三区| www一区二区www免费| 久久精品国产亚洲av麻豆| 久久国产乱子伦免费精品| 国产亚洲sss在线播放| 3344在线看片免费| 亚洲精品成人久久| 免费涩涩在线视频网| 久久精品免费大片国产大片| 亚洲黄色网址大全| 国产精品免费一级在线观看| 一区二区三区免费看| 亚洲最新黄色网址| 亚洲?V无码成人精品区日韩| 久久免费的精品国产V∧|