UIPickerView button selector issue in iOS 7

寵の児 提交于 2019-12-06 07:12:22

问题


Having a weird issue of UIPickerView only on iOS 7

I have a UIPickerView which contains 3 rows. Each row has a button whose selector is defined, but it never respond on button tap.

Here is my code.

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

回答1:


You will need to use

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

instead in iOS 7 and do your needed work in it.




回答2:


You can't use button on picker view cell. I have used toolbar and added a bar button item on it. By this

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

you will get the row number and use that to perform desired functionality.



来源:https://stackoverflow.com/questions/19245744/uipickerview-button-selector-issue-in-ios-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!