2016-08-30 162 views
0

我正在开发一个具有UISwitch的应用程序。我想以编程方式将开关设置为打开/关闭。我试过这样,但开关已经打开时,settingfalse ...任何想法?UISwitch设置状态

@IBOutlet var switch: UISwitch! 

override func viewWillAppear(animated: Bool) { 
    super.viewWillAppear(animated) 

    switch = UISwitch() 

    if setting == true { 
     switch.setOn(true, animated: false) 
    } else { 
     switch.setOn(false, animated: false) 
    } 
} 

变量setting是没问题的。我测试了它

+1

从您发布的代码中,如果'setting'为'false',那么开关将关闭。这不是预期的功能吗? – random

+0

你能指定更多细节吗? – barryjones

+0

关于您期望的功能和实际功能的更多细节将对我们有很大帮助。 – ZGski

回答

0

我定了!

我刚刚删除了行switch = UISwitch(),现在它适用于我。