iPhone SDK NumberPad custom inputView for done button - iphone

Currently I have a textfield, which pulls up a NumberPad keyboard for input.
I want the NumberPad keyboard layout, but I need a button to resign that responder.
Is there a way to code the UITextField so that it has a NumberPad keyboard and the space on the left of the keyboard which is blank is replaced with a 'Done', 'Send' or 'Go' button.
Or do I need to create a custom inputView for the UITextField?
I would have thought since there is a blank key on the NumberPad apple would have made it useful for something.

Just try having a UIToolBar with a done button on it as the inputAccessoryView of the text field. In the button click event provide the code to dismiss the keyboard. Think that should suit you.
Or if you want to have a custom button drawn over the keyboard you can search here to get the solution

The empty button is needed if you use phonePad. But you can add inputAccessoryView with 'Apply' and 'Cancel' buttons, and dismiss the number pad with then.
- (void)viewDidLoad
{
[super viewDidLoad];
UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
numberToolbar.barStyle = UIBarStyleBlackTranslucent;
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:#"Apply" style:UIBarButtonItemStyleDone target:self action:#selector(doneWithNumberPad)],
nil];
[numberToolbar sizeToFit];
numberTextField.inputAccessoryView = numberToolbar;
}
-(void)cancelNumberPad{
[numberTextField resignFirstResponder];
numberTextField.text = #"";
}
-(void)doneWithNumberPad{
NSString *numberFromTheKeyboard = numberTextField.text;
[numberTextField resignFirstResponder];
}

You need to do a custom inputview now and assign it to the inputview property of your text field. I am currently researching it, but that is the right, approved way of doing it. It seems overkill just for changing a single button though.

Related

UIActionSheet with UIPickerView and Done, Next, Previous buttons

i am trying to implement an action sheet that contains a picker view and a segmented control bar with a previous button, next button and done button like the image as follows http://imgur.com/8wVMy. I currently am able to make it look like this http://imgur.com/AXn6H. I was wondering if someone could help me get the picker view to sit on the bottom and just make it look a little better. Thanks for any help.
Unless you're targeting very old versions of iOS (i.e. versions earlier than 3.2), the best way to do it is to take a completely different approach.
As of 3.2, any UIResponder (which includes all UIViews) can return a UIView from its inputView property to show that view instead of the keyboard when the view becomes the first responder. This even works for views that normally don't become first responder or don't display the keyboard at all. It's simple:
Design your popup view, as you would any other view.
Ensure that your widget view returns YES from canBecomeFirstResponder.
Ensure that your widget view returns an instance of your popup view from inputView.
More details are available in the documentation.
Also, BTW, if you're on an iPad you should probably use a UIPopoverController to display a UIPickerView instead of either of these methods. Apple may actually require this if you intend to get your app in the app store.
The next and previous buttons are actually showing your images to segmentedController Within a toolbar. To get it You have to define the segmentedController and UIToolbar on. H. Next add the DataSource and UIPickerView
Then in the viewDidLoad create objects and define Their properties. For example:
if (keyboardToolbar == nil) {
keyboardToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
[keyboardToolbar setBarStyle:UIBarStyleBlackTranslucent];
segControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Anterior", #"Siguiente", nil]];
[segControl setSegmentedControlStyle:UISegmentedControlStyleBar];
[segControl setTintColor:[UIColor blackColor]];
segControl.frame = CGRectMake(5, 7, 150, 33);
segControl.momentary = YES;
[segControl addTarget:self action:#selector(segSelected:) forControlEvents:UIControlEventValueChanged];
UIBarButtonItem *extraSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *aceptar = [[UIBarButtonItem alloc] initWithTitle:#"Hecho" style:UIBarButtonItemStyleDone target:self action:#selector(cerrarTeclado:)];
//aceptar.width = 70.0f;
[keyboardToolbar setItems:[[NSArray alloc] initWithObjects: extraSpace, aceptar, nil]];
[keyboardToolbar addSubview:segControl];
}

Changing colours of a UIToolbar + it's buttons when presented in a Popover Controller on the ipad

On both the iPhone and the iPad I have a need to present two buttons on the right hand side of a navigation bar. I'm doing this with the following snippet of code:
UIToolbar *rightBarButtons = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 92, 44.01)];
UIBarButtonItem *send = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction
target:self
action:#selector(send)];
[send setStyle:UIBarButtonItemStyleBordered];
UIBarButtonItem *add = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:#selector(addRecipe:)];
[add setStyle:UIBarButtonItemStyleBordered];
NSArray *buttons = [[NSArray alloc] initWithObjects:send,add,nil];
[send release];
[add release];
[rightBarButtons setItems:buttons];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBarButtons];
[buttons release];
[rightBarButtons release];
On the iPhone the colours are fine, and in landscape mode on the iPad it is fine as they are grey. However in portrait mode the view appears inside a popover controller which has a dark black/blue colour. My buttons and the toolbar show up as the default grey.
How can I make the toolbar buttons match? If you do not use the hack above and just present one button as normal the colour change is handled and I guess I just need to implement that colour change manually, problem is, I can't seem to get the colour to change at all.
This would appear to be a property called barStyle and not tintColor as I previously thought. The simplest solution is to copy the bar style from elsewhere:
[rightBarButtons setBarStyle:self.navigationController.navigationBar.barStyle];
It's then fairly trivial to ensure the style remains correct as the view changes. Although I have to say I quite liked the dark blue black buttons over the silver navigation bar that gave after rotation.

