0

作成した新しいビューに問題があります。単一の UITextField と UIButton を持つ登録ビューがあります。

このビューを別のビューから呼び出す

//otherview.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    RegistrationAlertViewController *regreg = [[RegistrationAlertViewController alloc] init];
    [self.view addSubview:regreg.view];

}

次に、このようにregregviewを作成します

//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

regregView.m に 2 つのデリゲートがあります。

  • textFieldShouldBeginEditing
  • shouldChangeCharactersInRange

奇妙な理由で、ビューが最初にロードされたときにtextFieldShouldBeginEditingが入力されますが、その後、 registrationTextFieldA shouldChangeCharactersInRangeに文字を入力し始めると、何らかの奇妙な理由で入力されることはありません。

私のデリゲートが適切に機能していない理由を理解する助けがあれば、大歓迎です。

4

5 に答える 5

0

xib アウトレットから UItextField デリゲートを追加<uitextfielddelegate>し、.h ファイルでデリゲート プロトコルを宣言します。

間違いなくそれはあなたにとってうまくいくでしょう.

幸運を !!!!

于 2013-07-09T08:43:59.030 に答える