user3091828
user3091828

Reputation: 21

Change a Nav Bar button icon in Swift

I'm trying to change the button icon form PLAY to PAUSE when I tap on it. But my code is not working.

This is my @IBAction code.

@IBAction func playTimer(sender: AnyObject) {

    if buttonSwitch == 0 {
        timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("result"), userInfo: nil, repeats: true)
        buttonSwitch = 1

        self.navigationItem.setLeftBarButtonItem(UIBarButtonItem(barButtonSystemItem: .Pause, target: self, action: "setAlarm"), animated: true)

        println("2")

    } else {
        timer.invalidate()
        buttonSwitch = 0
    }

}

I have tried everything but nothing's happening. No errors, and the icon does not change.

Upvotes: 2

Views: 5244

Answers (5)

Bendrix
Bendrix

Reputation: 1043

Here is simple code. Running Xcode 6.3 This code does NOT change the button identifier. Setup is a single view controller. Navigation Bar Item, one Button Bar Item on the left side of the bar. Link the button bar item to the IBAction "playTimer".

import UIKit

class ViewController: UIViewController {

    var buttonSwitch : Int!
    var timer : NSTimer!
    override func viewDidLoad() {
        super.viewDidLoad()
        buttonSwitch = 0;
        self.navigationItem.setLeftBarButtonItem(UIBarButtonItem(barButtonSystemItem: .Play, target: self, action: "playTimer:"), animated: true)
    }

    @IBAction func playTimer(sender: AnyObject) {

        if buttonSwitch == 0 {
            println("buttonSwitch = 0")
            buttonSwitch = 1
            self.navigationItem.setLeftBarButtonItem(UIBarButtonItem(barButtonSystemItem: .Pause, target: self, action: "playTimer:"), animated: true)

        }  else {
            println("buttonSwitch = 1")
            self.navigationItem.setLeftBarButtonItem(UIBarButtonItem(barButtonSystemItem: .Play, target: self, action: "playTimer:"), animated: true)
            buttonSwitch = 0
        }
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

Several posts on this forum have claimed to solve the button item identifier change question with this or similar code, yet I and others who try this code find that it runs, but the button identifier does not change. The two println() statements alternate and display as expected but the button does not change. Not in viewDidLoad and not in playTimer.

PLEASE: Before a moderator deletes this "Answer" as not an answer, consider that it is a cleaned up example answer of those above and purportedly should work. This question appears in at least five posts on this forum, yet not one contains an answer that performs. Please consider allowing this "answer" to stand in the hope that someone clever can take this code and modify it so that the button identifier does change.

Upvotes: 0

NRV
NRV

Reputation: 81

//oh sorry! previously i had given Objective c code..., 

 var imageCheckUI:UIImage = UIImage(named: "checkmark-25.png")!
        var rectCheckFram : CGRect = CGRectMake(-10, 0, 20, 20)
        var BtnCheckUI : UIButton = UIButton(frame: rectCheckFram)
        BtnCheckUI .setBackgroundImage(imageCheckUI, forState: UIControlState.Normal)
        BtnCheckUI.addTarget(self, action: "Correctdata", forControlEvents:.TouchUpInside)


        imageCheckUI = UIImage(named: "appbar.close.png")!
        rectCheckFram  = CGRectMake(-10, 0, 30, 30)
        var BtnCloseUI : UIButton = UIButton(frame: rectCheckFram)
        BtnCloseUI .setBackgroundImage(imageCheckUI, forState: UIControlState.Normal)
        BtnCloseUI.addTarget(self, action: "close", forControlEvents:.TouchUpInside)
        let barbtncheck: UIBarButtonItem = UIBarButtonItem(customView: BtnCheckUI)
        var  barbtnRIght :UIBarButtonItem = UIBarButtonItem(customView: BtnCloseUI)
        let array:NSArray = NSArray(objects: barbtncheck,barbtnRIght)
        self.navigationItem.setRightBarButtonItems(array, animated: true)

Upvotes: 2

Vincent
Vincent

Reputation: 206

Here is working code, Im guessing you forgot the ":" on the action... maybe...

import UIKit


class ViewController: UIViewController {

    var buttonSwitch : Int!
    var timer : NSTimer!
    override func viewDidLoad() {
        super.viewDidLoad()
        buttonSwitch = 0;
        self.navigationItem.setLeftBarButtonItem(UIBarButtonItem(barButtonSystemItem: .Play, target: self, action: "playTimer:"), animated: true)


    }

    @IBAction func playTimer(sender: AnyObject) {
        if buttonSwitch == 0 {
            timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("result"), userInfo: nil, repeats: true)
            buttonSwitch = 1
            self.navigationItem.setLeftBarButtonItem(UIBarButtonItem(barButtonSystemItem: .Pause, target: self, action: "playTimer:"), animated: true)

            println("2")

        } else {
            self.navigationItem.setLeftBarButtonItem(UIBarButtonItem(barButtonSystemItem: .Play, target: self, action: "playTimer:"), animated: true)
            timer.invalidate()
            buttonSwitch = 0
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

Upvotes: 0

NRV
NRV

Reputation: 81

// Use as it iss in you code for swift
UIImage* image3 = [UIImage imageNamed:@"appbar.refresh.png"];
        CGRect frameimg = CGRectMake(-10, 0, 30, 30);
        UIButton *someButton = [[UIButton alloc] initWithFrame:frameimg];
        [someButton setBackgroundImage:image3 forState:UIControlStateNormal];
        [someButton addTarget:self action:@selector(RefresInformation:)forControlEvents:UIControlEventTouchUpInside];
        [someButton setShowsTouchWhenHighlighted:YES];
        UIImage* image4 = [UIImage imageNamed:@"appbar.save.png"];
        CGRect frameimg4 = CGRectMake(0, 0, 30, 30);
        UIButton *someButton4 = [[UIButton alloc] initWithFrame:frameimg4];
        [someButton4 setBackgroundImage:image4 forState:UIControlStateNormal];
        [someButton4 addTarget:self action:@selector(SaveNewProjectRecord:)forControlEvents:UIControlEventTouchUpInside];
        [someButton setShowsTouchWhenHighlighted:YES];
        UIImage* image5 = [UIImage imageNamed:@"appbar.close.png"];
        UIButton *someButton5 = [[UIButton alloc] initWithFrame:frameimg4];
        [someButton5 setBackgroundImage:image5 forState:UIControlStateNormal];
        [someButton5 addTarget:self action:@selector(CancelTransaction:)forControlEvents:UIControlEventTouchUpInside];
        [someButton5 setShowsTouchWhenHighlighted:YES];
        UIBarButtonItem *mailbutton =[[UIBarButtonItem alloc] initWithCustomView:someButton];
        UIBarButtonItem *mailbutton4=[[UIBarButtonItem alloc] initWithCustomView:someButton4];
        UIBarButtonItem *mailbutton5=[[UIBarButtonItem alloc] initWithCustomView:someButton5];
        self.navigationItem.rightBarButtonItems=[[NSArray alloc] initWithObjects:mailbutton5,mailbutton, mailbutton4, nil];

Upvotes: 0

Samaga
Samaga

Reputation: 98

When you say nothing's happened, I'm assuming not even your println is showing. In this case, make sure buttonSwitch is initially set to 0. If this doesn't resolve it, put a break on the first line of the IBAction function and see if it is called when you press the button. If not, there is a problem with your outlet. If so, and the println is working, then there is a problem with your navigationItem reference. In your code, you are switching from play to pause, but you need to put the inverse code in the else statement to switch back (if that's what you want).

Upvotes: 0

Related Questions