"Next" button custom keyboard Objective-C - ios

I've created a custom keyboard using Objective-C. However, I'm stuck on a certain feature and that is being able to implement a "Next" button like that already available on the stock iOS keyboard. (I've attached a screen shot of the stock iOS keyboard below).
Here is my keyboard view controller code that connects my button voids to the actual keyboard :
-(void)addGesturesToKeyboard{
[self.keyboard.globeSwitch addTarget:self action:#selector(advanceToNextInputMode) forControlEvents:UIControlEventTouchUpInside];
[self.keyboard.planA addTarget:self action:#selector(execPlanA) forControlEvents:UIControlEventTouchUpInside];
// what I want to implement -> [self.keyboard.planA addTarget:self action:#selector(textFieldShouldReturn:) forControlEvents:UIControlEventTouchUpInside];
[self.keyboard.planB addTarget:self action:#selector(execPlanB) forControlEvents:UIControlEventTouchUpInside];
[self.keyboard.planC addTarget:self action:#selector(execPlanC) forControlEvents:UIControlEventTouchUpInside];
}
And here is my textFieldshouldReturn method that doesn't seem to be working with me :
-(void)textFieldShouldReturn:(UITextField*)textField
{
NSInteger nextTag = textField.tag + 1;
// Try to find next responder
UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
if (nextResponder) {
// Found next responder, so set it.
[nextResponder becomeFirstResponder];
} else {
// Not found, so remove keyboard.
[textField resignFirstResponder];
}
}

Related

How can I make UIButton disable which is created programmatically

I created some buttons in my code. I want to set it disable but I'm not sure how. here's my code.
UIButton *btn_levels = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn_levels.frame=CGRectMake(x-10, y, 40, 40);
[btn_levels setTitle:[beginer_lvl objectAtIndex:i] forState:UIControlStateNormal];
[btn_levels addTarget:self action:#selector(btn_Method) forControlEvents:UIControlEventTouchUpInside];
btn_levels.tag =i;
btn_levels.backgroundColor=[UIColor blackColor];
btn_levels.tintColor=[UIColor cyanColor];
NSLog(#"btn nm=%#",[beginer_lvl objectAtIndex:i]);
[self.scroll addSubview:btn_levels];
and that is the button method but I don't know what to do...
-(void)btn_Method
{
//to make button disable
}
Modify this line
[btn_levels addTarget:self action:#selector(btn_Method:) forControlEvents:UIControlEventTouchUpInside];
and this method
-(void)btn_Method:(UIButton*)sender
{
sender.enabled = NO;
}
change the statement
[btn_levels addTarget:self action:#selector(btn_Method) forControlEvents:UIControlEventTouchUpInside];
to
[btn_levels addTarget:self action:#selector(btn_Method:) forControlEvents:UIControlEventTouchUpInside];
Also change the below method -
-(void)btn_Method
{
//to make button disable
}
to
-(void)btn_Method:(UIButton*)button
{
//to make button disable
[button setEnabled:NO];
}
disabled but visible
btn_levels.enabled = NO;
also invisible
btn_levels.hidden = YES;
Target actions can send the object for which it'll performed the action. To make your method to know which button was tap, you need to make its definition to get the button inside the method. So just change you button definition to look like this,
- (void) btn_Method:(UIButton *)sender {...}
also, where you're adding target to the button you've to add a colon ( : ) after the method name to tell the compiler that, this action will need an object on which an action was added. So that line should look like this,
[btn_levels addTarget:self action:#selector(btn_Method:) forControlEvents:UIControlEventTouchUpInside];
^
Here, we will get a sender (you can give any name to your argument) of type UIButton with btn_Method action call. That's it, now you can do anything with that button, so in your case you want to make it disable, so the method would look like this,
- (void) btn_Method:(UIButton *)sender {
sender.enabled = NO;
}

UIButton UIControl target action 2 actions for single button

I have a button that has a selected and non-selected state.
My target action code for the button is this:
NSArray *targets = [myButton actionsForTarget:self forControlEvent:UIControlEventTouchUpInside];
if ([targets count] == 0) {
[myButton addTarget:self action:#selector(save:) forControlEvents:UIControlEventTouchUpInside];
When the button is pressed, it goes to selected state in the method.
If the button is selected, a different action should be called. Is there any unintended consequence to doing it this way or is there a way to add actions with UIControlState instead?
if (myButton.selected == NO){
[myButton addTarget:self action:#selector(save:) forControlEvents:UIControlEventTouchUpInside];
}
else{
[myButton addTarget:self action:#selector(delete:) forControlEvent:UIControlEventTouchUpInside];
}
-(IBAction)myButton:(id)sender
{
UIButton *btn = (UIButton*)sender;
btn.selected = !btn.selected;
if(btn.selected)
{
//Do whatever you want when button is selected
[self delete];
}
else
{
//Do whatever you want when button isDeselected
[self save];
}
}
Your approach might be confusing. You can use like this.
[myButton addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
- (void)buttonPressed:(UIButton *)sender
{
if(sender.selected)
[self save];
else
[self delete];
}
-(void)save
{
// your code
}
-(void)delete
{
// your code
}
i believe that you are wanting something like toggle button. if i am right use yourbutton.currentTitle to follow the functionality. i.e. if [youtbutton.currentTitle isEqualToString: #"save]" then you should perform save and set that to delete and vice verse

UIKeyboardTypeNamePhonePad - Display options

I need to use the, Name Phone Keyboard type (UIKeyboardTypeNamePhonePad) but having the number pad appear first when the text field is selected.
I need to input data such as
123 -
657 -
450b -
123a
I always need input a number first, but occasionally either a A / B at the end.
You can try to change keyboard type in function of the size of the string:
Implement delegate UITextFieldDelegate
Then set defaut textfield keyboard;
[textField setKeyboardType:UIKeyboardTypeNumberPad];
[textField setDelegate:self];
Then in the following method, change the keyboard when text size equals 3:
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if([[textField text] length] > 3)
{
[textField setKeyboardType:UIKeyboardTypeDefault];
[textField reloadInputViews];
}
return YES;
}
Another solution is to add an accessory view button (you can change of keyboard manually):
- (void)viewDidLoad
{
[super viewDidLoad];
textField.keyboardType = UIKeyboardTypeNumberPad;
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(0, 0, 50, 50)];
[button addTarget:self action:#selector(changeKeyboard) forControlEvents:UIControlEventTouchDown];
textField.inputAccessoryView = button;
}
-(void)changeKeyboard
{
if([textField keyboardType] == UIKeyboardTypeNumberPad)
{
textField.keyboardType = UIKeyboardAppearanceDefault;
}
else
{
textField.keyboardType = UIKeyboardTypeNumberPad;
}
[textField reloadInputViews];
}
click on the textfield,then go to Hide or show utilities ie View(you will find it at right hand side beside the editor.There you select the third view ie the most right one).
Then you click the attributes inspector.In that you will find a menu called "keyboard" which is set as default,but you can select whichever pad you want.
Hope it works for you.

How to make UIButton dragable but don't trigger click event

I usually use this method to implement the UIButton movable;
[button addTarget:self action:#selector(dragMoving:withEvent:) forControlEvents:UIControlEventTouchDragInside];
But it will trigger the touchUpInside event at same time, and I need touchUpInside event to do some other things.
So does anyone know how to avoid this?
Thanks a lot;
use these code below to solve it;
[button addTarget:self action:#selector(touchDown:withEvent:) forControlEvents:UIControlEventTouchDown];
[button addTarget:self action:#selector(touchDragInside:withEvent:) forControlEvents:UIControlEventTouchDragInside];
[button addTarget:self action:#selector(touchUpInside:withEvent:) forControlEvents:UIControlEventTouchUpInside];
- (void) touchUpInside :(UIControl*)c withEvent:ev{
NSLog(#"touch Up inside");
if (count) {
NSLog(#"here I can do something about clicking event");
}
}
- (void) touchDown:(UIControl*)c withEvent:ev{
NSLog(#"touch Down");
count = YES;
}
-(void) touchDragInside:(UIControl*)c withEvent:ev{
NSLog(#"touch drag inside");
c.center = [[[ev allTouches] anyObject] locationInView:self.view];
count = NO;
}
Actually the best way to accomplish this is to use UIPanGestureRecognizer and add it to the button. It handles the dragging very well and you also can tap on it without needing to do anything else.

How to find which button is pressed on the array of buttons in ios

I have an array of buttons,I want the text on each button..to be displayed on the label..so friends,please tell me how can I achieve this
Regards
Ranjit
Simply add button in this way
Button.tag = i
[Button addTarget:self action:#selector(MenuDetail:) forControlEvents:UIControlEventTouchUpInside];
[listOfButtons addObject:Button];
and handle touch up event as following
-(IBAction)MenuDetail:(id)sender
{
UIButton *temp= (UIButton *)sender;
NSInteger parentValue=temp.tag;
clsMenuItem *tempMenu=[appDelegate searchMenu:parentValue];
if(parentValue==1)
{
// do something
}
else if(parentValue==1)
{
// do something
}
}
hope this helps

Resources