Reputation: 11
I have programmatically created 2 UITextFeilds and 1 UIButton for an IOS app view, every time the view is called. The UIButton calls function(method) login how could i create a way to distinguish between the two UITextViews? I am a JS programmer some coming from the same thought pattern is there a way to get the object by an id? or get all UITextFeilds split them like:
-(void)login{
Array *UITF[UITFself.view.UITextFeilds];
UITF[0].text;
}
Thanks in advance
UITextField *uname = [[UITextField alloc] initWithFrame:CGRectMake(80.0, 100, 150.0, 30.0)];
[uname addTarget:self
action:@selector(textFieldDone:)
forControlEvents:UIControlEventEditingDidEndOnExit];
uname.borderStyle = UITextBorderStyleRoundedRect;
uname.keyboardType = UITextAutocapitalizationTypeNone;
uname.returnKeyType = UIReturnKeyDone;
uname.autocorrectionType = UITextAutocorrectionTypeNo;
[self.view addSubview:uname];
-(void)login {
//send login message
NSString *u=uname.text;//user name text
NSString *p=pass.text;//password text
NSLog(u);
NSLog(p);
}
Upvotes: 0
Views: 184
Reputation: 3522
UIView objects have a NSInteger tag property that can be useful in the case of dynamically created views. Set the value at creation time and use viewWithTag: from any of its parent views.
Upvotes: 0
Reputation: 32681
Store the variables you use to create your UITextFields as instance variables -- or better, as @property
-- in the header, and not local variables. Thus you can access them from anywhere in your class.
.h:
@interface YourClass : UIViewController {
}
@property(nonatomic, retain) IBOutlet UITextField* nameField;
@property(nonatomic, retain) IBOutlet UITextField* passField;
@end
.m:
@implementation YourClass
@synthesize nameField, passField;
-(void)dealloc {
self.nameField = nil; // release memory when your class is deallocated
self.passField = nil; // release memory when your class is deallocated
[super dealloc];
}
-(void)loadView {
// here create your UITextFields programatically...
self.nameField = [[[UITextField alloc] initWitHFrame:...] autorelease];
...
self.passField = [[[UITextField alloc] initWitHFrame:...] autorelease];
...
// or much more easier, you should create them using InterfaceBuilder,
// this would save you a lot of code
}
-(IBAction)login {
NSLog(@"name: %@ ; pass: %@", nameField.text, passField.text);
}
@end
FYI, note that if you used InterfaceBuilder to build your interface, you wouldn't have the problem as you would already have the IBOutlets connected to your UITextFields (and you would have saved all the code needed to create and configure your UITextFields by code).
Side-note: don't use NSLog(stringVariable);
but rather NSLog(@"%@",stringVariable);
because if your stringVariable
contains a litteral '%' followed by any specifier character (for example, in your case, if the text entered in your nameField, and typed by the user, contain "user%xyz"), your code will crash. So always use a statically entered litteral string as the first parameter of NSLog, not a variable.
Upvotes: 1
Reputation: 48398
The easiest thing to do is to declare the UITextField
s in the header:
UITextField *nameTextField;
UITextField *passTextField;
Then use these references in the implementation.
Upvotes: 1