Uibarbutton Not Changing

UIBarButtonItem not updating

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refreshOrderList:)];

You cannot initalise an already initialised object and expect it to work properly. Create a new bar button item and set it as the rightBarButtonItem.

UIBarButtonItem selector not working

try with inside scope once

override func viewDidLoad() {
super.viewDidLoad()

let rightButton = UIBarButtonItem(title: "Right", style: .plain, target: self, action: #selector(self.onRightLeftClick(_ :)))
rightButton.tag = 1
let leftButton = UIBarButtonItem(title: "Left", style: .plain, target: self, action: #selector(self.onRightLeftClick(_ :)))
rightButton.tag = 2
self.navigationItem.rightBarButtonItem = rightButton
self.navigationItem.leftBarButtonItem = leftButton
}

handle the action as

func onRightLeftClick(_ sender: UIBarButtonItem){
if sender.tag == 1{
// rightButton action
}else{
// leftButton action
}
}

UIBarButtonItem changing title not working

I've done the following to dynamically change the title of a UIBarButtonItem. In this situation I am not using a UIViewTableController and cannot use the standard editButton. I have a view with a tableView as well as other subviews and wanted to emulate the behavior of the limited UIViewTableController.

- (void)InitializeNavigationItem
{
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:2];
UIBarButtonItem* barButton;

barButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:@selector(addNewItem:)];
barButton.style = UIBarButtonItemStyleBordered;
[array addObject:barButton];

// --------------------------------------------------------------------------------------

barButton = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UIBarButtonItemStyleBordered
target:self
action:@selector(editMode:)];
barButton.style = UIBarButtonItemStyleBordered;
barButton.possibleTitles = [NSSet setWithObjects:@"Edit", @"Done", nil];
[array addObject:barButton];

self.navigationItem.rightBarButtonItems = array;
}

- (IBAction)editMode:(UIBarButtonItem *)sender
{
if (self.orderTable.editing)
{
sender.title = @"Edit";
[self.orderTable setEditing:NO animated:YES];
}
else
{
sender.title = @"Done";
[self.orderTable setEditing:YES animated:YES];
}
}

Note that I didn't use the the UIBarButtonSystemItemEdit barButton, you cannot manually change the name of that button, which makes sense.

You also might want to take advantage of the possibleTitles property so that the button doesn't resize when you change the title.

If you are using a Storyboard/XIB to create/set these buttons, ensure that the Bar Button Item Identifier is set to Custom for the button which you'd want to control the title for.

Setting UIBarButtonItem not changing title, changing only style

Just do this

UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style: UIBarButtonItemStyleBordered target:self action:@selector(edit:)];
barButton.tag = 0;
barButton.possibleTitles = [NSSet setWithObjects:@"Edit", @"Done", nil];

Just change state according to click. simple thing.

-(void) edit:(UIBarButtonItem *) barBtnItem
{
if (barBtnItem.tag == 0)
{
barBtnItem.tag = 1;
[self.navigationItem.rightBarButtonItem setTitle:@"Done"];
}
else
{
barBtnItem.tag = 0;
[self.navigationItem.rightBarButtonItem setTitle:@"Edit"];
}
}

UIBarButton added to navigation not working on iOS 13

Assign the target to self

syncButton.target = self

OR

customBu.addTarget(self, action: #selector(someMethod), for: .touchUpInside)

and set it as a customView

iOS11 UIBarButtonItem not working

Had the same issue. It came down to an extension on UIButton in a file called "UIButton+MinimumTouchArea.swift" in our project which overrides UIButton.hitTest and breaks UIBarButtonItem in iOS 11. It took us a the whole day to figure out!

Swift | UIBarButton not working / not going to action target

The target (self) is still nil when you initialize the volumeButton like you do. Make the button a lazy var instead:

lazy var volumeButton = UIBarButtonItem(image: UIImage(named: "speaker"), style: .plain , target: self, action: #selector(volumeButtonTapped))

As @Robert Dresler stated you also have to replace the second if statement in the volumeButtonTapped function with an else if to make your code work - although this has nothing to do with the target action problem.

You could also make your code a little bit cleaner by using a solution similar to this:

var deviceIsMuted = false {
didSet {
volumeButton.image = UIImage(named: deviceIsMuted ? "mute" : "speaker")
print("volume set to", deviceIsMuted ? "mute" : "loud")
}
}

@objc func volumeButtonTapped(_ sender: Any) {
deviceIsMuted = !deviceIsMuted
}

UIBarButtonItem is not working swift 3.0

Try this:

override func viewDidLoad() {
super.viewDidLoad()

self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "ButtonName", style: .done, target: self, action: #selector(YourViewController.yourAction))

}


Related Topics



Leave a reply



Submit