【问题标题】:UIPickerView button selector issue in iOS 7iOS 7 中的 UIPickerView 按钮选择器问题
【发布时间】:2013-10-15 06:15:02
【问题描述】:

仅在 iOS 7 上遇到一个奇怪的 UIPickerView 问题

我有一个包含 3 行的 UIPickerView。每行都有一个按钮,其选择器已定义,但它从不响应按钮点击。

这是我的代码。

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

NSLog(@"row %d", row);

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);
    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.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];
[manageButton addTarget:self action:@selector(manageButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

return view;
}

-(void) manageButtonPressed : (UIButton *) sender {
//Not Called
}

【问题讨论】:

  • 您找到解决方案了吗?我也面临着类似的问题。
  • 您不能在选择器视图行上放置按钮。所以我在它上面添加了一个工具栏并将按钮放在上面。我使用行号通过选择器执行所需的功能

标签: ios uibutton ios7 uipickerview


【解决方案1】:

你需要使用

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

改为在 iOS 7 中完成您需要的工作。

【讨论】:

  • 用户希望对选择器单元格中的按钮进行操作,而不是对整个选择器单元格。
  • 在 iOS 7 之前,这做得相当好。对于 UIPickerView 的每一行中的 UIButton,按钮点击操作都是可用的。但在 iOS 7 中,这不起作用,需要使用 didSelectRow 来确定通过 UIPickerView 的 viewForRow 方法点击了哪一行/按钮。你可以在 iOS 7/XCode 5 [稳定版] 中试一试
【解决方案2】:

您不能在选择器视图单元格上使用按钮。我使用了工具栏并在其上添加了一个栏按钮项。以此

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

您将获得行号并使用它来执行所需的功能。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-12
    • 2015-12-25
    相关资源
    最近更新 更多