Swift - How to toggle between Play and Pause with UIBarButtonItem - swift

I am trying with the below Swift code in my timer application to start and pause with the same button I have. But it is does not toggle. The button is set with Identifier as "Play" in the IDE. When I run the app, Timer starts and pauses correctly. But it does not toggle button. It always remain as "Play"
#IBAction func pressPausePlay(sender: AnyObject) {
if playPause == false
{
timer.invalidate()
self.playPauseButtonVar = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Play, target: self, action: "pressPausePlay")
playPause = true
}
else
{
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("updateTime"), userInfo: nil, repeats: true)
self.playPauseButtonVar = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Pause, target: self, action: "pressPausePlay")
playPause = false
}
}

You can use this Example scenarios to toggle between Play and Pause, for Button Titles, Image, Navigation Bar, ToolBar
import UIKit
import AVFoundation
class ViewController: UIViewController {
var myPlayer = AVAudioPlayer()
#IBOutlet var myNaviBar: UINavigationBar! //OUTLET TO NAVIGATION BAR IMPORTANT TO WORK WITH ITS BARBUTTON ITEMS
#IBOutlet var myToolBar: UIToolbar!
//**************START :Bottom LEFT TOOLBAR Bar Button Item***********/
#IBAction func toolBarItemPlay(sender: AnyObject) {
myToolBar.setItems([UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Pause, target: self, action: "toolBarItemPause:")], animated: true)
myPlayer.play()
}
#IBAction func toolBarItemPause(sender: AnyObject) {
myToolBar.setItems([UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Play, target: self, action: "toolBarItemPlay:")], animated: true)
myPlayer.pause()
}
//**************END :Bottom LEFT TOOLBAR Bar Button Item***********/
//**************START :Top LEFT Naivation Bar Button Item***********/
#IBAction func leftNavgationbarPlay(sender: UIBarButtonItem) {
myNaviBar.topItem?.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Pause, target: self, action: "leftNavigationbarPause:")
myPlayer.play()
}
//no need of control drag to this one below because we are calling from the above leftNavgationbarPlay's Selector/action
#IBAction func leftNavigationbarPause(sender : UIBarButtonItem) {
myNaviBar.topItem?.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Play, target: self, action: "leftNavgationbarPlay:")
myPlayer.pause()
}
//**************END :Top LEFT Naivation Bar Button Item***********/
//**************START :Top Right Naivation Bar Button Item***********/
#IBAction func naviPlay(sender: UIBarButtonItem) {
//change the topright navigation bar item from SystemItem Play '>' to systemitem pause '||'
// here I am assigning the top left item Play to Bar button item Pause
/* ACTION SELECTOR IMPORTANT. IT MUST CALL THE IB ACTION FUNCTION OF PAUSE AND VICEVERSA, WITH ":" */
myNaviBar.topItem?.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Pause, target: self, action: "naviPause:")
myPlayer.play()
}
#IBAction func naviPause(sender: UIBarButtonItem) {
myNaviBar.topItem?.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Play, target: self, action: "naviPlay:")
myPlayer.pause()
}
//**************END : Top Right Naivation Bar Button Item***********/
//************START :BUTTON IMAGE CHANGE - ">"to "||"************//
#IBAction func imagePlayButton(sender: UIButton) {
let chkImage = sender.currentImage!
if chkImage == UIImage(named: "play.png"){
sender.setImage(UIImage(named: "pause.png"), forState: .Normal)
myPlayer.play()
} else {
sender.setImage(UIImage(named: "play.png"), forState: .Normal)
myPlayer.pause()
}
}
//************END :BUTTON IMAGE CHANGE - ">"to "||"************//
//************START BUTTON TITLE CHANGE - PLAY to PAUSE************//
#IBAction func playButton(sender: UIButton) {
let chkTitle = sender.currentTitle
if chkTitle == "Play" {
sender.setTitle("Pause", forState: .Normal)
myPlayer.play()
} else {
sender.setTitle("Play", forState: .Normal)
myPlayer.pause()
}
}
//************END BUTTON TITLE CHANGE - PLAY to PAUSE************//
#IBAction func naviStop(sender: UIBarButtonItem) {
myPlayer.stop()
myPlayer.currentTime = 0
}
#IBOutlet var sliderUI: UISlider!
#IBAction func mySlider(sender: UISlider) {
myPlayer.volume = sliderUI.value
}
override func viewDidLoad() {
super.viewDidLoad()
let audioFilePath = NSBundle.mainBundle().pathForResource("ARRehman", ofType: "mp3")
//need NSURL from string
do {
myPlayer = try AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: audioFilePath!))
}
catch {
print("Some error")
}
}
}

