Strange issues with tag label 0? - iphone

I have created a bar over the keyboard for textfields with previous/next/done button selections. In doing so, I noticed an odd occurance with my tags that I used to navigate between the textfields. I am creating my interface programmatically with a loop, and as such, just set the tag values to the loop variable i.
I started the i variable at 0 so the very first text field created had a tag of zero. Basically what was happening is the 'previous' button functionality would only go so low as 1. It wouldn't even go back to the text field with the 0 tag. The only way to fix this was to increase all tag values by 1 so the first text field started at 1 instead of zero.
Here is my code. Is there a bug in my code that I cannot see? or is this a weird issue with tags?
-(void)gotoPrevTextfield{
// If the active textfield is the first one, can't go to any previous
// field so just return.
UITextField *textField = (UITextField *)[inputsView viewWithTag:0];
NSLog(#"%i",textField.tag);
NSLog(#"%i",txtActiveField.tag);
if (txtActiveField == textField) {
NSLog(#"returning at previous");
return;
}
else {
NSLog(#"set responder");
// Otherwise if a different textfield has the focus, the operation
// of "previous" button can be done and set the previous as the first
// responder.
textField = (UITextField *)[inputsView viewWithTag:txtActiveField.tag - 1];
NSLog(#"%i",textField.tag);
NSLog(#"%i",txtActiveField.tag);
[textField becomeFirstResponder];
}
}

Note that unset tags default to 0 so that is almost a poor choice. You may be getting another view that you don't expect.
A fairly good practice is to add some constant such as 100, consider making the constant a const int or #define for clarity.

Related

Automatic typing password iphone

In my UIViewController I have 2 textfields. One called passwordtextfield and the other retype. Instead of having people actually retype their passwords I want it to automatically fill it in. Is this possible? I tried something that keeps on crashing.
[_passwordtextfield addTarget:self action:#selector(updateTextField:) forControlEvents:UIControlEventEditingChanged];
- (void)updateTextField:(id)sender{
UITextField *retype = ((UITextField *)_passwordtextfield).text;
}
There is no need to cast your text field. You can assign direct value of your text field.So
Write this line
retypetextfield.text = _passwordtextfield.text;
instead of
UITextField *retype = ((UITextField *)_passwordtextfield).text;
I don't know why you want to do this, as mentioned in the comments, but to answer the question... It looks like you are creating a new UITextField in updateTextField instead of setting the text of one that exists. It should look something like this:
- (void)updateTextField:(id)sender{
retypeFied.Text = _passwordtextfield.text;
}
Substitute whatever you named the pointer to your second field for retypeField.

Get variable of Keyboard type

I have been trying to get the variable for the keyboardType
self.titleField.keyboardType
When i use nslog it returns about 10 values that are either 0 or 4
I have implemented custom behavior for the number pad keyboard so i need to check if it's the number pad or default keyboard that is showing up.
I have also tried to use self.view.tag to set it by view instead of keyboard.
The nslog for that returns all the tags of the previous view controllers that the application
has been through.
I think this may be because i'm reusing a single view controller class for each separate
controller. I am using a switch statements to determine what
data to display based on the tag.
Is their a way to reset the tag in viewWillDisappear?
Try assigning tag value to textfields and check which textfield became first responder in TF's delegate method -textFieldDidBeginEditing:
The UIKeyboardType enumeration gives names to the keyboard type numbers:
switch (self.titleField.keyboardType) {
case UIKeyboardTypeDefault:
NSLog(#"default keyboard");
break;
case UIKeyboardTypeNumberPad:
NSLog(#"number pad keyboard");
break;
default:
NSLog(#"unexpected keyboard type %d", self.titleField.keyboardType);
break;
}

Get the number value of a texfield Xcode

I want to have a textfield where the user can input a number and then set a label to that value. Is there any "texfield.value" or something that can fetch the value in numbers?
This is the only piece i heve done. should i put it here?:
- (IBAction)set:(id)sender;
{
}
Thanks in advance
In Xcode, select the textField or textView and change the keyboard type to Numeric.
Then you can get the value of whatever the user inputs into that field by doing:
int value = [[textField.text] intValue];
You can grab the integer value of a textfield using the following snippet.
[textField.text intValue]
Hope this helps.
You can get by textfield.text this will return the text value of that text field , now to convert it to number
NSNumber * mynumber = [NSNumber numberWithInt:[[textfield. text] intValue]];
nslog(#"%#",mynumber);
Here's an example of something I just used:
[self.phoneNumberTextField.text intValue];
You have to be careful with this one. Here are the steps to get this to work
In storyboard, select the textfield
Open the attributes tab for the textfield in Interface Builder
Change keyboard type to "Number Pad" (This guarantees that the user will only be able to enter ints into the textfield)
Now, in your code, type [self.myTextField.text intValue]; (set this equal to a local variable)
Now, as far as WHERE in the code you'll put this, it depends. If you're trying to log in a user, I'd recommend putting this in an IBAction like 'loginButtonPressed'. Or before you transition to a new screen in 'viewWillDisappear' or even 'prepareForSegue'.
Good luck!
This is from 3 years ago, I tried using that and didn't work and so I find this working for me
int i = textfield.text.intValue;

Xcode update label text from other class

Few days back this question was about changing the label text from an other class. I changed a few things around:
I now have a function setLabelText:
- (void)setLabelText:(NSString*)text{
//myLabel.text = text;
[myLabel performSelectorOnMainThread : # selector(setText : ) withObject:text waitUntilDone:YES];
NSLog(#"class:%#, label:%#", self, myLabel);
}
Both self and label are filled, but the display is not showing the correct text..
I'm calling this function from an other class, if I change my label text from a button on the viewcontroller it changes correctly.
EDIT:
I have the label changing now, every time when the class is done with it's functions the label edits the text. Although.. this is always the last set value, I need it to change every time it gets into a for loop, how can I get this to work? Seems it doesn't update the viewController while the app is doing it's for loop

How can I get the next IBAction to affect the next empty IBOutet UILabel?

I have 12 Buttons with 0-11 tagged. When the user clicks the button the tag is inserted into the first of 12 label outlets. I want the next clicked button to automatically insert its tag into the next available outlet. As of now the tag simply adds itself to the first outlet. I have tried a variety of fixes, but to no avail.
This is my implementation thus far:
-(IBAction)enterNumber:(id)sender {
cell1 = cell1+(int)[sender tag];
label1.text = [NSString stringWithFormat:#"%i",cell1];
}
My problem is getting the next IBAction to display only in cell2 (I assume this help will lead me to use the fix for cell3-cell12. None of my books' suggestions provide suitable processes to my limited knowledge.
Edited after Danillo
I hadn't considered the IBOutletCollection, I hadn't heard of it! I will certainly try to use it. For the others who wanted a clearer question to my problem I will attempt represent part of the screen that the user will interact with. The top numbers are fixed buttons. The bottom squares are where the clicked top buttons' tags are logged sequentially to the series. I just clicked (4). I would like the next click of any top button to be logged into the second bottom square, but what is happening is that the integer is being added to the integer in the first box.
(1) - (2) - (3) - (4) - (5) - (6)
[ 4 ] [ _ ] [ _ ] [ _ ] [ _ ] [ _ ] [ _ ] [ _ ] [ _ ] [ _ ] [ _ ]
It sounds as though what you are trying to do is provide content sequentially to a series of labels. If that is correct, this is the approach I would take:
iOS 4 and later supports outlet collections, which are exposed for your use as arrays filled with whatever objects you've assigned.
So, just as you've tagged your buttons, tag your labels. Then, in your .h file, declare an Outlet Collection like so:
#property (nonatomic, retain) IBOutletCollection(UILabel) NSArray *labels;
Control drag each label to wire it to that outlet collection.
Then, when it's time to assign content to a label, iterate through the collection:
for (UILabel *label in self.labels)
{
if (label.tag == whateverIntYouCareAbout)
{
//Do stuff
break;
}
}
From what you said "but what is happening is that the integer is being added to the integer in the first box." I believe your problem you are able to display the number in the first label only. This is because in your method you are setting the text for the label as label1.text = someValue; using cell1 So it is the label1 which is always updated. You should get the label for the corresponding tag of the button. For example if your tag is 3 then you should change the text of label3,if its 7 then change the text of label7. Also make sure if your cell needs to be changed accordingly. This is what I understood from what you mentioned. Correct me if I am wrong.