尝试使用 -(BOOL) textFieldShouldReturn:(UITextField *)textField;使键盘在子视图中消失

trying to use -(BOOL) textFieldShouldReturn:(UITextField *)textField; to make the keyboard go away in a subview

我已经尝试解决这个问题好几个小时了。 None 我在任何地方找到的帖子都提供了解决方案。问题是 textField 在子视图中,我不知道如何让它响应 Return 按钮以隐藏键盘。 这是我的视图控制器;

    #import <UIKit/UIKit.h>
#import "Combatant.h"
@interface ViewController : UIViewController <CombatantDelegate>


- (IBAction) addView;
- (IBAction) roll;

@end

    #import "ViewController.h"

static int startY = 60;

@interface ViewController ()
{
    NSMutableArray * customViews;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    customViews = [[NSMutableArray alloc] init];
}

- (IBAction) addView
{
    Combatant * thing = [[NSBundle mainBundle] loadNibNamed:@"Combatant" owner:nil options:nil][0];
    [self.view addSubview: thing];
    thing.center = CGPointMake(self.view.center.x, startY + customViews.count * thing.bounds.size.height);
    thing.combatDelegate = self;
    [customViews addObject:thing];
}

- (IBAction) roll
{
    for (Combatant * cust in customViews)
    {
        [cust rollWasTapped];
    }
}
@end

这是我的习惯Class;

    #import <UIKit/UIKit.h>

@class Combatant;

@protocol CombatantDelegate <NSObject>

@end



@interface Combatant : UIView <UITextFieldDelegate>
{
    IBOutlet UITextField * name;
    IBOutlet UITextField * base;
    IBOutlet UITextField * die;
    IBOutlet UITextField * mod;
    IBOutlet UILabel * total;
    NSString *value;
    int dRoll;
    int cBase;
    int cMod;
}

@property (nonatomic, strong, readwrite) NSString *value;
@property (nonatomic, strong) IBOutlet UITextField * name;
@property (nonatomic, strong) IBOutlet UITextField * base;
@property (nonatomic, strong) IBOutlet UITextField * die;
@property (nonatomic, strong) IBOutlet UITextField * mod;
@property (nonatomic) IBOutlet UILabel * total;
-(void) rollWasTapped;
-(BOOL) textFieldShouldReturn:(UITextField *)textField;
@property (nonatomic, weak) id<CombatantDelegate> combatDelegate;
-(int) dieRoll;
-(int) convertBase;
-(int) convertMod;
@end

    #import "Combatant.h"

@implementation Combatant

@synthesize value;
@synthesize name;
@synthesize base;
@synthesize die;
@synthesize mod;
@synthesize total;

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {

    }
    return self;
}

-(BOOL) textFieldShouldReturn:(UITextField *)textField{
    [name resignFirstResponder];
    return YES;
}

- (void) rollWasTapped;
{
    int tot;
    tot = [self convertBase] + [self dieRoll] + [self convertMod];
    total.text = [NSString stringWithFormat:@"%i", tot];
    NSLog(@" totaling %i ", tot);
}

 -(int) dieRoll
 {
     int val = [die.text intValue];
     if (val <7 && val >0)
     {
         NSLog(@" %i die", val);
     } else {
         NSLog(@" there are no dice");
         val = 0;    }
     int d = 0;
     for (int i = 0; i < val; i++) {
         d = d + arc4random()%6 + 1;
     }
     dRoll = d;
     NSLog(@"%i die roll result = %i ", val, dRoll);
     return dRoll;
 }

 -(int) convertBase
 {
     cBase = [base.text intValue];
     NSLog(@"base = %i", cBase);
     if (cBase > -1 && cBase < 20)
     {
         return cBase;
     }
     return 0;
}

 -(int) convertMod
 {
    cMod = [mod.text intValue];
    NSLog(@"mod = %i", cMod);
    if (cMod > -20 && cMod < 20)
    {
        return cMod;
    }
    return 0;
 }

- (void) dealloc
{
    self.combatDelegate = nil;
}
@end

除键盘隐藏外,代码在各个方面都完美无缺。

放在textFieldDidEndEditing:方法中

- (void)textFieldDidEndEditing:(UITextField *)textField {
{
[textField resignFirstResponder];
}

在 viewDidLoad 中添加:

name.delegate = self;

或者您可以在连接检查器的情节提要中执行此操作,方法是将委托出口拖到视图控制器。

PS: 在这种情况下,textFieldShouldReturn 方法也应该在视图控制器中。

您正在使用描述是否应该 return 的委托?在你 returning 之前,你正在将事件发送到你的文本字段键盘到 return 或关闭,这是不可能的。请尝试@soulshined 提到的代码。

- (void)textFieldDidEndEditing:(UITextField *)textField {
{
    [textField resignFirstResponder];
}

在最基本的意义上,您希望当前活动的文本字段 resignFirstResponder。您的问题似乎是如何访问该文本字段。

编辑:以下段落快速跳转主题,因为我正在详细说明一些可能的解决方案而不是 1 个。

目标:访问文本字段。 解决方案: 1) 创建一个 属性,在自定义 class 上公开文本字段。 2) 创建一个调用自定义 class.

上的文本字段的函数

假设每个视图上有 4 个文本字段,您有 4 个属性公开文本字段?

在那种情况下,继续创建一个函数,在该函数中,在视图中,确保它调用 self.textField1 resign...等等,然后是 self.textField2,只要确保它命中所有这些.

目前您在回调中调用了 name resignFirstResponder。但首先让我们回溯一下。所有 textFields 在其委托上调用 textFieldShouldReturn。因此,请确保所有 4 个文本字段都具有委托视图。在界面构建器或代码中,self.name.delegate(或self.name.textFieldDelegate,我忘记了)= self;每一个。

还有,记得叫"self.","name"是另外一回事。其实自己。访问 属性 访问器。但是它创建的不可见变量称为_name(开头有下划线的东西,这适用于所有属性)。

现在我们知道您的所有字段都被正确调用了,让我们回到委托。如果您确实希望 "currently active, returning" 文本字段退出其响应者,委托将在其对象视图中传递 textField,因此请记住在当前返回的字段上调用 ​​resignFirst...。

[textField resignFirstResponder]

当然,这属于委托回调方法,因为 textField 不存在,以免您通过其名称(self.name 或其他名称)访问它。

最后但同样重要的是,在所有内容后面放一个按钮,但要大到足以覆盖屏幕,让它调用一个函数。在该函数中,您可以 "splay" 调用每个文本字段。基本上,使该函数在每个自定义视图上调用 "hideTheKeyboard" 或您命名的任何名称(调用 resignFirst 的函数...在自定义视图中的所有字段上)。

它应该看起来像您的函数 for(view in customViews) 但它不是调用 roll,而是调用您的自定义函数。

在那个函数中,仅仅因为它是一个可能的解决方案,你也可以做类似

- (IBAction) roll
{
    for (Combatant * cust in customViews)
    {
        [cust.name resignFirstResponder];
        [cust.die resignFirstResponder]; 
        // and on for the other 2
    }
}

但是,如您所知,您可能希望将其封装到您调用的函数中的原因只是为了减少 code/simplicity 行。比找到可行的解决方案更重要的是您了解如何以及为什么可以这样做。我在上面列出了 3 种解决方案(使用 属性、使用函数访问 textField 或使用委托)。

最简单的是委托,最紧凑的是函数,最广泛的是属性。 (每个也有限制)。我想给你一个透彻的理解,这样你就可以看到它们了。