You need to replace bar button items hold in UIToolbar after you assign a new instance to self.playPauseButtonVar.
If you have toolBar or navigationBar outlets:
self.toolBar.setItems([self.playPauseButtonVar], animated: true)
// Or
self.navigationBar.setItems([self.playPauseButtonVar], animated: true)
If your class inherits UIViewController:
self.setToolbarItems([self.playPauseButtonVar], animated: true)
// Or
self.navigationItem.setRightBarButtonItem(self.playPauseButtonVar, animated: true)

Yoichi, thanks for pointing me to the correct place. I removed the references on the Tool bar Outlet variable (bottomToolBar) under "Referencing Outlets". That resolved the error. However I am not able to add the tool bar outlet again, if I add, the error comes back!

it worked with me by creating bar button by code this is my code
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var navigationBar: UINavigationBar!
#IBOutlet weak var displayCountLabel: UILabel!
var buttonPause = UIBarButtonItem()
var buttonPlay = UIBarButtonItem()
var count=0
var timer=NSTimer()
override func viewDidLoad() {
super.viewDidLoad()
buttonPause = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Pause, target: self, action: "pauseAction")
buttonPlay = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Play, target: self, action: "playAction")
self.navigationBar?.topItem?.leftBarButtonItem = buttonPlay
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func stopTimer(sender: AnyObject) {
timer.invalidate()
count = 0
displayCountLabel.text = "\(count)"
self.navigationBar?.topItem?.leftBarButtonItem = buttonPlay
}
func pauseAction() {
self.navigationBar?.topItem?.leftBarButtonItem = buttonPlay
timer.invalidate()
}
func playAction() {
self.navigationBar?.topItem?.leftBarButtonItem = buttonPause
timer=NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("displayCount"), userInfo: nil, repeats: true)
}
func displayCount()
{
count++
displayCountLabel.text = "\(count)"
}
}

Related

Change barButtonSystemItem after tapping

I have a bar button in the right side of the screen, it's barButtonSystemItem: .edit and it's used to put tableView in the editing mode. I want when, user tap on it, it change to barButtonSystemItem: .done and it close tableview from editing mode.
Just to be clear, every time that barButton is clicked, the type of it should be change from edit to done.
Here is my code, but it always stay with edit, and not changing to done
fileprivate func addBarButton() {
if tableView.isEditing {
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(editButtonAction))
} else {
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .edit, target: self, action: #selector(doneButtonAction))
}
}
#objc func editButtonAction(sender: UIBarButtonItem) {
tableView.isEditing = !tableView.isEditing
}
#objc func doneButtonAction(sender: UIBarButtonItem) {
tableView.isEditing = !tableView.isEditing
}
override func viewDidLoad() {
super.viewDidLoad()
addBarButton()
tableView?.isEditing = true
}
In the bodies of methods editButtonAction and editButtonAction you only change the tableView editing state by doing tableView.isEditing = !tableView.isEditing. This action does nothing to navigationItem at all.
I'd recommend you to refactor the code a bit by renaming addBarButton into updateBarButton and call it every time when the table editing state changes and additionally from viewDidLoad as you do now. So your code would become something like this:
fileprivate func updateBarButton() {
if tableView.isEditing {
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(editButtonAction))
} else {
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .edit, target: self, action: #selector(doneButtonAction))
}
}
#objc func editButtonAction(sender: UIBarButtonItem) {
tableView.isEditing = !tableView.isEditing
updateBarButton() // Note the additional call of the update method
}
#objc func doneButtonAction(sender: UIBarButtonItem) {
tableView.isEditing = !tableView.isEditing
updateBarButton() // Note the additional call of the update method
}
override func viewDidLoad() {
super.viewDidLoad()
tableView?.isEditing = true
updateBarButton() // Note that we call this method after changing the table view state so that it will have the most recent value
}

UIBarButtonItem is not Highlighted after setting

