【问题标题】:Add a UIButton to UIDatePicker将 UIButton 添加到 UIDatePicker
【发布时间】:2013-01-30 19:42:36
【问题描述】:

我有一个 UITextField,我将 inputView 设置为 UIDatePicker,这样当按下 UITextField 而不是键盘时,会弹出一个 UIDatePicker。我还想添加一个带有“下一步”按钮的工具栏,以便用户可以轻松跳转到下一个字段,但我不确定如何执行此操作,因为我已经在更改 UITextField 上的视图以获取UIDatePicker。这是加载视图时使用的代码:

- (void)viewDidLoad
{
self.datePicker = [[UIDatePicker alloc] init];
self.datePicker.datePickerMode = UIDatePickerModeDate;
[self.datePicker addTarget:self action:@selector(datePickerValueChanged) forControlEvents:UIControlEventValueChanged];
issue.inputView = self.datePicker;
}

关于将“下一步”按钮添加到工具栏顶部的建议?

【问题讨论】:

标签: ios uitextfield uitoolbar uidatepicker


【解决方案1】:

创建一个视图以用作文本字段的 inputAccessoryView。此视图应包含您的按钮(例如 Next、Cancel、Previous)。

【讨论】:

  • 好的,我添加了UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 22)]; pickerToolbar.barStyle=UIBarStyleBlackOpaque; [pickerToolbar sizeToFit]; NSMutableArray *barItems = [[NSMutableArray alloc] init]; UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithTitle:@"Next" style:UIBarButtonItemStyleBordered target:self action:@selector(expirenext)]; [barItems addObject:flexSpace]; [pickerToolbar setItems:barItems animated:YES]; expire.inputAccessoryView = pickerToolbar;,它成功了!我如何将按钮右对齐?
  • 这太难读了。使用代码更新您的问题。如果您对新代码有新问题,您应该提出新问题。
【解决方案2】:

代码如下:

if (keyboardToolbar == nil) {
keyboardToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
[keyboardToolbar setBarStyle:UIBarStyleBlackTranslucent];

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

UIBarButtonItem *next = [[UIBarButtonItem alloc] initWithTitle:@"Next" style:UIBarButtonItemStyleDone target:self action:@selector(switchToNextField:)];

[keyboardToolbar setItems:[[NSArray alloc] initWithObjects: extraSpace, next, nil]];
}

issue.inputAccessoryView = keyboardToolbar;

datePicker = [[UIDatePicker alloc] init];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker addTarget:self action:@selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];

issue.inputView = datePicker;

在您的 switchToNextField 方法中,只需使用 [nextTextField becomeFirstResponder] 将 nextField 作为 firstResponder;

【讨论】:

    【解决方案3】:

    您可以创建一个视图,然后将任何 UI 元素放入其中。

    @property (nonatomic,strong) UIPopoverController *popoverControllerBirthday;
    

    将以上内容添加到您的 .h 文件中,并将其同步到 .m @synthesize popoverControllerBirthday;

    还将您的 @synthesize datePicker; 更改为 @synthesize datePicker=_datePicker;

    - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    
        if (textField==self.yourtextfield) {
            UIViewController* popoverContent = [[UIViewController alloc] init]; //ViewController
    
            UIView *popoverView = [[UIView alloc] init];   //view
            popoverView.backgroundColor = [UIColor blackColor];
    
            _datePicker=[[UIDatePicker alloc]init];//Date picker
            _datePicker.frame=CGRectMake(0,0,320, 216);
            _datePicker.datePickerMode = UIDatePickerModeDate;
            [_datePicker setMinuteInterval:5];
            [_datePicker setTag:10];
            [popoverView addSubview:_datePicker];
    
    
    
            UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            [button addTarget:self
                       action:@selector(result)
             forControlEvents:UIControlEventTouchUpInside];
            [button setTitle:@"Next" forState:UIControlStateNormal];
            button.frame = CGRectMake(0, 220, 320, 45);
            UIColor *titleColor = [UIColor whiteColor];
            [button setTitleColor:titleColor forState:UIControlStateNormal];
           // UIImage *doneButtonImage = [UIImage imageNamed:@"create event button.png"]; choose a button background if you ahve one
            [button setBackgroundImage:doneButtonImage forState:UIControlStateNormal];
            [popoverView addSubview:button];
    
    
            popoverContent.view = popoverView;
            popoverControllerBirthday = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
            popoverControllerBirthday.delegate=self;
    
            CGRect myFrame =textField.frame;
            myFrame.origin.x = 200;// your coordinates change it as you wish
            myFrame.origin.y = 195;
    
            [popoverControllerBirthday setPopoverContentSize:CGSizeMake(320, 265) animated:NO];
            [popoverControllerBirthday presentPopoverFromRect:myFrame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
    
            return NO; // tells the textfield not to start its own editing process (ie show the keyboard)
        }
        else{
             return YES;
        }
    
    }
    //handle your result call next uitextfield in your case
    -(void) result 
    {
        //in result method you can get date using datepicker.date
        if (self.popoverControllerBirthday)
             {
                 [self.popoverControllerBirthday dismissPopoverAnimated:NO];
                  self.popoverControllerBirthday = nil;
        }
    
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setTimeStyle:NSDateFormatterNoStyle];
        [dateFormatter  setDateFormat:@"M d yyyy"];
        NSString *parseDatePickerDAte =[dateFormatter stringFromDate:_datePicker.date]; // your string that is choosen at datepicker
       // self.textfield.text = parseDatePickerDAte ; set this to your textfield if you want
    
       //google this dont remember exact code for that, just changing your textfield to desired one
       [self.nexttextfield becomesFirstResponder];
    
    
    
    }
    

    【讨论】:

      【解决方案4】:

      您可以使用SMDatePicker 代替 UIDatePicker。它将使用 UIToolbar 和 UIDatePicker 创建一个自定义视图。您可以使用必要的按钮轻松自定义工具栏。看看示例项目。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多