UISwitch action not work in iOS 10

跟風遠走 提交于 2019-12-12 05:29:46

问题


I have a switch button in one cell of table view. Previously in iOS 9, everything works fine, after click switch button will goto the action method.But after update to iOS 10, the action method never be called. Anyone has similar issue like this?

cell.m

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {

        // Initialization code

        UIImageView *bgView = [[UIImageView alloc] initWithFrame:CGRectZero];
        [bgView setImage:[[UIImage imageNamed:@"customcell.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)]];
        [self setBackgroundView:bgView];


        UIImageView *selectedBgView = [[UIImageView alloc] initWithFrame:CGRectZero];
        [selectedBgView setImage:[[UIImage imageNamed:@"cellselect.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)]];

        [self setSelectedBackgroundView:selectedBgView];

        nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 13, 250, 40)];

        [nameLabel setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleRightMargin];

        [nameLabel setFont:[UIFont boldSystemFontOfSize:16]];
        [nameLabel setBackgroundColor:[UIColor clearColor]];
        [nameLabel setTextColor:[UIColor whiteColor]];

        [nameLabel setNumberOfLines:0];

        [self.contentView addSubview:nameLabel];

        switchButton = [[UISwitch alloc] initWithFrame: CGRectMake(230, 17, 100, 30)];

        [switchButton setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];

        [switchButton addTarget:self action:@selector(switchActivated:) forControlEvents:UIControlEventTouchUpInside];

        [switchButton setHidden:YES];

        [self.contentView addSubview:switchButton];

    }
    return self;
}

Action method:

- (void) switchActivated:(id)sender
{
    if(switchButton.on)
    {
        [selectedItem setObject:@"YES" forKey:@"valueEntry"];
    }
    else 
    {
        [selectedItem setObject:@"NO" forKey:@"valueEntry"];
    }
}

ViewController.m cell for row at index path:

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

UITableViewCell *tableViewCell = [aTableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"homeReportCustomizationCell_%d",indexPath.row]];

        if(!tableViewCell)
        {
            tableViewCell = [[Cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:@"Cell_%d",indexPath.row]];
        }
...........
}

回答1:


Just change UIControlEventTouchUpInside inside [switchButton addTarget:self action:@selector(switchActivated:) forControlEvents:UIControlEventTouchUpInside];

To: UIControlEventValueChanged




回答2:


I've got the same issue!

I have a UISwitch and a UITextField inside a UIScrollView. I add the scrollView to my self.view.

I tested this with an iPad Pro running iOS 10-- Before upgrading to the latest version of iOS, everything worked just fine. Now my methods are not getting called!!

Hopefully I won't have to resort to a programmatic solution-- that would be a bummer.



来源:https://stackoverflow.com/questions/40213611/uiswitch-action-not-work-in-ios-10

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