UISwitch操作在iOS 10中不起作用 [英] UISwitch action not work in iOS 10

查看:264
本文介绍了UISwitch操作在iOS 10中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表格视图的一个单元格中有一个切换按钮.以前在iOS 9中,一切正常,单击切换按钮后将转到该操作方法,但是更新到iOS 10后,该操作方法将永远不会被调用.任何人都有类似的问题吗?

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

操作方法:

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

ViewController.m索引路径中行的单元格:

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]];
        }
...........
}

推荐答案

我遇到了同样的问题!

我在UIScrollView中有一个UISwitch和一个UITextField.我将scrollView添加到我的self.view.

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

我使用运行iOS 10的iPad Pro测试了该功能在升级到最新版本的iOS之前,一切正常.现在我的方法没有被调用!!

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.

这篇关于UISwitch操作在iOS 10中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