- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经尝试解决这个问题好几个小时了。我在任何地方找到的帖子都没有提供解决方案。问题是 textField 在 subview 中,我不知道如何让它响应 Return 按钮以隐藏键盘。这是我的 View Controller ;
#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
这是我的自定义类;
#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
除键盘隐藏外,该代码在各个方面都完美无缺。
最佳答案
在最基本的意义上,您希望当前事件的文本字段 resignFirstResponder。您的问题似乎是如何访问该文本字段。
编辑:以下段落快速跳转主题,因为我正在详细说明一些可能的解决方案而不是 1 个。
目标:访问文本字段。解决方案:1) 创建一个在自定义类上公开文本字段的属性。2) 创建一个调用自定义类上的文本字段的函数。
假设每个 View 上有 4 个文本字段,您有 4 个属性公开文本字段?
在那种情况下,继续创建一个函数,并在该函数中,在 View 中,确保它调用 self.textField1 resign...等等,然后是 self.textField2,只要确保它命中所有这些。
目前您在回调中调用 name resignFirstResponder。但首先让我们回溯一下。所有 textFields 在其委托(delegate)上调用 textFieldShouldReturn。因此,请确保所有 4 个文本字段都具有委托(delegate) View 。在界面生成器或代码中,self.name.delegate(或 self.name.textFieldDelegate,我完全忘记了)= self;每一个。
还有,记得叫“self.”,“name”是别的。其实自己。访问属性访问器。但是它创建的不可见变量称为 _name(开头有下划线,这适用于所有属性)。
现在我们知道您的所有字段都被正确调用了,让我们回到委托(delegate)。如果您确实希望“当前事件的、正在返回的”文本字段退出其响应者,委托(delegate)将在其对象 View 中传递 textField,因此请记住在当前返回的字段上调用 resignFirst...。
[textField resignFirstResponder]
当然,这属于委托(delegate)回调方法,因为 textField 不存在,以免您通过其名称(self.name 或其他名称)访问它。
最后但同样重要的是,在所有内容后面放一个按钮,但要大到足以覆盖屏幕,让它调用一个函数。在该函数中,您可以“展开”对每个文本字段的调用。基本上,让该函数在每个自定义 View 上调用“hideTheKeyboard”或任何您命名的函数(调用 resignFirst 的函数...在自定义 View 中的所有字段上)。
它应该看起来像带有 for(view in customViews) 的函数,但它不是调用 roll,而是调用您的自定义函数。
在那个函数中,仅仅因为它是一个可能的解决方案,你也可以做类似的事情
- (IBAction) roll
{
for (Combatant * cust in customViews)
{
[cust.name resignFirstResponder];
[cust.die resignFirstResponder];
// and on for the other 2
}
}
但是,如您所知,您可能希望将其封装到您调用的函数中的原因仅仅是为了减少代码行数/简单性。比找到可行的解决方案更重要的是您了解如何以及为什么可以这样做。我在上面列出了 3 种解决方案(使用属性、使用函数访问 textField 或使用委托(delegate))。
最简单的是委托(delegate),最紧凑的是函数,最广泛的是属性。 (每个也有限制)。我想提供一个透彻的理解,以便您可以看到它们。
关于ios - 尝试使用 -(BOOL) textFieldShouldReturn :(UITextField *)textField; to make the keyboard go away in a subview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28145707/
我有一个带有列的表提供者 implied(tiny int)(something like nullable bool) provi
我正在阅读 VideoFileWriter来自 AForge.Video.FFMPEG 的类(class)通过 ILSPY 组装(我很想看看特定方法是如何工作的)并发现了这个: public bool
这是我的完整代码... import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import
我有一个输入 list类型 [Maybe SomeType]和一个谓词 p类型 SomeType -> Bool ,我想回答这个问题“谓词 p 是否适用于所有碰巧在输入中的 SomeType ?”。
使用 !!x 有什么区别吗?对比(bool)x ? 假设__STDC_VERSION__ >= 199901L和 #include 他们都保证结果是0吗?或 1 ,并且无论 x 的大小和值如何,都不
我正在编写一些 C++ 代码,我想调用两个函数(checkXDirty 和 checkYDirty),并返回 true如果任一返回 true。即使一个返回 true 我也需要评估两者,所以我的第一个想
我注意到 bool在 QtCreator 中以不同于其他类型的颜色突出显示: 只有在包含某些 header 时才会发生这种情况,最终我将其追踪到 . QtCreator 的代码检查器似乎无法手动跟踪
有一个函数: func (first: Int) -> Int -> Bool -> String { return ? } 返回值怎么写?我对上面 func 的返回类型感到很困惑。 最
训练神经网络学习“异或” 我正在尝试使用“批量归一化”,我创建了一个批量归一化层函数“batch_norm1”。 import tensorflow as tf import nump
我已经创建了任务函数来验证我的 json 文件。一切正常,直到我没有使用结果。当我试图从 async task function 获得结果时它显示错误为 Cannot implicitly conve
我有一个函数 func login (parameters: [(String, Any)], completion: @escaping (Bool) -> Vo
我正在处理最近从 X/Motif 转移到 Qt 的 C++ 代码库。我正在尝试编写一个 Perl 脚本,它将用 bool 替换所有出现的 Boolean(来自 X)。该脚本只是做了一个简单的替换。 s
嗨,我正尝试创建一个Visiblity小部件,如果用户在Firebase数据库阵列上,该小部件将显示。看起来像这样(成员数组): 如您所见,我创建了一个StreamBuilder,如果当前用户的用户名
我创建了如下的rest api方法, Future activateAccount(int id, int code) async{ final body = {"code": '$c
在我的Flutter应用中,我有一个返回Future的函数,但我想将结果作为Stream。这是函数: Future isGpsOn() async { if (await Geolocat
我可以看到 BOOLEAN 覆盖了 __visit_name__ class BOOLEAN(Boolean): __visit_name__ = 'BOOLEAN' 控制调度员选择的访问者方
考虑以下代码: bool x; bool? y = null; x = y?? true; 将 bool? 分配给 bool 是一个编译时错误,但上面的代码在编译和运行时都成功了。为什么?尽管第三条语
我正在重写一些 Javascript 代码以在 Excel VBA 中工作。由于在这个网站上搜索,我已经设法翻译了几乎所有的 Javascript 代码!但是,有些代码我无法准确理解它在做什么。这是一
我想拍一张bool来自Vec并在 if 语句中进行比较。如何解决以下错误? | 7 | if cell { | ^^^^ expected
我在我的应用程序崩溃跟踪工具中发现了一些崩溃。基本上我有一个 tabBarController,其中一个选项卡有一个嵌入式 UIWebView,另一个选项卡有一个带有 UITableView 的 Co
我是一名优秀的程序员,十分优秀!