简体   繁体   English

将touchupinside添加到以编程方式创建的按钮,并将NSDate提交到文本字段

[英]Adding touchupinside to a programmatically created button, and submitting a NSDate to a textfield

The problem I'm coming across is that I'm unsure how to add touchupinside to the below code for the save date button. 我遇到的问题是我不确定如何将touchupinside添加到以下代码中的“保存日期”按钮。 I'm just trying to launch a UIDatePicker and submit a date into a text field, and then dismiss it. 我只是想启动UIDatePicker并将日期提交到文本字段中,然后将其关闭。

So I don't know how to link this programmatically created button to the datePicker method. 因此,我不知道如何将此以编程方式创建的按钮链接到datePicker方法。 Not only that but, I haven't used UIDatePicker before, so reading this into a textfield I'm a bit fuzzy on. 不仅如此,我以前也没有使用过UIDatePicker,因此将其读入文本字段时我有点模糊。 The text field is dateFieldText. 文本字段是dateFieldText。

Once the user taps Save Date then the UIDatePicker should be dismissed. 一旦用户点击“保存日期”,则应关闭UIDatePicker。

Thanks for the help. 谢谢您的帮助。 This issue is a bit more complicated for me. 这个问题对我来说有点复杂。

      - (void)textFieldDidBeginEditing:(UITextField *)sender
        {
            sender.delegate = self;
                if([sender isEqual:dateFieldText])
                {
                    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]
                                               initWithTitle:@"Save Date"
                                               style:UIBarButtonItemStyleDone
                                               target:self
                                               action:@selector(datePicker)];

                self.navigationItem.rightBarButtonItem = doneButton;

            }
            else{
                UIBarButtonItem *submitButton = [[UIBarButtonItem alloc]
                                               initWithTitle:@"Done"
                                               style:UIBarButtonItemStyleDone
                                               target:self
                                               action:@selector(datePicker)];
                self.navigationItem.rightBarButtonItem = submitButton;

            }  
        }


    -(IBAction)datePicker
    {
        UIDatePicker *datePicker = [[UIDatePicker alloc] init];
        datePicker.datePickerMode = UIDatePickerModeDateAndTime;
        [datePicker addTarget:self action:@selector(saveDate)  forControlEvents:UIControlEventValueChanged];
        [self.dateFieldText setInputView:datePicker];
    }

    // I haven't made the saveDate method yet...

You have a few things wired up incorrectly. 您有一些接线错误。 You need to make the date picker the text field's inputView at the point you create the text field, not after the user taps a button that isn't added until the user puts focus on the text field. 您需要在创建文本字段时使日期选择器成为文本字段的inputView,而不是在用户点击一个未添加的按钮(直到用户将焦点放在文本字段上)之后。

You also don't need a Done button and a Save Date button. 您也不需要“完成”按钮和“保存日期”按钮。 Either have a single Done button or have a Save and a Cancel button. 有一个“完成”按钮,或者有一个“保存”和“取消”按钮。

You are also going to need to implement the textFieldDidEndEditing delegate method to remove any buttons that you add. 您还将需要实现textFieldDidEndEditing委托方法以删除添加的任何按钮。

Your saveDate method will need to update the text field's text with the currently selected date (after converting the date to a string with an NSDateFormatter ). 您的saveDate方法将需要使用当前选定的日期更新文本字段的文本(在使用NSDateFormatter将日期转换为字符串之后)。

The action for your Done/Save/Cancel button(s) (whatever you end up doing) simply needs to call resignFirstResponder on the text field. “完成/保存/取消”按钮的操作(无论您最终要做什么) resignFirstResponder需要在文本字段上调用resignFirstResponder This will dismiss the date picker. 这将关闭日期选择器。 The button actions may also need to save off or revert the date's value as appropriate. 按钮操作可能还需要保存或适当地还原日期的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM