-1

i have input only number from text field and i want to change it to integer. what code should i write in?

just say text field say for 10 and i want to take that 10 = x, x = integer. so if i use it for mathematic process a = x + 1 and display say 11. is there any chance? i hope you understand what i mean. thank you. i think both of you will answer this simple question but i not because im beginner. im a 3d design graphic switch 180 degree become xcode programming. BTW ive tried this before but it cant.

 NSInteger number = [[txtBtn text] integerValue];

Edited :

i've got this error, local declaration of 'number' hides instance variable, pointed to number at result = (number * 2) - 1; and i put the code like this.

NSInteger number = [[txtBtn text] intValue];
result = (number * 2) - 1; 

i dont know whats wrong.

Alan Haggai Alavi
  • 72,802
  • 19
  • 102
  • 127
Piyo
  • 181
  • 1
  • 2
  • 9

1 Answers1

0

Are you sure it's a TextField and not a UIButton you are trying to get the integerValue from?

[txtBtn text]

I created a TextField and a UIButton, and implemented your code:

- (IBAction)btnClicked:(id)sender {
    NSInteger i = [[inputField text] intValue];
    int result = (i*2)-1;
    NSLog(@"%i", result);
}

the output results is correct. ex: type 5, output in console is 9.

Cake
  • 109
  • 1
  • 9
  • no, because my IBAction using UITextField as first responder. so thats why i want to take my input by UITextField to convert to be integer for next step. but anyway, your code is right, same as mine. thank you for your help. – Piyo Jun 28 '12 at 16:48