您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 使用技巧 >> iOS 7 技巧

iOS 實作 UIDatePicker + UIToolbar 時間選擇器

編輯:iOS 7 技巧

製作一個簡潔好用的時間選擇器,其實只要善用 UITextField 的 inputView 屬性即可。本文直接上程式碼,示範如何讓文字框在獲得焦點時彈出一個搭配工具列的日期滾輪。

核心觀念是:把 UIDatePicker 設為 textField 的 inputView,再把一個帶有「完成」按鈕的 UIToolbar 設為 inputAccessoryView,這樣使用者選好日期後點完成即可回填文字並收起鍵盤。

#import "ViewController.h"

@interface ViewController () <UITextFieldDelegate> {
    UITextField *textFiled;
    NSString *timeStr;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    textFiled = [[UITextField alloc] initWithFrame:CGRectMake(70, 80, 120, 30)];
    textFiled.layer.borderColor = [[UIColor grayColor] CGColor];
    textFiled.layer.borderWidth = 1.0f;
    [self.view addSubview:textFiled];

    // 建立時間選擇器
    UIDatePicker *datePicker = [[UIDatePicker alloc] init];
    // 只顯示中文
    [datePicker setLocale:[NSLocale localeWithLocaleIdentifier:@"zh-CN"]];
    // 監聽滾動事件
    [datePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged];
    // 只顯示日期
    datePicker.datePickerMode = UIDatePickerModeDate;
    // 光標移到文字框時,彈出時間選擇器
    textFiled.inputView = datePicker;

    // 建立工具列
    UIToolbar *toolbar = [[UIToolbar alloc] init];
    toolbar.barTintColor = [UIColor blackColor];
    toolbar.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 35);

    // 為工具列添加按鈕
    UIBarButtonItem *item0 = [[UIBarButtonItem alloc] initWithTitle:nil style:UIBarButtonItemStylePlain target:self action:nil];
    UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:nil style:UIBarButtonItemStylePlain target:self action:nil];
    UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem *item3 = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(click:)];

    toolbar.items = @[item0, item1, item2, item3];
    // 設定文字框的輔助視圖
    textFiled.inputAccessoryView = toolbar;
}

#pragma mark - 時間選擇
- (void)dateChanged:(UIDatePicker *)sender {
    NSDate *date = sender.date;

    NSDateFormatter *yearDateFormatter = [NSDateFormatter new];
    yearDateFormatter.dateFormat = @"yyyy";

    NSDateFormatter *monthDateFormatter = [NSDateFormatter new];
    monthDateFormatter.dateFormat = @"MM";

    NSDateFormatter *dayDateFormatter = [NSDateFormatter new];
    [dayDateFormatter setDateFormat:@"dd"];

    timeStr = [NSString stringWithFormat:@"%@-%@-%@",
               [yearDateFormatter stringFromDate:date],
               [monthDateFormatter stringFromDate:date],
               [dayDateFormatter stringFromDate:date]];
}

#pragma mark - 確定按鈕點擊
- (void)click:(UIButton *)sender {
    NSLog(@"timeStr>=%@", timeStr);
    textFiled.text = timeStr;
    [textFiled resignFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end

這裡有幾個小細節值得留意:dateChanged 用三個獨立的分別格式化年、月、日,最後拼成「yyyy-MM-dd」的字串,好處是格式清晰、易於維護;工具列中用 FlexibleSpace 把「完成」按鈕推到最右側,符合多數使用者的操作習慣;點擊完成時呼叫 resignFirstResponder 收起選擇器,讓表單回到正常狀態。若需要選擇「時分」,只要把 datePickerMode 改為 UIDatePickerModeDateAndTime 並調整格式化字串即可。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved