Tom Andrews Tom Andrews - 20 days ago 7
Objective-C Question

Setting variables from textbox values in objective-c

Sort of new to objective-c, just wondering how I can set a variable as a value from a textbox. I'm setting up my login screen and will be using a JSON request to process the variables.

@interface LoginController : UIViewController
{
IBOutlet UITextField *txtUsername;
IBOutlet UITextField *txtPassword;
IBOutlet UIButton *btnLogin;
}

-(IBAction)btnLogin:(id)sender;


@end


Everything is defined in the above .h

@implementation LoginController

-(IBAction)btnLogin:(id)sender
{
username = [txtUsername];
}


Just need to know the syntax to set that username variable to the text in the textbox when btnLogin is clicked. (.m file above)

Answer

You get the value of the text in the text field like so :

-(IBAction)btnLogin:(id)sender
{
    NSString * username = [txtUsername text];
}

also please to the documentation of the UITextField