karl
karl

Reputation: 91

How to get or set value to specify UITextField?

Halo, I am a beginner in programming, and I start to learn Objective c now !

for (int i=0; i<9; i++){
        y += 50;
        x = 160;

        for (int j=0; j<9; j++){
            grid = [[UITextField alloc] initWithFrame:CGRectMake(x, y, 50 , 50)];
            grid.layer.borderWidth = 1;
            grid.textAlignment = NSTextAlignmentCenter;
            [[self view] addSubview:grid];

            x += 50;

            grid.text =  [@(j) stringValue];

            [initialArray addObject:grid];

        }

    }

I tried to using these code to for-loop many UITextField.

and each grid has a number, and I put the grid into a NSMutableArray

but how can I set a value to specify field? is it has a id for me to do this? and how to get the value from field when the button is clicked? thank you very much !!!

Upvotes: 0

Views: 2154

Answers (2)

Dishant Rajput
Dishant Rajput

Reputation: 1357

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {


if (textField==yourTTextField) {
NSString *strText = [textField.text stringByReplacingCharactersInRange:range withString:string];

if ([strText isEqualToString:@"$"] == TRUE) {

    textField.text = @"";
    return NO;
}
if (strText.length ==1) {

    textField.text = [NSString stringWithFormat:@"$ %@", strText];
    return NO;
}
}
return YES;
}

Upvotes: 1

Saranjith
Saranjith

Reputation: 11547

Setting a string

 grid.text=@""some value";
 [initialarray objectAtIndex:i].text=@"some value";

Getting value

 NSString *str=grid.text
 NSString *str=[initialarray objectAtIndex:i].text; 

You can set tag by simply giving

grid.tag=i; 
[initialarray objectAtIndex:i].tag=i;

i can be a loop variable.

Upvotes: 3

Related Questions