Does changing a button's title prevent further action? - ios

I have a bar button, let's call it button 1, which when clicked, programatically segues to a different page.
However, after clicking another button (button 2), button 1's title is changed.
From then on, button 1 no longer responds regardless of whether it's title has been changed back by clicking button 2 again.
I hope you understand, if not - I can clarify
code: (button 2 is edit) - (1 is info/add)
-(void)editButton:(id)sender
{
[self setEditing:(![self isEditing])];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style: UIBarButtonItemStylePlain
target:self
action:#selector(edit:)];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Add"
style: UIBarButtonItemStylePlain
target:self
action:#selector(infoToAdd:)];
}
-(void) edit:(UIBarButtonItem *) barBtnItem
{
// if not editing
if (![self isEditing]) {
[self setEditing:YES];
[self.navigationItem.rightBarButtonItem setTitle:#"Done"];
[self.navigationItem.leftBarButtonItem setTitle:#"Add"];
} else {
[self setEditing:NO];
[self.navigationItem.rightBarButtonItem setTitle:#"Edit"];
[self.navigationItem.leftBarButtonItem setTitle:#"Info"];
}
}
-(void) infoToAdd:(UIBarButtonItem *) barBtnItem
{
// if not editing
if (![self isEditing]) {
[self.navigationItem.leftBarButtonItem setTitle:#"Info"];
} else {
[self.navigationItem.leftBarButtonItem setTitle:#"Add"];
}
}
-(void)infoAddButton:(id)sender
{
if (self.tableView.isEditing) {
[self performSegueWithIdentifier:#"segueToAdd" sender:self];
} else {
[self performSegueWithIdentifier:#"segueToInfo" sender:self];
}
}
Edit:
It seems the function 'infoAddButton' is not even getting called (I used a breakpoint) when I press the button, after changing it's title.

i think this is help you. here explain only left button same logic you can implement for right button
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Menu" style:UIBarButtonItemStyleBordered target:self action:#selector(dosomeTask:)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"show" style:UIBarButtonItemStyleBordered target:self action:#selector(dosomeTask1:)];
isValid is a Bool type. so initially in your viewDidLoad set NO
-(void)dosomeTask:(id)sender{
if (isValid) {
self.navigationItem.leftBarButtonItem.title=#"Menu";
isValid=NO;
[self infomationShow];
}else{
self.navigationItem.leftBarButtonItem.title=#"show";
isValid=YES;
[self menuController];
}
}
-(void)menuController{
NSLog(#"menu");
}
-(void)infomationShow{
NSLog(#"infomationShow");
}

Related

UIBarButtonSystemItemSave width unequal to UIBarButtonSystemItemCancel

I have seven UIBarButtonItems in a UIToolbar.
UIBarButtonSystemItemFixedSpace
UIBarButtonSystemItemCancel
UIBarButtonSystemItemFlexibleSpace
UIBarButton with custom text
UIBarButtonSystemItemFlexibleSpace
UIBarButtonSystemItemSave
UIBarButtonSystemItemFixedSpace
Everything is laying out as I would normally expect it to except for the UIBarButtonSystemItemSave item. It has an extra space next to it.
I added borders to each subview inside of the toolbar to try to figure out what was happening but still couldn't quite figure it out.
Maybe there was something happening with the order of the buttons so I switched the items around in order, but still couldn't figure it out.
Just in case it was something about the way I created the button, I changed the init option to UIBarButtonSystemItemCancel for both Save and Cancel.
Here's the code that I've been using.
- (UIToolbar *)headerToolbar {
if (!_headerToolbar) {
_headerToolbar = [[UIToolbar alloc] initWithFrame:self.headerToolbarFrame];
_headerToolbar.barTintColor = [[OVThemeManager shared] blueColor];
if (UIApplication.sharedApplication.userInterfaceLayoutDirection == UIUserInterfaceLayoutDirectionLeftToRight) {
_headerToolbar.items = #[[self fixedSpace:buttonBufferWidth],
self.cancelButton,
self.flexibleSpace,
self.titleButton,
self.flexibleSpace,
self.saveButton,
[self fixedSpace:buttonBufferWidth]];
} else {
_headerToolbar.items = #[[self fixedSpace:buttonBufferWidth],
self.saveButton,
self.flexibleSpace,
self.titleButton,
self.flexibleSpace,
self.cancelButton,
[self fixedSpace:buttonBufferWidth]];
}
[self borderSubviews:self.headerToolbar];
}
return _headerToolbar;
}
- (void)borderSubviews:(UIView *)superview {
superview.layer.borderWidth = 1.0f;
superview.layer.borderColor = [UIColor colorWithRed:0.001f *(float)(arc4random()%1000)
green:0.001f *(float)(arc4random()%1000)
blue:0.001f *(float)(arc4random()%1000)
alpha:1.0f].CGColor;
for (UIView *subview in superview.subviews) {
[self borderSubviews:subview];
}
}
- (CGRect)headerToolbarFrame {
CGRect frame = self.view.bounds;
frame.size.height = headerToolbarHeight;
return frame;
}
- (UIBarButtonItem *)saveButton {
if (!_saveButton) {
_saveButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave
target:self
action:#selector(saveButtonTouched:)];
_saveButton.tintColor = [[OVThemeManager shared] blueColor];
}
return _saveButton;
}
- (UIBarButtonItem *)cancelButton {
if (!_cancelButton) {
_cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:#selector(cancelButtonTouched:)];
_cancelButton.tintColor = [[OVThemeManager shared] blueColor];
}
return _cancelButton;
}
- (UIBarButtonItem *)titleButton {
if (!_titleButton) {
_titleButton = [[UIBarButtonItem alloc] initWithTitle:#"Other button".localized
style:UIBarButtonItemStylePlain
target:nil
action:nil];
_titleButton.tintColor = [[OVThemeManager shared] blueColor];
}
return _titleButton;
}
- (UIBarButtonItem *)flexibleSpace {
return [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
}
- (UIBarButtonItem *)fixedSpace:(CGFloat)width {
UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil
action:nil];
fixedSpace.width = width;
return fixedSpace;
}

How to use textFieldShouldReturn method as selector action?

On the right is picker view toolbar button 'Next' and want to call textFieldShouldReturn method on its action via #selector. But it gives me error Undeclared Selector.
This is button's code,
UIBarButtonItem *nextBtn = [[UIBarButtonItem alloc] initWithTitle:#"Next" style:UIBarButtonItemStylePlain target:self action:#selector("textFieldShouldReturn here")];
And this is my textFieldShouldReturn method,
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
self.addScrollView.contentSize = CGSizeMake(320, 1800);
[self.addScrollView setContentOffset:CGPointMake(0, 0 ) animated:NO];
if (textField == industryTxtField) {
[subIndustryTxtField becomeFirstResponder];
}
else if (textField == subIndustryTxtField) {
[address1TxtField becomeFirstResponder];
}
ScreenShot
Try like this ...
UIBarButtonItem *nextButton = [[UIBarButtonItem alloc] initWithTitle:#"Next" style:UIBarButtonItemStylePlain target:self action:#selector(moveToNextField)];
-(void)moveToNextField
{
if (industryTxtField.isFirstResponder)
{
[subIndustryTxtField becomeFirstResponder];
}
else if (subIndustryTxtField.isFirstResponder)
{
[address1TxtField becomeFirstResponder];
}
}

UIBarButtonItem in navigation bar for open side menu doesn't work

I am using UIBarButtonItem in the navigation bar for open side menu in my project but it does not work.
Here is the code which I implemented:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.title=#"Add Money";
[self setupMenuBarButtonItems];
//initData
}
- (void)setupMenuBarButtonItems {
// self.navigationItem.rightBarButtonItem = [self rightMenuBarButtonItem];
if(self.menuContainerViewController.menuState == MFSideMenuStateClosed &&
![[self.navigationController.viewControllers objectAtIndex:0] isEqual:self]) {
// self.navigationItem.leftBarButtonItem = [self backBarButtonItem];
} else {
self.navigationItem.leftBarButtonItem = [self leftMenuBarButtonItem];
}
}
- (UIBarButtonItem *)leftMenuBarButtonItem {
return [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:#"menu.png"] style:UIBarButtonItemStyleBordered
target:self
action:#selector(leftSideMenuButtonPressed:)];
}
- (void)leftSideMenuButtonPressed:(id)sender {
[self.menuContainerViewController toggleLeftSideMenuCompletion:^{
[self setupMenuBarButtonItems];
}];
}

get UITextField Tag in Custom Methods

In my Project I have two UITextFields, one is used for Postal Code another one is used for Phone Number , so I used the Keyboard in NumberPad, Here I used the UIToolbar for both UITextFields for hide values, but my problem is TextField is does not identify the correct Tags in custom Methods for hide the textfields. How to resolve this issue
and my code is
- (void)viewDidLoad
{
[super viewDidLoad];
numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
numberToolbar.items = [NSArray arrayWithObjects:
[[UIBarButtonItem alloc]initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(cancelNumberPad:)],
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(doneWithNumberPad:)],
nil];
}
- (IBAction)cancelNumberPad:(UITextField*)textField {
NSLog(#"The user is typing in text field %d",textField.tag);
if (textField.tag==50) {
[txtPostalCode resignFirstResponder];
txtPostalCode.text=#"";
}
else
{
[txtphoneno resignFirstResponder];
txtphoneno.text = #"";
}
}
- (IBAction)doneWithNumberPad:(UITextField*)textField {
NSLog(#"The user is typing in text field %d",textField.tag);
if (textField==txtPostalCode) {
[txtPostalCode resignFirstResponder];
}
else
{
[txtphoneno resignFirstResponder];
}
}
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
if (textField.tag==50) {
textField.inputAccessoryView = numberToolbar;
}
else if
(textField.tag==5) {
textField.inputAccessoryView = numberToolbar;
}
}
in my console report is
2014-05-03 14:06:23.614 why-Q[2000:60b] The user is typing in text field 0
how to get the correct tag in custom methods
//yourviewcontroller.h
#interface ViewController : UIViewController {
int tag;
}
//yourviewcontroller.m
- (void)viewDidLoad {
[super viewDidLoad];
tag = 0;
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField// return NO to disallow editing.
{
tag = textField.tag;
return YES;
}
- (IBAction)cancelNumberPad:(UITextField*)textField {
NSLog(#"The user is typing in text field %d",tag);
}
- (IBAction)doneWithNumberPad:(UITextField*)textField {
NSLog(#"The user is typing in text field %d",tag);
}

UIBarButtonItem changing title not working

How can I change the title of a UIBarButtonItem? I have the following code which is called when an edit button is pressed on my UINavigationBar.
-(void)editButtonSelected:(id)sender {
NSLog(#"edit button selected!");
if(editing) {
NSLog(#"notediting");
[super setEditing:NO animated:NO];
[tableView setEditing:NO animated:NO];
[tableView reloadData];
[rightButtonItem setTitle:#"Edit"];
[rightButtonItem setStyle:UIBarButtonItemStylePlain];
editing = false;
}
else {
NSLog(#"editing");
[super setEditing:YES animated:YES];
[tableView setEditing:YES animated:YES];
[tableView reloadData];
[rightButtonItem setTitle:#"Done"];
[rightButtonItem setStyle:UIBarButtonItemStyleDone];
editing = true;
}
}
The edit button is changing color (so the line which sets the style is working), however the line which sets the title of the button is 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.
I had this problem and resolved it by setting the UIBarButtonItem style to the custom type when it's initialised. Then the titles would set when changing their title values.
You may also want to set the possibleTitle value in the viewDidLoad method to ensure the button is sized correctly for all the possible titles it can have.
In my case what prevented the title being displayed was that in the xib I'd selected the Bar button item 'identifier' property as 'Cancel'.
I tried setting the title property even before assigning the button to the navigation bar, but the title was not being updated.
I made it like this:
And it started working just as I wanted.
If you look at the documentation of the title property, it is explicitly mentioned that you should set it before assigning it to the navigation bar. Instead of doing what you're doing right now, you can use two bar button items – one for done and one for edit, and set them alternatively.
Actually if all you want if switching between "Edit" and "Done", just use
self.navigationItem.rightBarButtonItem = self.editButtonItem;
It will handle this transition for you
Just switch out the buttons each time the user presses them.
- (void)setNavigationButtonAsEdit
{
UIBarButtonItem* editButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(#"Edit", #"")
style:UIBarButtonItemStylePlain
target:self
action:#selector(editButtonPressed:)];
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObject:editButton];
}
- (void)setNavigationButtonAsDone
{
UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(#"Done", #"")
style:UIBarButtonItemStylePlain
target:self
action:#selector(editButtonPressed:)];
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObject:doneButton];
}
And then create an IBAction to handle the button press:
- (IBAction)editButtonPressed:(id)sender
{
NSString* buttonText = [sender title];
if ([buttonText isEqualToString:NSLocalizedString(#"Edit",#"")])
{
[self setNavigationButtonAsDone];
}
else
{
[self setNavigationButtonAsEdit];
}
}
Swift 3.0, 3.2, or 4.0
If your custom ViewController containing a tableView object follows the UITableViewController Delegate and Datasource protocols, you can append a system editBarButtonItem to your navigationItem.leftBarButtonItem(s) or navigationItem.rightBarButtonItem(s) with the following code:
func setupTableView() {
tableView.delegate = self
tableView.dataSource = self
navigationItem.rightBarButtonItem = editButtonItem
editButtonItem.action = #selector(CustomViewController.editTableView(_:))
}
#objc func editTableView(_ sender: UIBarButtonItem) {
tableView.isEditing = !tableView.isEditing
if tableView.isEditing {
sender.title = "Done"
} else {
sender.title = "Edit"
}
}
Try:
UIButton *rightButton;
rightButton = (UIButton*)self.navigationItem.rightBarButtonItem.customView;
[rightButton setTitle:#"Done" forState:UIControlStateNormal];
Because rightBarButtonItem.customView == “the button your added”.

Resources