【问题标题】:Why UISwitch can not pass event?为什么 UISwitch 不能传递事件?
【发布时间】:2017-11-27 08:25:40
【问题描述】:

我在UITableView中有一个UISwitch,我想通过如下方式传递事件,但是事件是nil。如果我为按钮更换了开关,事件就会存在

[switch addTarget:self action:@selector(switchTaped:event:) forControlEvents:UIControlEventValueChanged];

-(void)switchTaped:(id)sender event:(id)event {
    NSSet *touches = [event allTouches];
    UITouch *touch = [touches anyObject];
    CGPoint currentTouchPosition = [touch locationInView:_tableView];
    NSIndexPath *indexPath= [_tableView 
    indexPathForRowAtPoint:currentTouchPosition];
    NSLog(@"%ld",indexPath.row);
}

【问题讨论】:

  • 你在哪里使用你的代码来添加动作到开关?

标签: ios uibutton uiswitch


【解决方案1】:

这是我自己的一个实现,一个通用的UITableViewCell,用于切换选择常见任务

文件.h

#import <UIKit/UIKit.h>

@interface SPFGenericBoolDataTableViewCell : UITableViewCell
{
    IBOutlet UILabel *dataTitleLabel;
    IBOutlet UISwitch *swDataValue;
}

@property (strong, nonatomic) UILabel *dataTitleLabel;
@property (strong, nonatomic) UISwitch *swDataValue;

-(void)setupWithTitle:(NSString*)title
            dataValue:(BOOL)dataValue
            indexpath:(NSIndexPath*)indexPath
andDataUpdateCallback:(void(^)(BOOL newValue,NSIndexPath*indexpath)) callback;

@end

文件.m

#import "SPFGenericBoolDataTableViewCell.h"

@interface SPFGenericBoolDataTableViewCell()
@property (strong) void(^dataWasUpdatedBlock)(BOOL newValue,NSIndexPath*indexpath);
@property (strong) NSIndexPath* currIndexPath;
@end

@implementation SPFGenericBoolDataTableViewCell

@synthesize swDataValue;
@synthesize dataTitleLabel;

#pragma mark -
#pragma mark NSCoding

- (void)encodeWithCoder:(NSCoder*)coder
{
    [super encodeWithCoder:coder];
    [coder encodeObject:self.dataTitleLabel forKey:@"dataTitleLabel"];
    [coder encodeObject:self.swDataValue forKey:@"swDataValue"];
}

- (instancetype)initWithCoder:(NSCoder*)decoder
{
    if(self = [super initWithCoder:decoder]){
        self.dataTitleLabel = [decoder decodeObjectForKey:@"dataTitleLabel"];
        self.swDataValue = [decoder decodeObjectForKey:@"swDataValue"];
    }
    return self;
}

-(void)setupWithTitle:(NSString*)title
            dataValue:(BOOL)dataValue
            indexpath:(NSIndexPath*)indexPath
andDataUpdateCallback:(void(^)(BOOL newValue,NSIndexPath*indexpath)) callback
{
    self.dataTitleLabel.text = title;
    [self.swDataValue setOn:dataValue];
    self.dataWasUpdatedBlock = callback;
    self.currIndexPath = indexPath;
    [self.swDataValue removeTarget:self action:@selector(valueChangedAction:) forControlEvents:UIControlEventValueChanged];
    [self.swDataValue addTarget:self action:@selector(valueChangedAction:) forControlEvents:UIControlEventValueChanged];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code

}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)valueChangedAction:(id)sender {
    if(self.dataWasUpdatedBlock != nil)
        self.dataWasUpdatedBlock(self.swDataValue.on,self.currIndexPath);
}

@end

使用示例

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


    SPFGenericBoolDataTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"SPFGenericBoolDataTableViewCell" forIndexPath:indexPath];

    [cell setupWithTitle:@"AnyTitle" dataValue:currValue indexpath:indexPath andDataUpdateCallback:^(BOOL newValue, NSIndexPath *indexpath) {
        NSLog(@"%@",indexPath);
    }];

    return cell;
}

【讨论】:

  • 请 NSLog(@"%@",event);是零?
  • 你想在事件参数中得到什么? @BaQiWL
  • tableView 中的indexPath.row
  • 这个 UISwitch 在一个 UITableViewCell 里面,你想得到那个单元格索引路径吗?这是你需要的吗?
  • 我将发布我的一个单元格的实现来做到这一点,好吗?你只需要实现回调就可以了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-27
  • 1970-01-01
  • 2011-12-25
  • 1970-01-01
  • 2020-08-07
相关资源
最近更新 更多