Joe
Joe

Reputation: 45

UITextField isn't working

am new to objective-c programming, so sorry if it's a silly question but i looked thoroughly and couldn't find the error, p.s: english isn't my first language so programming keywords might be unintentionally used

I've set up a txtfield and defined some of it's properties, such as the keyboard-number pad and stuff, the problem is, when i run it on the simulator, nothing happens, the defult keyboard is called.

i'm just trying to do simple stuff to get it in my head, in my .h, i have

@interface practiceViewController : UIViewController <UITextFieldDelegate>
{
    UITextField *userInput;
}
@property (nonatomic, retain, readwrite) UITextField *userInput;
@end

and in my .m

-(UITextField *)userInput{
if (userInput == nil) {

userInput=[[UITextField alloc]init];
userInput.keyboardType= UIKeyboardTypeNumberPad;
userInput.returnKeyType= UIReturnKeyDone;
userInput.textColor=[ UIColor redColor];
userInput.clearButtonMode= UITextFieldViewModeWhileEditing;
userInput.delegate = self;
}

return userInput;

}

i also tried to make it as an input, but for some reson it's not working

int nInput=[[userInput text] intValue];

whats pissing me off about this is that i did the same in a previous project and the "input worked" only, but now everything isn't. and my property as i haven't defined anything. i appreciate your help

Upvotes: 0

Views: 374

Answers (4)

Prasad G
Prasad G

Reputation: 6718

userInput= [[UITextField alloc] initWithFrame:CGRectMake(10, 50, 200, 30)]
userInput.keyboardType= UIKeyboardTypeNumberPad;
userInput.returnKeyType= UIReturnKeyDone;
userInput.textColor=[ UIColor redColor];
userInput.clearButtonMode= UITextFieldViewModeWhileEditing;
userInput.delegate = self;

You can just set the frame and add below property(to visible the textfield frame in View otherwise it is not visible in View).

  userInput.borderStyle = UITextBorderStyleRoundedRect;
 [self.view addSubview:userInput];

I hope it will be helpful to you.

Upvotes: 0

JiLLu_
JiLLu_

Reputation: 13

I Think you have to set the frame for UITextField like this ...

userInput.frame=CGRectMake(100,100,60,31);

in your -(UITextField *)userInput method, that's y it didn't show ..

Upvotes: 1

Peter DeWeese
Peter DeWeese

Reputation: 18333

Although this is tagged iPhone, do you happen to be simulating an iPad? If so, I have run into this. The iPad will not show a number pad keyboard.

Upvotes: 0

Deepak Danduprolu
Deepak Danduprolu

Reputation: 44633

Well few things are hazy but ...

You are getting the value of the text field using,

int nInput = [[userInput text] intValue];

If you are not getting the correct value then nInput will be zero. That is because userInput must be nil. You can verify this by logging it by adding NSLog(@"%@", userInput); before the line above.

Whatever value is printed, it looks like your on screen text field is not referenced to by userInput.

Since you're doing the entire thing programmatically, you will have to add your userInput to the view hierarchy by doing this,

[self.view addSubview:self.userInput];

Using the property accessor is important here is important as otherwise userInput would be nil unless you've accessed it using self.userInput earlier.

Upvotes: 0

Related Questions