【问题标题】:A UISwitch that is never off一个永远不会关闭的 UISwitch
【发布时间】:2011-05-13 11:36:20
【问题描述】:

我使用以下代码创建了一个 UISwitch:

CGRect switchFrame = CGRectMake(200, 10, 94, 27);
UISwitch *yesNo = [[[UISwitch alloc] initWithFrame:switchFrame] autorelease];
[yesNo addTarget:self action:@selector(handleSwitch:) forControlEvents:UIControlEventValueChanged];

但是,无论开关处于何种状态,on 属性始终返回 YES,即使它在视觉上是 NO

事件处理程序如下所示:

-(IBAction) handleSwitch: (UISwitch *) sender{
    self.displayCompleted = sender.on;
}

设置 displayCompleted 属性:

-(void) setDisplayCompleted:(BOOL)newValue{
    displayCompleted = newValue;
    [[self tableView] reloadData];
}

这可能是什么原因?

【问题讨论】:

  • 我认为您提供的代码没有任何问题。 -handleSwitch: 方法或您实际检索开关状态的部分呢?
  • 我只包含了事件处理程序以及它设置的属性。

标签: objective-c cocoa-touch uiswitch


【解决方案1】:

我犯了一个愚蠢的错误:在 reloadData 之后重新创建单元格时,创建 UISwitch 时没有反映 displayCompleted 属性的当前状态。

【讨论】:

    猜你喜欢
    • 2017-07-22
    • 2011-06-26
    • 1970-01-01
    • 1970-01-01
    • 2019-12-21
    • 2018-11-01
    • 2014-08-04
    • 2012-09-13
    • 2011-08-12
    相关资源
    最近更新 更多