2016-03-23 32 views
2

我有一个开关作为酒吧按钮,但我不知道如何引用一个酒吧按钮项目的开/关值。 XCode不允许将交换机识别为除条形按钮项外的其他任何内容。开关嵌入在barbutton中,无法被引用。切换UISwitch作为BarButtonItem

@IBOutlet weak var toggleOutlet: UIBarButtonItem! 
@IBAction func toggleButton(sender: UIBarButtonItem) { 

    // if toggleOutlet.on // not recognized as bar button item 

,如果有人能够帮助这将是伟大 感谢

回答

0

越来越近..但功能不被认可;

@IBAction func togglePresence(sender: UIBarButtonItem) { 
    updateSwitch() 
} 

// hold empty status of switch 
// this is set to true during viewDidLoad 
var statusofswitch:Bool! 

func updateSwitch() { 
    if statusofswitch == true { 
     statusofswitch = false 
    } 
    if statusofswitch == false { 
     statusofswitch = true 
    } 
} 
3

感谢您的查询。

您可以使用以下步骤执行此操作。

鉴于没有负荷,调用setRightNavButton()和做加法的任务,因为,

func setRightNavButton() { 

    let switchControl=UISwitch(frame:CGRectMake(0, 0, 51, 31)) 
    switchControl.on = true 
    switchControl.onTintColor = K.Color.AppBackgroundColor 
    switchControl.setOn(true, animated: false) 
    switchControl.addTarget(self, action: #selector(NotificationVC.switchValueDidChange(_:)), forControlEvents: .ValueChanged) 
    self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(customView: switchControl) 

} 


func switchValueDidChange(sender:UISwitch!) 
{ 
    if sender.on { 
     print("on") 
    } else{ 
     print("off") 
    } 
} 

感谢