I am having trouble with a new view I have created, I have a registration view that has a single UITextField on it and a UIButton.
I call this view from another view like so
//otherview.m
- (void)viewDidLoad
{
[super viewDidLoad];
RegistrationAlertViewController *regreg = [[RegistrationAlertViewController alloc] init];
[self.view addSubview:regreg.view];
}
Then I create my regregview like this
//regregView.h
#import <UIKit/UIKit.h>
@interface RegistrationAlertViewController : UIViewController <UITextFieldDelegate> {
// textfields for registration
IBOutlet UITextField *registrationTextFieldA;
}
// textfields for registration
@property (strong, nonatomic) IBOutlet UITextField *registrationTextFieldA;
@end
//regregView.m
#import "RegistrationAlertViewController.h"
@interface RegistrationAlertViewController ()
@end
@implementation RegistrationAlertViewController
@synthesize registrationTextFieldA;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
registrationTextFieldA = [[UITextField alloc] init];
registrationTextFieldA.delegate = self;
[registrationTextFieldA becomeFirstResponder];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if([textField.text length] > 4)
{
//Get next TextField... A simple way to do this:
// UITextField *newTextField = [textField.superview viewWithTag:(textField.tag+1)];
// [newTextField becomeFirstResponder];
return NO;
//remember to set the tags in order
}
return YES; //you probably want to review this...
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if((textField.text.length + string.length) > 4)
{
//Get next TextField... A simple way to do this:
// UITextField *newTextField = [textField.superview viewWithTag:(textField.tag+1)];
// [newTextField becomeFirstResponder];
//remember to set the tags in order
}
return YES; //you probably want to review this...
}
@end
I have the two delegates in my regregView.m
- textFieldShouldBeginEditing
- shouldChangeCharactersInRange
for some bizarre reason textFieldShouldBeginEditing is entered when the view first loads but then when I start entering characters into registrationTextFieldA shouldChangeCharactersInRange is never being entered for some bizarre reason.
any help figuring out why my delegates are not working properly would be greatly appreciated.
As you are initializing the view while implementing this file.So set delegate in init method not in viewDidLoad.
Include UITextFielDelegate category in yourClass.h file and Try this code .
I hope it helps you.
A wild guess here. The problem is in your otherview.m
Try making a strong property of RegistrationAlertViewController on top of your otherview.m
Then in your view did load, you can do
Hope that works.. Just seems like a similar problem I ran into before.. Good luck
Don't allocate
UITextField
instance inviewDidLoad
method. Replace you code with this :Hope it helps you.
Add UItextField delegate from your xib outlet and declare delegate protocol
<uitextfielddelegate>
in your .h file.Definitely it will work fine for You.
Good Luck !!!!