Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/104.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
Ios 尝试使用-(BOOL)textField应返回:(UITextField*)textField;使键盘在子视图中消失的步骤_Ios_Delegates_Keyboard_Uitextfield_First Responder - Fatal编程技术网

Ios 尝试使用-(BOOL)textField应返回:(UITextField*)textField;使键盘在子视图中消失的步骤

Ios 尝试使用-(BOOL)textField应返回:(UITextField*)textField;使键盘在子视图中消失的步骤,ios,delegates,keyboard,uitextfield,first-responder,Ios,Delegates,Keyboard,Uitextfield,First Responder,我已经试着解决这个问题好几个小时了。我在任何地方找到的帖子都没有提供解决方案。问题是textField在子视图中,我不知道如何让它响应Return按钮来隐藏键盘。 这是我的视图控制器 #import <UIKit/UIKit.h> #import "Combatant.h" @interface ViewController : UIViewController <CombatantDelegate> - (IBAction) addView; - (IBAc

我已经试着解决这个问题好几个小时了。我在任何地方找到的帖子都没有提供解决方案。问题是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
#导入
#输入“Combatant.h”
@界面ViewController:UIViewController
-(iAction)添加视图;
-(i)滚动;
@结束
#导入“ViewController.h”
静态整数起始=60;
@界面视图控制器()
{
NSMutableArray*自定义视图;
}
@结束
@实现视图控制器
-(无效)viewDidLoad{
[超级视图下载];
customViews=[[NSMutableArray alloc]init];
}
-(iAction)添加视图
{
Combatant*thing=[[NSBundle mainBundle]loadNibNamed:@“Combatant”所有者:nil选项:nil][0];
[self.view addSubview:thing];
thing.center=CGPointMake(self.view.center.x,startY+customViews.count*thing.bounds.size.height);
thing.combatDelegate=自我;
[自定义视图添加对象:对象];
}
-(i)滚动
{
用于(作战人员*customview中的客户)
{
[cust ROLLWASTP];
}
}
@结束
这是我的定制课程

    #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
#导入
@阶级战斗员;
@礼宾战斗员代表
@结束
@接口作战人员:UIView
{
IBOutlet UITextField*名称;
ibuitextfield*base;
IBUitextField*模具;
IBUitextField*mod;
IBUILabel*总计;
NSString*值;
int dRoll;
int-cBase;
int-cMod;
}
@属性(非原子、强、读写)NSString*值;
@属性(非原子,强)IBOutlet UITextField*名称;
@属性(非原子,强)ibuitextfield*基;
@属性(非原子,强)ibuitextfield*die;
@属性(非原子,强)IBUTextField*mod;
@属性(非原子)IBUILabel*总计;
-(无效)已被攻丝;
-(BOOL)textField应返回:(UITextField*)textField;
@属性(非原子,弱)id combatDelegate;
-(int)迪罗;
-(int)基地;
-(int)国防部;
@结束
#输入“Combatant.h”
@执行战斗员
@综合价值;
@综合名称;
@合成碱;
@综合模具;
@合成mod;
@综合总量;
-(id)initWithCoder:(NSCoder*)aDecoder
{
self=[super initWithCoder:aDecoder];
如果(自我){
}
回归自我;
}
-(BOOL)textField应返回:(UITextField*)textField{
[姓名辞职第一响应者];
返回YES;
}
-(无效)已被攻丝;
{
int tot;
tot=[self convertBase]+[self Dierroll]+[self convertMod];
total.text=[NSString stringWithFormat:@“%i”,tot];
NSLog(@“总计%i”,总计);
}
-(int)dieRoll
{
int val=[die.text intValue];
如果(val 0)
{
NSLog(@“%i die”,val);
}否则{
NSLog(@“没有骰子”);
val=0;}
int d=0;
对于(int i=0;i-1&&cBase<20)
{
返回cBase;
}
返回0;
}
-(int)convertMod
{
cMod=[mod.text intValue];
NSLog(@“mod=%i”,cMod);
如果(cMod>-20&&cMod<20)
{
返回cMod;
}
返回0;
}
-(无效)解除锁定
{
self.combatDelegate=nil;
}
@结束

除了键盘隐藏之外,代码在各个方面都很完美。

将其放入
textfielddidediting:
方法中

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

在viewDidLoad中添加以下内容:

name.delegate = self;
也可以在连接检查器的情节提要中,通过将代理出口拖动到视图控制器来执行此操作


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

您使用的是委托,它描述了它是否应该返回?在您返回之前,您正在将事件发送到文本字段键盘以返回或取消,这是不可能的。请按照@soulshined提到的代码进行尝试

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

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

编辑:以下段落快速跳转主题,因为我详细介绍了一些可能的解决方案,而不是1

目标:访问文本字段。 解决: 1) 创建一个属性,该属性公开自定义类上的文本字段。 2) 创建一个调用自定义类上的文本字段的函数

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

在这种情况下,继续创建一个函数,在该函数中,在视图上,确保它调用self.textField1。。。诸如此类,然后是self.textField2,只要确保它能击中所有人

当前,您在回调中调用name resignFirstResponder。但首先让我们回溯一下。所有textfield调用textfield都应返回其委托。因此,请确保所有4个文本字段都具有作为代理的视图。在界面生成器或代码中,self.name.delegate(或self.name.textFieldDelegate,我完全忘记了)=self;每一个

还有,记得叫“自我”,“名字”是另外一个意思。实际上是我自己。访问属性访问器。但它创建的不可见变量称为_name(与开头的下划线相同,这适用于所有属性)

现在我们知道所有字段都被正确调用了,让我们回到委托。如果您确实希望“当前活动,返回”文本字段退出其响应程序,代理将在其对象视图中传递文本字段,因此请记住首先调用resignFirst。。。在当前返回的字段上

[textField resignFirstResponder]
当然,这属于委托回调方法,因为textField在其他情况下不存在,以免您通过其名称(self.name或其他名称)访问它
- (IBAction) roll
{
    for (Combatant * cust in customViews)
    {
        [cust.name resignFirstResponder];
        [cust.die resignFirstResponder]; 
        // and on for the other 2
    }
}