【问题标题】:A button in PickerView not working iOS 7PickerView 中的一个按钮在 iOS 7 中不起作用
【发布时间】:2013-10-08 05:46:41
【问题描述】:

这是我的代码,我无法处理按钮选择器。

- (UIView *)pickerView:(UIPickerView *)pickerView
        viewForRow:(NSInteger)row
      forComponent:(NSInteger)component
       reusingView:(UIView *)view {

    if(view == nil) {
        view = [[[UIView alloc] init] autorelease];
    }

    [view setFrame:CGRectMake(0, 0, 320, 44)];
    UIButton *manageButton = (UIButton *)[view viewWithTag:TAG_MANAGE + row];
    UILabel *descTypeLabel = (UILabel *) [view viewWithTag:TAG_DESCTYPE_LABEL + row];
    if(manageButton == nil &&  row != 0) {

        //CGRect frame = CGRectMake(210, 7, 90, 30);
        CGRect frame = CGRectMake(0, 7, view.frame.size.width, 30);
        manageButton = [UIButton buttonWithType:UIButtonTypeCustom];
        manageButton.frame = frame;
        [manageButton setTitle:@"Manage" forState:UIControlStateNormal];
        [manageButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [manageButton setBackgroundImage:[[UIImage imageNamed:@"blackButton.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(8, 8, 8, 8)] forState:UIControlStateNormal];
        manageButton.tag = TAG_MANAGE + row;
        [view addSubview:manageButton];
    }
    if(descTypeLabel == Nil) {
        descTypeLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, 190, 44)];
        descTypeLabel.layer.borderColor = [UIColor greenColor].CGColor;
        descTypeLabel.layer.borderWidth = 1.0;
        descTypeLabel.backgroundColor = [UIColor clearColor];
        descTypeLabel.tag = TAG_DESCTYPE_LABEL + row;
        [descTypeLabel setText:[descTypes objectAtIndex:row]];
        [view addSubview:descTypeLabel];
        [descTypeLabel release];
    }
    [manageButton addTarget:self action:@selector(managePressed:) forControlEvents:UIControlEventTouchUpInside];

    return view;
}

-(void) managePressed:(UIButton *) sender {

    //This selector is not called on manage button tap!

}

【问题讨论】:

    标签: ios uipickerview


    【解决方案1】:

    我认为您应该先将选择器添加到 manageButton,然后再将其添加到 pickerView
    移动这行代码就行了:

    [manageButton addTarget:self action:@selector(managePressed:) forControlEvents:UIControlEventTouchUpInside];
    

    在这行代码之前:

    [view addSubview:manageButton];
    

    我希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      我找到了解决方案。显然,根据您的设计,可能不需要带有完成按钮的工具栏,因此在 UIPickerView 单元格上添加“不可见”完成按钮的一种好方法是:

      // Instantiate an UIImagePicker into your class
      @interface myView () {
          UIPickerView *_picker;
      }
      @end
      
      // alloc/init the picker of your class and a UITapGestureRecognizer
      - (void)initPicker {
          _picker = [[UIPickerView alloc] init];
          [_picker addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self
                                                                                action:@selector(dismissPicker)]];
      }
      
      // method called when the user taps on the UIPickerView.
      // The normal behaviour is to use the current selected row as the one wanted by the user.
      - (void)dismissPicker {
          NSInterger selectedRow = [_picker selectedRowInComponent:0];
          // and now use the data and dismiss the picker the way you like the most
      }
      

      这就是所有的人;p

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-10-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多