UIalertController date control, uialertcontroller

Source: Internet
Author: User

UIalertController date control, uialertcontroller

The idea is that the height of the UIalertController is determined by the content, and the view can be modified.

First, you do not need to change the class initialization (because you only need to modify the view Interface)

Inherit the. h file of UIalertController

#import <UIKit/UIKit.h>typedef void(^BlockDate)(id date);@interface SheetDate : UIAlertController@property(nonatomic,copy)BlockDate blockDate;@property(retain,nonatomic)UIDatePicker* datePicker;-(NSString*)dealDate;-(void)addDatePicker;@end

Inherit from the. m file of UIalertController

# Import "SheetDate. h "@ interface SheetDate () @ end @ implementation SheetDate-(void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view from its nib .} -(void) didReceiveMemoryWarning {[super didreceivemorywarning]; // Dispose of any resources that can be recreated .} /*** add date control */-(void) addDatePicker {self. datePicker = [[UIDatePicker alloc] initWithFrame: CGRectMake (0, 0,320,216)]; self. datePicker. datePickerMode = UIDatePickerModeDate; self. datePicker. locale = [[NSLocale alloc] initWithLocaleIdentifier: @ "zh_CN"]; [self. view addSubview: self. datePicker];}/*** process date data ** @ return */-(NSString *) dealDate {NSDateFormatter * formatter = [[NSDateFormatter alloc] init]; if (self. datePicker. datePickerMode = UIDatePickerModeTime) {[formatter setDateFormat: @ "HH: ss"];} else {[formatter setDateFormat: @ "YYYY-MM-dd"];} NSString * currentTime = [formatter stringFromDate: self. datePicker. date]; return currentTime ;}

Then use

/*** Date/time select ** @ param sender */-(IBAction) chooseDate :( UIButton *) sender {SheetDate * sheetDate = [SheetDate alertControllerWithTitle: nil message: @ "\ n" preferredStyle :( UIAlertControllerStyleActionSheet)]; [sheetDate addDatePicker]; sheetDate. blockDate = ^ (NSString * date) {[sender setTitle: date forState :( UIControlStateNormal)] ;}; // start time, end time if (sender. tag = 70 | sender. tag = 71) {sheetDate. datePicker. datePickerMode = idle;} UIAlertAction * OK = [UIAlertAction actionWithTitle: @ "OK" style :( UIAlertActionStyleDefault) handler: ^ (UIAlertAction * _ nonnull action) {sheetDate. blockDate ([sheetDate dealDate]);}]; [sheetDate addAction: OK]; [self presentViewController: sheetDate animated: YES completion: nil];

First, create a uialertcontroller object using the system method. Use \ n to increase the display height. Then call [sheetDate addDatePicker] to add a custom interface and initialize the block using date data as needed.

Then, trigger it in the sheet option.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.