UITextField viewWithTag Usage?

I have a scroll view in which I have placed multiple UITextFields. When the text field is changed, it is sent to a selector and the function is carried out. Where I am having trouble is the "Tag".


So I can read the [sender tag], but I cannot read the text from within the UITextField at that tag.


    NSString *holdString = ((UITextField*)[textChipCount viewWithTag:[sender tag]]).text; // says (null)
// Also tried (and many other random attempts)
    NSString *holdString = ((UITextField*)[self.view viewWithTag:[sender tag]]).text;// Crashes


So I get that the text box selected tag = 3 (or which ever one is selected), but now I would like to convert the content from within the text field into a string. Can anyone help me?

Answered by PBK in 89781022

You can simply do
NSString *theText=sender.text;


That will be the active textField in any call to a delegate method for the textField.

What is "sender"? What is "textChipCount"? What does viewWithTag return in each case? (is it a UITextField?)

The sender is a UITextField -> textChipCount. (I've placed 10 UITextField on a scrollview)

[sender tag] = correct number

viewWithTag = (null) when I use textChipCount

viewWithTag = "Crashes, No Instance variable" when I use self.view

It is more useful to post actual code declarations and copied and pasted error messages, not your summary of how you think it is 🙂 Such problems are often rooted in incorrect assumptions about what error messages mean etc.


I would split those big long lines into multiple statements so you can more clearly see what's happening, and step through it in the debugger, checking each step individually.


NSInteger tag = [sender tag]; // Is this the value you expect?
UIView *view = [self.view viewWithTag:tag]; // Is this view non-null and an instance of UITextField?
NSString *text = ((UITextField *)view).text; // Is this non-null?


But your use of "sender" makes me wonder. That sounds like a parameter to an IBAction, perhaps coming from a button. If that button's tag property is 3, and the text field you are looking for also has a tag of 3, then perhaps viewWithTag is returning the button. But all this is speculation since you haven't posted enough code. There are ways around that if that's the issue.

Accepted Answer

You can simply do
NSString *theText=sender.text;


That will be the active textField in any call to a delegate method for the textField.

Well here is the code I have for it.


- (void) calculations {
    for (int x = 0; x < 15; x++) {
        UIFont *fontChips = [UIFont fontWithName:@"Helvetica" size:15];
        textChipCount = [[UITextField alloc] initWithFrame:CGRectMake(curXLoc, curYLoc, 50, 30)];
        textChipCount.font = fontChips;
        textChipCount.textColor = [UIColor whiteColor];
        textChipCount.backgroundColor = [UIColor darkGrayColor];
        textChipCount.textAlignment = NSTextAlignmentCenter;
        [textChipCount setAdjustsFontSizeToFitWidth:YES];
        textChipCount.text = [NSString stringWithFormat:@"%i",aChipsEntry [x]];
        textChipCount.enabled = YES;
        textChipCount.clearsOnBeginEditing = YES;
        [textChipCount setTag:tag]; // Tagged
        [textChipCount setKeyboardAppearance:UIKeyboardAppearanceDark];
        [textChipCount setKeyboardType:UIKeyboardTypeNumberPad];
        [textChipCount addTarget:self action:@selector(actCloseKeyboard:) forControlEvents:UIControlEventTouchUpOutside]; // sender tag works       
        [scrollChipScheme addSubview:textChipCount];
     }





// These are going to be placed into a function, but I'm testing them here first to get them working


    ((UITextField *) [self.view viewWithTag:0]).text = @"Test";
          // App Crashes here with this -[UIView setText:]: unrecognized selector sent to instance 0x125e98490'
          // *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView setText:]: unrecognized selector sent to instance 0x125e98490'     
          // *** First throw call stack: etc....
     NSLog(@"%@",((UITextField *) [self.view viewWithTag:0]).text);


    ((UITextField *) [textChipCount viewWithTag:0]).text = @"Test";
    NSLog(@"%@",((UITextField *) [textChipCount viewWithTag:0]).text);
          // (null) is logged.

}

I ended up doing it this way, but yes THANK YOU!


NSString *holdString = ((UITextField*)sender).text;

Thank you everyone for helping. It is nice to see people willing to give input for those of us who get stuck on even the smallest of things.

UITextField viewWithTag Usage?
 
 
Q