1
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];

    if (self) {
        self.sw = [[[UISwitch alloc] initWithFrame:CGRectMake(210, 8, 50, 30)] autorelease];
       [sw setOn:YES];
        [sw addTarget:self action:@selector(switchChanged) forControlEvents:UIControlEventTouchUpInside];

       [self addSubview:sw];
    }

    return self;
}

-(void)switchChanged
{
    //    [self setImage];
    [sw setOn:NO];
    if (_delegate && [_delegate respondsToSelector:@selector(selectedWith:)]){
        [_delegate selectedWith:self];
    }
}

当我点击开关时,没有调用函数switchChanged?

4

2 回答 2

1

改为forControlEvents改为UIControlEventValueChanged_UIControlEventTouchUpInside

尝试这个,

 [sw addTarget:self action:@selector(switchChanged) forControlEvents:UIControlEventValueChanged];
于 2013-08-07T06:54:21.227 回答
0

因为当您需要更改 UISwitch 中的值时,您指定的事件应该是 UIControlEventValueChanged

[sw addTarget:self action:@selector(switchChanged) forControlEvents: UIControlEventValueChanged];
于 2013-08-07T07:13:39.250 回答