Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/103.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 正在编辑不正确的UITextfield_Objective C_Ios_Xcode_Keyboard_Uitextfield - Fatal编程技术网

Objective c 正在编辑不正确的UITextfield

Objective c 正在编辑不正确的UITextfield,objective-c,ios,xcode,keyboard,uitextfield,Objective C,Ios,Xcode,Keyboard,Uitextfield,我刚刚完成了一个制作自己键盘的教程。所以我利用这些知识,制作了一个真正简单的应用程序,用我自己的键盘添加两个数字。这只是两个数字。但当我点击一个文本字段时,另一个文本字段会被编辑!我已附上我的代码,任何帮助将不胜感激。感谢@spool之前的所有帮助 #import "KeyPad2ViewController.h" @interface KeyPad2ViewController () @property (nonatomic) BOOL userIsEnteringANumber; @end

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

#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

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

调用textFieldShouldBeginEditing:方法时,第一响应程序尚未设置为正确的字段。将textfield shouldbeginediting:替换为textfield didbeginediting:以解决此问题。

您确定所有连接都正确吗?@hotlicks,是的,我甚至返回以确保numberOne文本字段连接到.h文件中的numberOne,与numberTwo变量相同。关于如何确保正确连接,还有其他想法吗?你确定textFieldShouldBeginEditing:是在设置第一个响应程序之后调用的,还是在设置第一个响应程序之前调用的?@DasbLinkedIn,我输入了一个NSLog,当我第一次输入文本字段时,textFieldShouldBeginEditing没有被调用。当我点击文本字段2 numberTwo时,numberOne成为第一响应者。如果我点击numberOne,NumberWO将成为第一响应者。你知道如何改变这个吗?我阅读了苹果公司关于这些方法的文档,但没有看到任何对我有帮助的东西。谢谢。我将代码更改为:-voidtextFieldDidBeginEditing:UITextField*textField{if[numberOne isFirstResponder]{self.currentTextField=self.numberOne;self.UserIsEnteringNumber=NO;}else if[NumberWO isFirstResponder]{self.currentTextField=self.numberTwo;self.userisEnteringNumber=NO;}}}我还将enterPressed方法更改为resignFirstResponder,以便键盘再次弹出。感谢大家在这方面的帮助。