UISteppper connect with textField - ios

How do I connect UIstepper with textField? When I connect it increment and decrement but, when I directly enter number to textfield after that try to increment by using stepper the value will start from the stepper minimum value. I want to continue from the textField value.

There is no binding in iOS development like you want. Basically you have to decide where you want to store your values. You could store your values in the stepper, in the textfield, or in some other variable entirely.
Secondly, but no less importantly, your stepper target function is all wrong. Your problem is that you are re-setting up your stepper each time the stepperPressed1: method is fired. Which I hope is connected to your stepper's valueChanged event, and I'll assume it is.
Let's for the moment assume that you are trying to store your numerical stepped value in the stepper.
You should be setting up your stepper only once, likely in your viewDidLoad method. And then reading the new value when the value changes and writing that to your text field.
For Example:
-(void)viewDidLoad{
[super viewDidLoad];
[self.stepper setMinimumValue:0];
[self.stepper setContinuous:YES];
[self.stepper setWraps:NO];
[self.stepper setStepValue:1];
[self.stepper setMaximumValue:300];
self.txtField.text=[NSString stringWithFormat:#"%g",self.stepper.value];
}
-(IBAction)stepperPressed1:(id)sender{
self.txtField.text=[NSString stringWithFormat:#"%g",self.stepper.value];
}
Now as far as setting the value in the stepper based on what is typed in the textfield. What you need to do is when the user is done typing in a value you want to get that value and assign it to the stepper. You would connect a function to the textField's "Did End On Exit" event. That function would look something like this:
-(IBAction)didEndOnExit:(id)sender {
self.stepper.value = self.txtField.text.doubleValue;
self.txtField.text=[NSString stringWithFormat:#"%g",self.stepper.value];
}

Related

Value of textfield before "Editing Changed" event is fired

I want to know if there is a way to know the value of textfield before the value is changed to the new value.
For example:
If the text field had a value "a" and the user entered "b", so the editing changed event is fired. When I try to fetch the value of the text field in the debugger it will display "ab". Is there a way that I can get "a" before "b" was written?
Here is a piece of code where I am trying to fetch the value:
#IBAction func commentEditingChanged(sender: AnyObject) {
if(!((sender as? UITextField)!.text!.isEmpty)){
counter++
}
else
{
counter--
}
}
In this case I want to know if the text was empty before the user entered text because I don't want the counter increments every time the user enters a character. I want to increment once the user entered any text.
And please don't propose to use delegate function textField:shouldChangeCharactersInRange:replacementString because my question is about this IBAction method.
If there is no way to do that using this IBAction Method, is there is any other IBAction method that can achieve what i want?

enablesReturnKeyAutomatically is not working with numeric keypad

I am implementing a message composer as like iMessages.
enablesReturnKeyAutomatically of UITextView is not working.
I have set it as textView.enablesReturnKeyAutomatically = YES;
but when I tap on numeric keypad and start typing something and sent using my send button. the textView got empty but the send button (return button of UITextView) is still enabled.
enablesReturnKeyAutomatically seems to only affect the uikeyboard, which in this case is a numeric keypad. The default numeric keypad does not have a return key, so there is nothing to enable/disable.
It sounds like you have a separate send button in the view. A workaround is inside - textView:shouldChangeTextInRange:replacementText: calculate what the new string will be with the replacementText and test if this new String is equal to #"". If so, then sendButton.enabled = NO, else sendButton.enabled = YES.
Remember to set the UITextViewDelegate and to trim the new string of white space.

UITextField input every time new iteration

I'm new to Objective-C and Xcode, and I have one problem, I tried looking in the Internet, but didn't find anything.
I have "game" and there I have a screen with two text fields, button and text view.User inputs integer values and taps the button. After tapping the button some functions(or, methods) are called. Method that is called after typing the button:
- (IBAction)okButtonTapped:(id)sender {
int save1=100;
int save2=100;
int save13=1;
int save22=1;
int save12=1;
int save23=1;
while(save13 !=0 && save23 !=0){
[self yourturn:&save1 heap2:&save2];
save12= save1;
save22=save2;
[self compturn:&save12 heap2:&save22];
save13=save12;
save23=save22;
save1=save13;
save2=save23;
}
}
Here some methods are called. And in this methods I use this code to save values from textfields that user inputs:
_heapNumberInt=[self.heapNumberTextField.text intValue];
_thingsNumberInt=[self.thingsNumberTextField.text intValue];
And the question is how to make users input new values in the textfield every time when there is new iteration in while loop?
Sorry for my English(it's not my native language) and for my long question.
All project you can download here
P.S. "game" is very simple: there are 2 heaps with 100 things each, user chooses his turn and then he inputs values: heap number(1 or 2 heap) and number of things he wants to take from the heap. Then there is computer's turn. And looser is someone who can't take things from any heap. It is real game called NIM.(but here I made only 2 heaps(not 3)).
Probably you need to read Event Handling Guide
https://developer.apple.com/library/ios/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/Introduction/Introduction.html#//apple_ref/doc/uid/TP40009541
About the question, for setting focus to the input you should use
[self.heapNumberTextField becomeFirstResponder];
UPDATE:
As I understand you need to:
_heapNumberInt = [self.heapNumberTextField.text intValue]; // get value
[self.heapNumberTextField setEditable:NO]; // disable editing to calculate
[self calculate];
self.heapNumberTextField.text = #""; // clean UITextField
[self.heapNumberTextField setEditable:YES]; // allow editing
[self.heapNumberTextField becomeFirstResponder]; // set focus to the textfield

Check if one method has been run in a different method

I'm writing some code in Xcode for an iPhone app and I want to be able to detect if a method has been run (i.e. a button was pressed, causing that method to run) in another method (I want to use an if statement so that if the button was pressed it will do this, but if it wasn't then it will do something else).
There is no has_method_been_run() function but you can check to see if the state was changed.
For example say method button_clicked() calls method change_font_to_blue(). In this case you can check to see if the font is blue and there for the method was called.
That's a very basic example of course but you could check any number of variables / the state of the UI to see if it was changed.
OR you can add a boolean to an object and just set it to true when you execute you're method that you're watching.
If your example is simply a button press I would change the UIButton's selected state
- (IBAction)buttonSelected:(UIButton *)sender {
sender.selected = YES;
// OR:
// self.myButton.selected = YES;
}
- (void)otherMethod {
if (self.myButton.isSelected) {
// button has been run through the selector method
// if you want, reset button's selected state
self.myButton.selected = NO;
} else {
// button has NOT been run through the selector method
}
}
This is a simple idea and by default the selected state of a UIButton is not visually different. If you want it to be visually changed then you can simply go into IB and change the visuals there for the selected state (including the title):
Then when the button is set to selected (self.myButton.selected = YES;) it will automatically change what the button looks like!

dynamically change UIKeyboards return key

I have two UITextfield the user enters his name into the first and email into the second. I would like to know how to change the UIKeyboards return key depending if the name text field has an entry or not.
For instance if nametextfield is empty then I would like the UIkeyboard return key to be Next
else if the nametextfield has an entry in it then when the user selects the email text field I would like the return key to be submit.
Is this possible? if so how would I go about accomplishing it? any help would be appreciated.
You can have return key customized to prefixed values that you can see in UIReturnKeyType enum for each UITextField.
textFieldName.returnKeyType = UIReturnKeyNext;
textFieldEmail.returnKeyType = UIReturnKeyDefault;
Not sure if this is what you're looking for though.
You have a chance to set up keyboard characteristics in the UITextFieldDelegate Protocol method textFieldShouldBeginEditing: which is called before the text field becomes the first responder (indeed to decide if it may become the first responder). If you don't already have a delegate for the text field(s) in question you would have to assign one and implement at least that method. Presumably the same object handling the text field could hold the delegate methods. The following implementation sets the return key to "Search".
- (BOOL) textFieldShouldBeginEditing:(UITextField *)textField {
NSLog(#"textFieldShouldBeginEditing");
textField.returnKeyType = UIReturnKeySearch;
return YES;
}
You'd have to look at the contents of your text fields to decide what value to use.
Make use of the textField.returnKeyType property.
you can check out all the available options here http://developer.apple.com/library/ios/documentation/uikit/reference/UITextInputTraits_Protocol/Reference/UITextInputTraits.html#//apple_ref/doc/c_ref/UIReturnKeyType
textfield.returnKeyType = UIReturnKeySearch;

Resources