ios - PickerView 中的一个按钮在 iOS 7 中不起作用

标签 ios uipickerview

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

- (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!

}

最佳答案

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

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

在这行代码之前:

[view addSubview:manageButton];

希望对您有所帮助。

关于ios - PickerView 中的一个按钮在 iOS 7 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19239989/

相关文章:

ios - UIPickerView 不重用以前的组件 View

objective-c - 如何调用pickerView : didSelectRow: inComponent: without user interaction?

iphone - 如何为不同的按钮操作设置单一选择器 View

ios - 获取权限后如何刷新权限

ios - 苹果 : how to distribute without store?

ios:<错误>:CGAffineTransformInvert:奇异矩阵

iphone - 模态对话框在 iOS 6 上不占用全屏

iphone - 更改了 Xcode 中的目标名称现在它运行黑屏

ios - iOS 中的内联 UIPickerview

objective-c - 在 3 个 UIPickers 的 uitextfield 中输入分数?