我想添加完成按钮,并在alertview控制器的文本文件中返回日期

问题描述:

actually i want this in objective c 我需要一些帮助这种类型的代码。 如果有任何可以帮助,然后一步一步进行。 我需要完成按钮并返回到文本字段的日期。我想添加完成按钮,并在alertview控制器的文本文件中返回日期

[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) { 
    textField.placeholder = @"date"; 
    textField.textColor = [UIColor blueColor]; 
    textField.clearButtonMode = UITextFieldViewModeWhileEditing; 



}]; 
+0

您需要在日期选择器中完成并重置按钮吗? – iOS

设置

[alert textFieldAtIndex:0].delegate = self; 

实施

- (BOOL)textFieldShouldReturn:(UITextField *)alertTextField {//add any method you want to execute to here. This method will be called when user taps on the textfield in alertview. 
[ alertTextField resignFirstResponder];// to dismiss the keyboard. 
[alert dismissWithClickedButtonIndex:0 animated:YES];//this is called on alertview to dismiss it. 
} 

.H

@interface CustomViewController : UIViewController <UIAlertViewDelegate, UITextFieldDelegate> 

{ 

UIAlertView *alert; 

} 

代表

alertTextField.delegate = self; 

yourMethod

(void) yourMethod 
{ 

    alert = [[UIAlertView alloc] initWithTitle:@"YourTitle" message:@"YourMessage" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Load",nil]; 

} 

UIAlertController *alert; 

//... 

-(void)showAlertController { 
    alert = [UIAlertController alertControllerWithTitle:@"Title" 
               message:@"Message" 
             preferredStyle:UIAlertControllerStyleAlert]; 

    UIAlertAction *button = [UIAlertAction actionWithTitle:@"Done" 
                style:UIAlertActionStyleDefault 
                handler:^(UIAlertAction * _Nonnull action) { 
                 //Do your thing 
                 //NSString *strTextField_1 = alert.textFields[0].text; 
                 //NSString *strTextField_2 = alert.textFields[1].text; 
                 //NSString *strTextField_3 = alert.textFields[2].text; 
                }]; 
    [alert addAction:button]; 

    [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { 
     [textField setPlaceholder:@"Notes"]; 
    }]; 

    __weak typeof(self) weakSelf = self; 
    [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { 
     UIDatePicker *picker = [[UIDatePicker alloc] init]; 
     [picker setMinimumDate:[NSDate date]]; 
     [picker setTag:1]; 
     [picker addTarget:weakSelf 
        action:@selector(datePickerUpdated:) 
     forControlEvents:UIControlEventValueChanged]; 
     [textField setInputView:picker]; 

     [textField setPlaceholder:@"date"]; 
    }]; 
    [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { 
     UIDatePicker *picker = [[UIDatePicker alloc] init]; 
     [picker setMinimumDate:[NSDate date]]; 
     [picker setTag:2]; 
     [picker addTarget:weakSelf 
        action:@selector(datePickerUpdated:) 
     forControlEvents:UIControlEventValueChanged]; 
     [textField setInputView:picker]; 

     [textField setPlaceholder:@"date"]; 
    }]; 

    [self presentViewController:alert 
         animated:YES 
        completion:nil]; 
} 

-(void)datePickerUpdated:(UIDatePicker *)datePicker { 
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; 
    [formatter setDateFormat:@"yyyy-MM-dd hh:mm"]; 
    NSString *string = [formatter stringFromDate:datePicker.date]; 

    NSInteger currentIndex = datePicker.tag; 
    [alert.textFields[currentIndex] setText:string]; 
} 

,只要你想你可以使用这个完整的例子。

#import "ViewController.h" 

@interface ViewController()<UITextFieldDelegate>{ 
    UIDatePicker *datepicker; 
    UIToolbar *toolbar; 
    UITextField *dateField; 
} 

@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    datepicker = [[UIDatePicker alloc]init]; 
    datepicker.datePickerMode= UIDatePickerModeDate; 
    datepicker.backgroundColor = [UIColor whiteColor]; 

    toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)]; 
    [toolbar setBackgroundColor:[UIColor whiteColor]]; 
    UIBarButtonItem *donebtn = [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(setDate)]; 

    UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil]; 

    UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:@selector(cancelDatePicker)]; 

    [toolbar setItems:[NSArray arrayWithObjects:cancelBtn,flex,donebtn, nil]]; 
} 


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

- (IBAction)showAlertController:(id)sender { 

    UIAlertController * alert= [UIAlertController 
           alertControllerWithTitle:@"Add Action" 
           message:@"Please enter notes or action" 
           preferredStyle:UIAlertControllerStyleAlert]; 

    UIAlertAction* save = [UIAlertAction actionWithTitle:@"Save" style:UIAlertActionStyleDefault 
               handler:^(UIAlertAction * action) { 
                NSLog(@"Notes: %@", alert.textFields[0].text); 
                NSLog(@"Date: %@", alert.textFields[1].text); 
               }]; 
    UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault 
                handler:^(UIAlertAction * action) { 
                 [alert dismissViewControllerAnimated:YES completion:nil]; 
                }]; 

    [alert addAction:save]; 
    [alert addAction:cancel]; 

    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) { 
     textField.placeholder = @"Notes"; 
     textField.delegate = self; 
    }]; 
    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) { 
     textField.placeholder = @"Date"; 
     textField.delegate = self; 
     [textField setInputView:datepicker]; 
     [textField setInputAccessoryView:toolbar]; 
     dateField = textField; 
    }]; 
    [self presentViewController:alert animated:YES completion:nil]; 
} 

-(void)setDate{ 

    NSDate *selectedDate = datepicker.date; 
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateFormat:@"MM/dd/yyyy"]; 
    dateField.text = [dateFormatter stringFromDate:selectedDate]; 

    [dateField resignFirstResponder]; 
} 

-(void) cancelDatePicker{ 
    [dateField resignFirstResponder]; 
} 

@end