How do you change color/image on the default backBarButtonItem?

I need to change color on the default "self.navigationItem.backBarButtonItem". To accomplish this I´ve created an custom Button class and implemented it like this:
- (void)viewDidLoad {
[super viewDidLoad];
BackButton *blueSaveButton = [[BackButton alloc] init];
UIBarButtonItem *temporaryBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:blueSaveButton];
temporaryBarButtonItem.title = #"Tillbaka";
self.navigationItem.backBarButtonItem = temporaryBarButtonItem;
[temporaryBarButtonItem release];
[blueSaveButton release];
[self gotoLocation];
}
But this has no effect at all on the button.
So how do you manage to do this without "breaking" the default/inherited behavior of the navigationbar?
EDIT: The reason is that the button needs to be corporate branded, so default styles will not do.
BR
If you want the button to be blue (like a Done button) and have the title "Save" you can use one of the built in bar button item types:
UIBarButtonItem *item = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:#selector(myAction:)] autorelease];
Ok found the solution (not so good but it works) do this:
1: Implement the button from this tutorial: www.switchonthecode.com
This will override the default behavior of navigationbar.
2: Implement this in every view that needs the custom button:
osmorphis.blogspot.com
BR

How to change UIBarButtonItem's type in UINaviagationBar at runtime?

