I have a tableview with an edit button on the left.
I created this edit button using
self.navigationItem.leftBarButtonItem = self.editButtonItem;
I want to know how to change the title of the Edit button to 'delete'
Cheers
Changing the visual title of the edit button to 'Delete' is a bad idea -- you'll probably find your app gets rejected for doing something as ill advised as that. If it does get into the store, you'll confuse your users. Don't do it!
If you want a button called Delete that does something specific directly related to deleting that has a clear context, that's another matter. Note that destructive action buttons are usually a red colour, to signal to the user that there's a destructive action there, and sometimes pop up a UIActionSheet to confirm the user's destructive action.
Please read Apple' Human Interface Guidelines:
http://developer.apple.com/library/ios/documentation/userexperience/conceptual/mobilehig/Introduction/Introduction.html
You can just change button title. Here is the code
self.editButtonItem.title = #"Delete";
self.navigationItem.leftBarButtonItem = self.editButtonItem;
Doable, though it might not be a good idea. See this answer by occulus.
Create a UIBarButtonItem as usual:
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:#"Whatever" style: UIBarButtonItemStyleBordered target:self action:#selector(doSomething)] autorelease];
Then call [self setEditing:!self.editing] in -(void)doSomething.
Try
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"your title" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;
[backButton release];
Related
how can I implement "Next" and "Previous" Button's over the keyboard.
I use UITextView and with click on "Next" the Next TextView gets focus, Previous the last TextView.
I do not found any tutorial on the Internet.
I hope everybody can help me.
Please use this custom control
COCOA
If you need any help, let me know.
This controlis very simple to use.
Updated Answer
Change the Button Label
Go to BSKeyboardControls.m file & make the following Changes
On line no. 42
[self setSegmentedControl:[[UISegmentedControl alloc] initWithItems:#[ NSLocalizedStringFromTable(#"<", #"BSKeyboardControls", #"Previous button title."),
NSLocalizedStringFromTable(#">", #"BSKeyboardControls", #"Next button title.") ]]];
On line No. 51
[self setDoneButton:[[UIBarButtonItem alloc] initWithTitle:NSLocalizedStringFromTable(#"Ok", #"BSKeyboardControls", #"Done button title.")
style:UIBarButtonItemStyleDone
target:self
action:#selector(doneButtonPressed:)]];
///// ********* Update 2 ********* ////////////
SET Image for Previous & Next
Just add the following code in BSKeyboardControls.m after line no. 52
[self.segmentedControl setImage:[UIImage imageNamed:#"add.png"] forSegmentAtIndex:BSKeyboardControlsDirectionPrevious];
[self.segmentedControl setImage:[UIImage imageNamed:#"add.png"] forSegmentAtIndex:BSKeyboardControlsDirectionNext];
change BSKeyboardControls.m file & make the following Changes
remove this one
// [self setRightArrowButton:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:106 target:self action:#selector(selectNextField)]];
add this
[self setRightArrowButton:[[UIBarButtonItem alloc]initWithTitle:#"Next" style:UIBarButtonItemStylePlain target:self action:#selector(selectNextField)]];
I'm updating some old code, and to make more room in a toolbar, I'm converting the Buttons from test to images. An example of the new and old code in loadView is this:
// New code, doesn't work.
UIButton *toggleKeyboardBtn = [UIButton buttonWithType:UIButtonTypeCustom];
toggleKeyboardBtn.bounds = CGRectMake( 0, 0, showKeyboardImage.size.width, showKeyboardImage.size.height );
[toggleKeyboardBtn setImage:showKeyboardImage forState:UIControlStateNormal];
UIBarButtonItem *toggleKeyboardItem = [[UIBarButtonItem alloc] initWithCustomView:toggleKeyboardBtn];
[toggleKeyboardItem setTarget:self];
[toggleKeyboardItem setAction:#selector(toggleKeyboard:)];
// Original code, works jut fine.
UIBarButtonItem *setupItem = [[[UIBarButtonItem alloc] initWithTitle:#"Setup" style:UIBarButtonItemStyleBordered target:[UIApplication sharedApplication].delegate action:#selector(showSetupView:)] autorelease];
My new code is copied from Cannot set action on UIBarButtonItem, and I'm fairly certain that I'm not making their mistake since my text button is working just fine.
showSetupView() is in my AppController.m file, and the setup screen appears and disappears as the button is pressed.
toggleKeyboard(), OTOH, is in the same file as the loadView() routine, and currently consists of this code:
//- (void)toggleKeyboard {
- (IBAction)toggleKeyboard:(id)sender {
NSLog(#"Entering toggleKeyboard()...");
hiddenKeyboard = !hiddenKeyboard;
[self prepareToolbarsAndStatusbar];
}
Needless to say, although I see the button-press animation, I never see the NSLog message. And one last observation, made by accident. Changing the setAction selector to this:
[toggleKeyboardItem setAction:#selector(noSuchRoutine:)];
compiles cleanly, possibly indicating that my routine name is being ignored for some reason.
Anyone have any ideas? Thanks.
I found the answer! In button action not responding iphone, it's said that the action and target need to be set on the UIButton, not the UIBarButtonItem. I don't know if that's new with the latest version of Xcode, but I guess it is since other questions (such as the one I mention above) use a different technique. Here's my new code:
UIButton *toggleKeyboardButton = [UIButton buttonWithType:UIButtonTypeCustom];
toggleKeyboardButton.bounds = CGRectMake( 0, 0, keyboardAddImage.size.width, keyboardAddImage.size.height );
[toggleKeyboardButton setImage:keyboardAddImage forState:UIControlStateNormal];
[toggleKeyboardButton addTarget:self action:#selector(toggleKeyboard) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *toggleKeyboardItem = [[UIBarButtonItem alloc] initWithCustomView:toggleKeyboardButton];
//[toggleKeyboardItem setTarget:self];
//[toggleKeyboardItem setAction:#selector(toggleKeyboard:)];
Though its too late, but for future references, I would like to quote apple docs for the method,
- (instancetype)initWithCustomView:(UIView *)customView;
The bar button item created by this method does not call
the action method of its target in response to user interactions.
Instead, the bar button item expects the specified custom view to
handle any user interactions and provide an appropriate response.
Did you try
-(void)toggleKeyboard
and
[toggleKeyboardItem setAction:#selector(toggleKeyboard)]; without :
and it made any difference? Is the method declared in the interface file?
Ok so at the moment i have a navigation controller with a right button which logs a user in.
however I want to change the login button to logout once it has been clicked and this logout button will call a different IBAction.
to help visualize this.
As default, I have a right button on the navigation controller which says login, once this login button is pressed the ibaction login:(id)sender is pressed.
what i want to do is change the button to logout and call logout:(id)sender when it is clicked.
is this possible.
thanks.
You can just change what the button does when it is pressed:
- (void)login:(UIButton*)button {
[button setTitle:#"Logout"];
[button setAction:#selector(logout:)];
}
- (void)logout:(UIButton*)button {
[button setTitle:#"Login"];
[button setAction:#selector(login:)];
}
Alternately, since the button is on the UINavigationBar, you could instead do this:
-(IBAction)login:(id)sender{
UIBarButtonItem *logoutButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:#selector(logout:)];
[[self navigationItem] setRightBarButtonItem:logoutButton];
[logoutButton release];
}
-(IBAction)logout:(id)sender{
UIBarButtonItem *loginButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:#selector(login:)];
[[self navigationItem] setRightBarButtonItem:loginButton];
[loginButton release];
}
You can set two different titles or image for the button, one for normal/default state and other for selected state.
Similarly, you can set two different operation based on the state of the button.
So, if button is in normal/default state, user needs to login. Once user does login, we put the button into selected state.
Thus, if button is in selected state, user needs to log out.
In this way, we can keep toggling the states of the same button, to do two different operations.
The code would look something like this.
-(IBAction) loginButtonPressed:(id) sender {
UIButton *loginButton = (UIButton *) sender;
if (loginButton.selected == NO) {
// Represents user needs to login. Code for login user.
}else
// Represents user needs to logout. Code for logout user.
}
// toggle the login/logout states.
loginButton.selected = !loginButton.selected;
}
You can specify titles/images for the button for normal/selected/highlighted/disabled states.
This would do your job with single button.
You can change the target action by using addTarget and removeTarget, as stated in this related question.
The problem here is to have an elegant way to detect the status of being logged in or not. You can think of a global variable, but it's a bad solution most of the times.
Another alternative is inspecting the UIButton's NSString *title property.
A nice approach is to have a sort of 'session' mechanism as in web applications, which will give you the status application-wide.
I want to have a keyboard which has a Next,Previous and Done button on top of it.
I have seen that in many apps.
Especially where there are forms to be filled.
I want to achieve something similar to above keyboard
How can I get that?
You'll find the answer on this other post.
I checked the iOS Library and the inputAccessoryView of a UITextField is exactly what you're looking for !
Hope this helps !
I just created a class called BSKeyboardControls which makes it very easy to add the controls to a keyboard. The class, instructions and example code can be found here at GitHub.
The controls works for text fields and text views and are optimized for both iPhone and iPad.
-(BOOL)textFieldShouldBeginEditing: (UITextField *)textField
{
UIToolbar * keyboardToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
keyboardToolBar.barStyle = UIBarStyleDefault;
[keyboardToolBar setItems: [NSArray arrayWithObjects:
[[UIBarButtonItem alloc]initWithTitle:#"Previous" style:UIBarButtonItemStyleBordered target:self action:#selector(previousTextField)],
[[UIBarButtonItem alloc] initWithTitle:#"Next" style:UIBarButtonItemStyleBordered target:self action:#selector(nextTextField)],
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(resignKeyboard)],
nil]];
textField.inputAccessoryView = keyboardToolBar;
}
- (void)nextTextField {
if (textField1) {
[textField1 resignFirstResponder];
[textField2 becomeFirstResponder];
}
}
-(void)previousTextField
{
if (textField2) {
[textField2 resignFirstResponder];
[textField1 becomeFirstResponder];
}
}
-(void)resignKeyboard {
[textField1 resignFirstResponder];
[textField2 resignFirstResponder];
}
I have a utility class that basically does this for you.
https://github.com/kalvish21/CustomKeyboard
The idea is very simple. You have to add an accessory tool bar with bar button items on it. There's a delegate which defines where what that button will do.
https://github.com/hackiftekhar/IQKeyboardManager
This is the best keyboard handler I have seen so far. Very excellent way to manage Text inputs.
Some of its features
1) ZERO LINE OF CODE
2) Works Automatically
3) No More UIScrollView
4) No More Subclasses
5) No More Manual Work
6) No More #imports
This is a custom control which is placed directly above the keyboard. I think a UIToolbar can be used for that.
Previous and next passes around the firstResponder of the textFields and Done will do the resign as well as hide the toolbar.
To match the keyboard animation have a look at this code I found or at SO: "What is the iPhone's default keyboard animation rate?"
I have created a repository with an implementation of this feature that works on iPhone/iPad in all orientations and highly customizable.
As mentioned in other answers, it's the inputAccessoryView that you're looking for.
I would like to add an alternative way here, by using this cocoapods:
pod 'UITextField-Navigation'
All UITextFields will have two additional properties: nextTextField and previousTextField. You can then simply connect the nextTextField in the Interface Builder and the Previous, Next and Done buttons are added automatically for you, all functional, no more code is needed, no subclassing.
You can also customize the UI, add more buttons, etc as you want to.
I my application i have added button in NavigationBar like this..
UIBarButtonItem *more=[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"search-25by25.png"] style:UIBarButtonItemStylePlain target:self action:#selector(SelectMission:)];
self.navigationItem.rightBarButtonItem = more;
When i am clicking on button application get's shutdown...
If i am doing same thing with normal button it's working fine can any one help me why it's behaving like this?
Try This
UIImage *i=[UIImage
imageNamed:#"search-25by25.png"];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.bounds = CGRectMake( 0, 0, i.size.width, i.size.height );
[myButton setImage:i forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(SelectMission:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *more=[[UIBarButtonItem alloc] initWithCustomView:myButton];
self.navigationItem.rightBarButtonItem
= more;
hope it helps :)
Have you looked in the code for SelectMission:? The code you've posted is only for presenting the button, which from your description appears to be working.
Also if there's anything being dumped into the console (Command-Shift-R)?
Judging by the crash log in your comment, I would say this has nothing to do with the UIBarButtonItem class in particular, and everything to do with your action handler. The crash logs tell the whole story: Your class does not implement a method called SelectMission: that takes one argument. Some caveats about the #selector keyword that you will want to double check:
1) Capitalization. Make sure that the method you implement is SelectMission:. Not selectMission:, selectmission:, Selectmission:, etc.
2) Arguments. The colon indicates that the method SelectMission: takes one argument. If you have implemented it and forgotten the argument it will crash with the exception you posted.
That should help narrow down the issue.