【问题标题】:ToolBar button not working when add on picker view like as accessoryView添加选择器视图(如附件视图)时,工具栏按钮不起作用
【发布时间】:2023-03-14 11:05:02
【问题描述】:

当我在选择器视图上添加工具栏时,工具栏按钮不起作用。它适用于 iOS 6,但不适用于 iOS 7。

 UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0 ,0, 320, 44)];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                               style:UIBarButtonItemStyleBordered
                                                              target:self
                                                              action:@selector(handleDone)];

UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"Cancel"
                                                                 style:UIBarButtonItemStyleBordered
                                                                target:self
                                                                action:@selector(handleCancel)];

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

[toolBar setItems:[NSArray arrayWithObjects:cancelButton, flexible, doneButton, nil] animated:YES];

[pikerView addSubview:toolBar];

【问题讨论】:

  • 使用uiview,在uiview里面添加toolbar和uipickerview。
  • stackoverflow.com/questions/20883388/… 看看我给出的这个答案
  • 感谢您的回复,但我想知道为什么会这样。我知道单独查看并单独添加两件事会起作用。 Rushabh 将您的回复作为答案,所以我可以接受它。需要解释上述问题为什么会这样。

标签: ios iphone uibutton uipickerview toolbar


【解决方案1】:

应将带有“完成”按钮的UIToolbar 添加到成为第一响应者的视图的inputAccessoryView。由于UIView 类继承自UIResponder,因此任何视图都可能包含inputViewinputAccessoryView。因此,您可以使用 UIResponder 的键盘显示/隐藏动画附带的默认动画行为,而不是以编程方式手动执行动画。

  1. 子类化 UIView 并覆盖 inputViewinputAccessoryView 属性并将它们设为 readwrite。在本例中,我将继承 UITableViewCell

    // FirstResponderTableViewCell.h
    @interface FirstResponderTableViewCell : UITableViewCell
    @property (readwrite, strong, nonatomic) UIView *inputView;
    @property (readwrite, strong, nonatomic) UIView *inputAccessoryView;
    @end
    
  2. 在子类的实现中覆盖 canBecomeFirstResponder

    // FirstResponderTableViewCell.m
    - (BOOL)canBecomeFirstResponder {
        return YES;
    }
    
  3. 在您的视图控制器中,创建并分配选择器视图和输入辅助工具栏

    // MyViewController.m
    - (void)viewDidLoad {
        [super viewDidLoad];
        UIPickerView *pickerView = [[UIPickerView alloc] init];
        UIToolbar *accessoryToolbar = [UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
        // Configure toolbar .....
    
        // note: myFirstResponderTableViewCell is an IBOutlet to a static cell in storyboard of type FirstResponderTableViewCell
        self.myFirstResponderTableViewCell.inputView = pickerView;
        self.myFirstResponderTableViewCell.inputAccessoryView = accessoryToolbar;
    }
    
  4. 不要忘记在需要时将第一响应者分配给视图(例如内部 - tableView:didSelectRowAtIndexPath:

    [self.myFirstResponderTableViewCell becomeFirstResponder];
    

希望这会有所帮助。

参考:http://blog.swierczynski.net/2010/12/how-to-create-uipickerview-with-toolbar-above-it-in-ios/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-15
    • 2020-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多