I am working on an iPhone's view which composed 3 elements, UITextView, UIToolBar with an UIBarButtonItem.
The goal is, I want UIBarButtonItem change its style from 'edit' (UIBarButtonSystemItemEdit) to 'Done' (UIBarButtonSystemItemDone) and update new selector to new method.
First of all, I have tried following code but it doesn't work:
Could you help me on this idea?
There is a builtin bar button with this behaviour, you get it via the editButtonItem property of a UIViewContoller. Tabbing that button will change the view controller it came from into editing mode, and toggle the button into a done button.
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
If you have added the button through IB then make sure to set the identifier to Custom
Also allocate a button in the .h with appropriate IBOutlet and Property
Synthesize the button in .m
Then in your code do the following:
// Set to done
editButton.style = UIBarButtonItemStyleDone;
editButton.title = #"Done";
// Set back to edit
editButton.style = UIBarButtonItemStyleBordered;
editButton.title = #"Edit";
to change the button the Done button use this
[self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleDone];
to change the button to Edit button use this
[self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleBordered];
I ended up doing something like this. Unfortunately, setting the title directly did not work, for some reason it was nil and would not let me set it to a different value. The self.editButton comes from an IBOutlet with the target and actions set. This code uses ARC. I hope this helps someone.
NSString *title = app.settings.editing
? NSLocalizedString(#"Done", #"")
: NSLocalizedString(#"Edit", #"");
UIBarButtonItemStyle style = app.settings.editing
? UIBarButtonItemStyleDone
: UIBarButtonItemStyleBordered;
UIBarButtonItem *editButton
= [[UIBarButtonItem alloc] initWithTitle:title
style:style
target:self.editButton.target
action:self.editButton.action];
self.navigationItem.rightBarButtonItem = editButton;

Editable TextView with Second NavBar - Text appears, but too late

Editable TextView with Second NavBar - Text appears, but too late.
The app has a single Navigation Controller.
I have an iPhone App that has basically three levels.
Level 1 - Table with category Names
Level 2 - Table with list of items for selected category
Level 3 - Tabbed View with several views, including UITextView for details of item
One to these Tabbed Views with a TextView is editable.
When the user taps in the editable TextView the KeyBoard
appears. User can type in the TextView. Characters appear
as they are typed.
At the top of this Level 3 TextView there is a NavBar (present for all 3 levels with
changes) with a BackButton and a "home->Level1" button on the right.
All works just fine until in the editable TextView I add a second NavigationBar
below the existing NavBar. This second NavBar has two buttons
as well. They are Save/Cancel.
When I click these Save and Cancel buttons the correct action
methods are reached. All is perfect with one exception, The text
which is typed does not appear in the TextView until either
the Save or the Cancel button is touched. The relevant Button setup and
action methods in my TabViewController.m are below. I need to persist this
data.
I thought that getting a Notification from the TextView and the action handleTextChange would do the trick, but no luck. I am stuck.
.........
- (void)loadView {
self.myTextView = [[UITextView alloc] init];
self.myTextView.delegate = self;
self.view = self.myTextView;
//UITextViewTextDidChangeNotification
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self
selector:#selector(handleTextChange:)
name:UITextViewTextDidChangeNotification
object:nil];
NSLog(#"Registered DG_HandleChangeTextNotification with notification center.");
}
- (void)handleTextChange:(NSNotification * )note
{
[self.myTextView setNeedsDisplay] ;
NSLog(#"...Handled Text Change.");
}
- (void)textViewDidBeginEditing:(UITextView *)textView
{
// provide my own Done/Save button to dismiss the keyboard
saveNavigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
saveNavigationBar.barStyle = UIBarStyleBlackOpaque;
UINavigationItem *doneItem = [[UINavigationItem alloc] init];
doneItem.title = #"My Notes";
UIBarButtonItem *doneItemButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemSave
target:self action:#selector(saveAction:)];
UIBarButtonItem *cancelItemButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self
action:#selector(cancelAction:)];
[doneItem setRightBarButtonItem:doneItemButton animated:NO];
[doneItem setLeftBarButtonItem:cancelItemButton animated:NO];
[saveNavigationBar pushNavigationItem:doneItem animated:NO];
[self.view addSubview:saveNavigationBar];
[doneItem release];
[cancelItemButton release];
[doneItemButton release];
}
- (void)saveAction:(id)sender
{
// finish typing text/dismiss the keyboard by removing it as the first responder
self.text = self.myTextView.text;
[self.saveNavigationBar removeFromSuperview];
[self.myTextView resignFirstResponder];
}
- (void)cancelAction:(id)sender
{
[self.saveNavigationBar removeFromSuperview];
[self.myTextView resignFirstResponder];
}
The Second NavBar was hiding the area of the UITextEdit
such that I had to type about four lines before I saw the text. I believe
I need to lower the height of the UITextEdit by 44 pixels.