I have declared two UIBarButtonItem, btnEdit and btnSave. initially i'm setting btnEdit in viewDidLoad. if user taps on btnEdit i want to set btnSave as rightBarButtonItem and when tapped on btnSave, set btnEdit
as rightBarButtonItem.
it is working fine and i'm able to tap on it but after first tap button is faded/ not in highlighted state. Images Attached.
How do i make it have normal State?
var btnEdit : UIBarButtonItem!
var btnSave : UIBarButtonItem!
#objc func btnEditTapped (){
textFieldViewNote.isEditable = true
navigationItem.rightBarButtonItem = btnSave
}
#objc func btnSaveTapped (){
textFieldViewNote.isEditable = false
navigationItem.rightBarButtonItem = btnEdit
}
override func viewDidLoad() {
super.viewDidLoad()
btnSave = UIBarButtonItem(barButtonSystemItem: .save, target: self, action: #selector(btnSaveTapped))
btnEdit = UIBarButtonItem(barButtonSystemItem: .edit, target: self, action: #selector(btnEditTapped))
navigationItem.rightBarButtonItem = btnEdit
}
After loading VC - Edit in normal state
After tapping on edit - Save btn in normal State
Edit faded tapping on save barbutton
Below is the fixed code
class ViewController: UIViewController {
var btnEdit : UIBarButtonItem!
var btnSave : UIBarButtonItem!
#objc func btnEditTapped (){
btnSave = UIBarButtonItem(barButtonSystemItem: .save, target: self, action: #selector(btnSaveTapped));
navigationItem.rightBarButtonItem = btnSave
}
#objc func btnSaveTapped (){
btnEdit = UIBarButtonItem(barButtonSystemItem: .edit, target: self, action: #selector(btnEditTapped)); navigationItem.rightBarButtonItem = btnEdit
}
override func viewDidLoad() {
super.viewDidLoad()
btnSave = UIBarButtonItem(barButtonSystemItem: .save, target: self, action: #selector(btnSaveTapped))
btnEdit = UIBarButtonItem(barButtonSystemItem: .edit, target: self, action: #selector(btnEditTapped))
navigationItem.rightBarButtonItem = btnEdit
}
}

Add 'Done' button to keyboard using UI Toolbar

I have text views that I want to be able to have a done button. I have added a UI toolbar and connected it as an outlet plus IB action for the done button
The toolbar is not showing up? what have I missed?
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
ingredientsTextField.inputAccessoryView
= keyboardToolbar
return true
}
#IBAction func didClickDoneButton(_ sender: UIBarButtonItem) {
ingredientsTextField.endEditing(true)
}
May be your creation of toolbar is wrong, check this
func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
self.addAccessoryView()
return true
}
func addAccessoryView() -> Void {
let toolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 44))
let doneButton = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(self.doneButtonTapped(button:)))
let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
toolBar.items = [flexibleSpace,doneButton]
toolBar.tintColor = UIColor.red
self.textView.inputAccessoryView = toolBar
}
func doneButtonTapped(button:UIBarButtonItem) -> Void {
// do you stuff with done here
}
Your code snippet looks fine! Did you set the delegate for your textfield? If you missed it please set it else func textFieldShouldBeginEditing will not be called!
Please find below working code for your reference,
import UIKit
class ViewController: UIViewController,UITextFieldDelegate {
#IBOutlet weak var textField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
internal func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
let numberToolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 50))
numberToolbar.barStyle = UIBarStyle.default
numberToolbar.items = [
UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.plain, target: self, action: Selector(("cancelNumberPad"))),
UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil),
UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.plain, target: self, action: Selector(("doneWithNumberPad")))]
numberToolbar.sizeToFit()
textField.inputAccessoryView
= numberToolbar
return true
}
#IBAction func didClickDoneButton(_ sender: UIBarButtonItem) {
textField.endEditing(true)
}
#IBAction func buttonClick(_ sender: Any) {
textField.endEditing(true)
}
}

Swift - Call UIToolBar by buttonClick Event

In my viewController there are one button and textField. When use click on textField it fires didBeginEdit event and i can call a toolbar.
This is my view :
MyView
When user click on textField it seems like below :
textFieldClicked
And My Swift code is below :
Class ViewController: UIViewController {
#IBAction func btn(sender: AnyObject) {
// i want to call ToolBar here ...
}
#IBOutlet weak var textFieldOutlet: UITextField!
#IBAction func txtFieldBeginEdit(sender: AnyObject) {
raiseToolBar(textFieldOutlet)
}
var datePicker : UIDatePicker!
let ToolBar = UIToolbar()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
func raiseToolBar(textField : UITextField)
{
self.datePicker = UIDatePicker(frame: CGRectMake(0,0,UIScreen.mainScreen().bounds.height , UIScreen.mainScreen().bounds.height / 3))
self.datePicker.backgroundColor = UIColor.whiteColor()
self.datePicker.datePickerMode = .Date
textField.inputView = self.datePicker
ToolBar.barStyle = .Default
ToolBar.translucent = true
ToolBar.tintColor = UIColor(red : 92/255, green : 216/255 ,blue : 255/255, alpha: 1 )
ToolBar.sizeToFit()
let done = UIBarButtonItem(title: "Tamam" , style: .Plain , target: self, action: #selector(ViewController.doneClick))
let space = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target : nil , action: nil)
let cancel = UIBarButtonItem(title: "İptal" , style: .Plain , target: self, action: #selector(ViewController.cancelClick))
ToolBar.setItems([done , space , cancel], animated: true)
ToolBar.userInteractionEnabled = true
textField.inputAccessoryView = ToolBar
}
func doneClick() {
}
func cancelClick() {
}
}
At this point there is no problem. Bu i want to call this UIToolBar by clic on the button(named btn). Under btnClick event i tried to send empty textField to raiseToolBar method but it also don't work. How can i edit my code to call ToolBar by buttonClick
**************** edit *****************
textFieldOutlet.becomeFirstResponder()
raiseToolBar(textFieldOutlet)
i solve like above. is it a clear way ?
You can assign first responder to your text field like this inside your btn action:
textFieldOutlet.isFirstResponder = true

