无法在当前项目中隐藏iOS键盘(xcode 5)

时间:2014-07-06 15:43:16

标签: ios iphone objective-c xcode5

Xcode 5 / iOS 7。

这个问题一直让我疯狂一天的大部分时间。在我目前的项目中,我无法使用resignFirstResponder隐藏iOS键盘。然而,奇怪的是,我能够在我目前正在开展的所有其他项目中使用它。代码如下。非常感谢,伙计们!

ViewController.h:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITextFieldDelegate>
{    
    IBOutlet UISegmentedControl *selectcontrolNumbers;
    IBOutlet UILabel *output;
    int grossNetVar;
    double calcVariable;
    IBOutlet UIButton *hideConvertNet;
    IBOutlet UIButton *hideConvertGross;

}

@property (strong, nonatomic) IBOutlet UITextField *dollarTextField;
@property (nonatomic)int grossNetVar;
@property (nonatomic) double calcVariable;
- (IBAction)selectAction:(id)sender;
- (IBAction)convertToNet:(id)sender;
- (IBAction)convertToGross:(id)sender;
@end

ViewController.m:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize grossNetVar;
@synthesize calcVariable;

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.dollarTextField.delegate = self;
}

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.dollarTextField resignFirstResponder];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)selectAction:(id)sender {

    if (selectcontrolNumbers.selectedSegmentIndex == 0){
        grossNetVar = 1;
    } else {
        grossNetVar = 2;
    }

    if (grossNetVar == 1) {
        hideConvertGross.enabled = YES;
        hideConvertNet.enabled = NO;
    } else {
        hideConvertGross.enabled = NO;
        hideConvertNet.enabled = YES;
    }
}

- (IBAction)convertToNet:(id)sender {
    NSString *textFieldText = _dollarTextField.text;
    calcVariable = .85;
    double dollarTotal = [textFieldText doubleValue];
    double netCalc = calcVariable * dollarTotal;

    NSString *netString = [[NSString alloc]initWithFormat:@"The NET total is %g", netCalc];
    output.text = netString;

}

- (IBAction)convertToGross:(id)sender {
}

@end

1 个答案:

答案 0 :(得分:0)

我建议使用一个工具栏(你可以将一个工具栏拖到屏幕上进入故事板),只有在键盘打开时才显示,并在其上放置一个完成按钮。创建一个IBAction,用于触摸您可以执行的按钮

 [donebutton resignFirstResponder];