正在编辑不正确的UITextfield

时间:2012-07-29 00:22:56

标签: objective-c ios xcode keyboard uitextfield

我刚刚完成了制作自己键盘的教程。所以我掌握了这些知识并制作了一个真正简单的应用程序,用我自己的键盘添加两个数字。这只是两个数字。但是当我点击一个文本字段时,另一个文本字段会被编辑!我附上了我的代码,任何帮助将不胜感激。感谢@spacious以前的所有帮助。

#import "KeyPad2ViewController.h"

@interface KeyPad2ViewController ()
@property (nonatomic) BOOL userIsEnteringANumber;
@end

@implementation KeyPad2ViewController
@synthesize numberOne;
@synthesize numberTwo;
@synthesize result;
@synthesize keyPad;
@synthesize userIsEnteringANumber;
@synthesize currentTextField;


- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if ([numberOne isFirstResponder]) 
{
    self.currentTextField = self.numberOne;
    self.userIsEnteringANumber = NO;
}
else if ([numberTwo isFirstResponder])
{
    self.currentTextField = self.numberTwo;
    self.userIsEnteringANumber = NO;
}
return YES;

}    

- (void)viewDidLoad
{
[super viewDidLoad];
[numberOne setInputView:keyPad];
[numberTwo setInputView:keyPad];
numberOne.delegate = self;
numberTwo.delegate = self;
// Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
[self setNumberOne:nil];
[self setNumberTwo:nil];
[self setResult:nil];
[self setKeyPad:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
    return YES;
}
}

- (IBAction)buttonPressed:(UIButton *)sender 
{
NSString *button = [sender currentTitle];
NSRange decimalpoint = [self.currentTextField.text rangeOfString:@"."];
if (self.userIsEnteringANumber)
{
    if ([button isEqualToString:@"."] && decimalpoint.location != NSNotFound)
    {
        self.currentTextField.text = self.currentTextField.text;
    }else 
        self.currentTextField.text = [self.currentTextField.text stringByAppendingString:button];
}else 
{
    self.currentTextField.text = button;
    self.userIsEnteringANumber = YES;
}
}

- (IBAction)backspacePressed:(UIButton *)sender 
{
if (self.userIsEnteringANumber)
{
    int positionOfDigitBeforeLastOne = self.currentTextField.text.length -1;
    if (self.currentTextField.text.length > 0)
    {
        self.currentTextField.text = [self.currentTextField.text substringToIndex:positionOfDigitBeforeLastOne];
    }
    if (self.currentTextField.text.length == 0)
        self.currentTextField.text = @"";
}
}

- (IBAction)clearPressed:(id)sender 
{
currentTextField.text = [NSString stringWithFormat:@""];
}

- (IBAction)enterPressed:(UIButton *)sender 
{
[self hideKeyPad];
}

- (IBAction)signPressed:(UIButton *)sender 
{
NSString *negativeNumber = @"-";
NSString *firstCharInDisplay = @"";
if (self.userIsEnteringANumber)
{
    firstCharInDisplay = [self.currentTextField.text substringToIndex:1];
    if ([firstCharInDisplay isEqualToString:@"-"])
    {
        self.currentTextField.text = [self.currentTextField.text substringFromIndex:1];
    }else {
        negativeNumber = [negativeNumber stringByAppendingString:self.currentTextField.text];
        self.currentTextField.text = negativeNumber;
    }
}
}

- (IBAction)add:(UIButton *)sender 
{
float a = [[numberOne text] floatValue];
float b = [[numberTwo text] floatValue];

float c = a + b;

[result setText:[NSString stringWithFormat:@"%.2f", c]];
}

- (void)showKeyPad
{
[UIView beginAnimations:@"animateView" context:nil];
[UIView setAnimationDuration:0.3];
CGRect keypadview = CGRectMake(0, 270, 320, 190); // puts keyboard on screen
keyPad.frame = keypadview;
keyPad.alpha = 1.0;
[UIView commitAnimations];

}

- (void)hideKeyPad
{
[UIView beginAnimations:@"animateView" context:nil];
[UIView setAnimationDuration:0.3];
CGRect keypadview = CGRectMake(0, 640, 320, 190);  // puts keyboard off screen
keyPad.frame = keypadview;
keyPad.alpha = 1.0;
[UIView commitAnimations];
}

@end

和我的.h文件是

#import <UIKit/UIKit.h>

@interface KeyPad2ViewController : UIViewController <UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *numberOne;
@property (weak, nonatomic) IBOutlet UITextField *numberTwo;
@property (weak, nonatomic) IBOutlet UILabel *result;
@property (strong, nonatomic) IBOutlet UIView *keyPad;
@property (weak, nonatomic) UITextField *currentTextField;

- (IBAction)buttonPressed:(UIButton *)sender;
- (IBAction)backspacePressed:(UIButton *)sender;
- (IBAction)clearPressed:(UIButton *)sender;
- (IBAction)enterPressed:(UIButton *)sender;
- (IBAction)octopusPressed:(UIButton *)sender;
- (IBAction)signPressed:(UIButton *)sender;
- (IBAction)add:(UIButton *)sender;

@end

感谢任何和所有帮助。这个网站真的是最好的!!

1 个答案:

答案 0 :(得分:0)

调用textFieldShouldBeginEditing:方法后,第一个响应者尚未设置为正确的字段。将textFieldShouldBeginEditing:替换为textFieldDidBeginEditing:以解决此问题。