Toggle Play/Pause UIBarButtonItem in Toolbar using Swift

I am trying to toggle my button between a play and pause image when I start and stop a ticker using Swift. My code:
import UIKit
class ViewController: UIViewController {
#IBOutlet var btnPlayPause: UIBarButtonItem!
var isPlaying = false
var timer = NSTimer()
var count = 0
#IBOutlet weak var lblTime: UILabel!
#IBOutlet var myToolbar: UIToolbar!
#IBAction func btnPlay(sender: UIBarButtonItem)
{
//set the button to animate
self.myToolbar.setItems([self.btnPlayPause], animated: true)
if !isPlaying //if the ticker is not ticking
{
//change the button to a pause button
println("worked")//start the ticker
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("updateTime"), userInfo: nil, repeats: true)
isPlaying = true
}else{ //if the ticker is ticking
//change the pause button to a play button
self.btnPlayPause = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Play, target: self, action: nil)
//pause the ticker
timer.invalidate()
//btnPlayPause.enabled = true
isPlaying = false
}
}
#IBAction func btnReset(sender: UIBarButtonItem)
{
//reset and restart the ticker
timer.invalidate()
count = 0
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("updateTime"), userInfo: nil, repeats: true)
}
#IBAction func btnStopit(sender: UIBarButtonItem)
{
//stop and reset the ticker to "0"
timer.invalidate()
count = 0
lblTime.text = String(count)
isPlaying = false
}
func updateTime()
{
//displays ticker label with count
lblTime.text = String(count++)
}
override func viewDidLoad()
{
super.viewDidLoad()
let button = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Pause, target: self, action: "btnStopit:")
self.btnPlayPause = button
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
}
The Play and all the other buttons on the toolbar are clearing and the toolbar is creating the Pause button alone by itself like this:
I want to just toggle my Play button with the Pause button without removing any of the other buttons from the toolbar. Is this possible?
If anyone could help me out with this I'd greatly appreciate it!
Thanks
Casting your button to a new instance of UIBarButtonItem isn't gonna do the needful. First create an outlet for your toolbar and retrieve the existing items, then change the play button item according to its position on the toolbar.
In my case the toggle button is on the left so I access it with index 0 and replace it with my corresponding toggle. And then calling the setItems() on your toolbar will update your toolbar. Set animation to true for a nice little fade animation.
#IBOutlet weak var toolBar: UIToolbar!
#IBAction func playPauseToggle(sender: UIBarButtonItem) {
var toggleBtn = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Play, target: self, action: "playPauseToggle:")
if playing {
player.pause()
playing = false
} else {
player.play()
toggleBtn = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Pause, target: self, action: "playPauseToggle:")
playing = true
}
var items = toolBar.items!
items[0] = toggleBtn
toolBar.setItems(items, animated: true)
}
slightly more dense version:
#IBAction func playPauseToggle(sender: UIBarButtonItem) {
var barButtonItems = toolBar.items!
barButtonItems[0] = UIBarButtonItem(barButtonSystemItem: player.rate == 1.0 ? .Pause : .Play,
target: self, action: "playPauseButtonWasPressed:")
toolBar.setItems(barButtonItems, animated: true)
}
//This code is tested and working with Swift 2
#IBOutlet weak var navigationBar: UINavigationBar!
//playToPause()
#IBAction func playButton(sender: UIBarButtonItem) {
let newBarButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Pause, target: self, action: "pauseButton:")
navigationBar.topItem?.rightBarButtonItem = newBarButton
}
// pauseToPlay()
#IBAction func pauseButton(sender: UIBarButtonItem){
let pauseBtnItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Play, target: self, action: "playButton:")
navigationBar.topItem!.rightBarButtonItem = pauseBtnItem
}