点击

时间:2016-02-18 18:38:08

标签: ios swift ios9 uibarbuttonitem ios9.1

我试图创建一个计数器。这个想法很简单,你点击" play"按钮,一旦你点击它应该消失,并成为一个"暂停"图标,会触发不同的动作。

我认为为计数器状态设置一个var并更改图标(只有一个按钮)可以解决这个问题,但我不知道如何设置按钮图像为"暂停&#34 ;或者从故事板面板创建时显示在下拉菜单中的任何其他内容。

这里是代码:

@IBOutlet weak var playButton: UIBarButtonItem!
var timer = NSTimer()
var currentStatus = "stopped"


@IBAction func playAction(sender: AnyObject) {
    if (currentStatus == "stopped"){
        timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("increaseTimer"), userInfo: nil, repeats: true)
        currentStatus = "running"
        // change button icon (playButton) to Stop
    }
    else {
        currentStatus = "stopped"
        timer.invalidate()
       // change button icon (playButton) to Play
    }

}

2 个答案:

答案 0 :(得分:1)

您可以像这样设置按钮样式:

//setButton to play
yourBarButtonItem = UIBarButtonItem(UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Play, target: self, action: "TheMethodThatTheButtonShouldCall"), animated: true)
//setButton to stop
yourBarButtonItem = UIBarButtonItem(UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Stop, target: self, action: "TheMethodThatTheButtonShouldCall"), animated: true)

答案 1 :(得分:1)

您可以使用以下数组设置按钮样式:

   func addCustomNavigationItemAtLeftAndRightSide(leftButtonItems:[UIBarButtonItem], rightButtonItems:[UIBarButtonItem]) {

    self.navigationItem.leftBarButtonItems = leftButtonItems
    self.navigationItem.rightBarButtonItems = rightButtonItems
 }

你可以使用这样的风格:

  let leftButtonItem = UIBarButtonItem(image: UIImage(named: "ic_top_back"), style: .Plain, target: self, action: "onBackButtonClicked:")
  addCustomNavigationItemAtLeftAndRightSide([leftButtonItem], rightButtonItems: [])