iOS Obj-C切换多个文本字段的安全文本输入
这里是Obj-C的新手。我有6个文本字段,由用户的PIN码组成 如何切换这6个UI文本字段的安全文本条目?有一个按钮可以显示和取消显示PIN码。默认情况下,在序列图像板上选中安全文本条目 在谷歌搜索了一段时间后,我发现这段代码旨在查找视图中的所有文本字段iOS Obj-C切换多个文本字段的安全文本输入,ios,objective-c,uitextfield,Ios,Objective C,Uitextfield,这里是Obj-C的新手。我有6个文本字段,由用户的PIN码组成 如何切换这6个UI文本字段的安全文本条目?有一个按钮可以显示和取消显示PIN码。默认情况下,在序列图像板上选中安全文本条目 在谷歌搜索了一段时间后,我发现这段代码旨在查找视图中的所有文本字段 - (NSArray*) findAllTextFieldsInView:(UIView*)view { NSMutableArray* textfieldarray = [[NSMutableArray alloc] init];
- (NSArray*) findAllTextFieldsInView:(UIView*)view {
NSMutableArray* textfieldarray = [[NSMutableArray alloc] init];
for (id x in [view subviews]) {
if ([x isKindOfClass:[UITextField class]])
[textfieldarray addObject:x];
if ([x respondsToSelector:@selector(subviews)]) {
[textfieldarray addObjectsFromArray:[self findAllTextFieldsInView:x]];
}
}
return textfieldarray;
}
这是我的密码
- (IBAction) revealPIN:(id)sender {
if (self.reveal == TRUE) {
self.reveal = FALSE;
}
else {
self.reveal = TRUE;
}
NSArray* allTextFields = [self findAllTextFieldsInView:[self view]];
}
我不知道将textField.secureTextEntry=YES
或否
我输入布尔值,以便检查切换状态。这之后我要做什么?还是有其他更优雅的方式来实现它?我不知道该怎么做
谢谢 如果通过故事板或xib添加文本文件,则可以在interface builder中找到此选项。在故事板中选择您的文本字段。这里我附上了参考图片 请选中所有文本字段的“安全”选项 当用户点击代码下方的“显示”更改文本字段安全选项时
-(NSArray*)findAllTextFieldsInView:(UIView*)view{
NSMutableArray* textfieldarray = [[NSMutableArray alloc] init];
for(id x in [view subviews]){
if([x isKindOfClass:[UITextField class]]){
x. secureTextEntry = YES;
[textfieldarray addObject:x];
}
if([x respondsToSelector:@selector(subviews)]){
[textfieldarray addObjectsFromArray:[self findAllTextFieldsInView:x]];
}
}
return textfieldarray;
}
如果您通过故事板或xib添加文本文件,则可以在interface builder中找到此选项。在故事板中选择您的文本字段。这里我附上了参考图片 请选中所有文本字段的“安全”选项 当用户点击代码下方的“显示”更改文本字段安全选项时
-(NSArray*)findAllTextFieldsInView:(UIView*)view{
NSMutableArray* textfieldarray = [[NSMutableArray alloc] init];
for(id x in [view subviews]){
if([x isKindOfClass:[UITextField class]]){
x. secureTextEntry = YES;
[textfieldarray addObject:x];
}
if([x respondsToSelector:@selector(subviews)]){
[textfieldarray addObjectsFromArray:[self findAllTextFieldsInView:x]];
}
}
return textfieldarray;
}
如果要使用户能够显示/隐藏安全条目,则必须在切换方法中使用以下代码:
yourTextField.secureTextEntry = YES;
yourTextField.secureTextEntry = NO;
如果要使用户能够显示/隐藏安全条目,则必须在切换方法中使用以下代码:
yourTextField.secureTextEntry = YES;
yourTextField.secureTextEntry = NO;
由于您使用的是故事板,我将执行以下操作:
UIViewController
子类的名称。假设您的类名为MyViewControllerMyViewController.m
,因为您的每个文本字段都需要一个相同类型的出口,因此最好使用IBOutletCollection
。因此,将以下类扩展添加到MyViewController.m
:
@interface MyViewController()
@property (nonatomic) IBOutletCollection(UITextFIeld) NSArray *textFields;
@end
@implementation MyViewController
- (void)updateTextFieldsWithSecureEntry:(BOOL)secureEntryEnabled {
for (UITextField *textField in self.textFields) {
textField.secureTextEntry = secureEntryEnabled
}
}
@end
MyViewController.m
放在右侧。控制从故事板中的每个UITextField
拖动到MyViewController.m
中的textFields
属性MyViewController.m
的实现中:
@interface MyViewController()
@property (nonatomic) IBOutletCollection(UITextFIeld) NSArray *textFields;
@end
@implementation MyViewController
- (void)updateTextFieldsWithSecureEntry:(BOOL)secureEntryEnabled {
for (UITextField *textField in self.textFields) {
textField.secureTextEntry = secureEntryEnabled
}
}
@end
[self updateTextFieldsWithSecureEntry:false]
即可关闭所有6个文本字段的安全输入由于您使用的是故事板,我将执行以下操作:
UIViewController
子类的名称。假设您的类名为MyViewControllerMyViewController.m
,因为您的每个文本字段都需要一个相同类型的出口,因此最好使用IBOutletCollection
。因此,将以下类扩展添加到MyViewController.m
:
@interface MyViewController()
@property (nonatomic) IBOutletCollection(UITextFIeld) NSArray *textFields;
@end
@implementation MyViewController
- (void)updateTextFieldsWithSecureEntry:(BOOL)secureEntryEnabled {
for (UITextField *textField in self.textFields) {
textField.secureTextEntry = secureEntryEnabled
}
}
@end
MyViewController.m
放在右侧。控制从故事板中的每个UITextField
拖动到MyViewController.m
中的textFields
属性MyViewController.m
的实现中:
@interface MyViewController()
@property (nonatomic) IBOutletCollection(UITextFIeld) NSArray *textFields;
@end
@implementation MyViewController
- (void)updateTextFieldsWithSecureEntry:(BOOL)secureEntryEnabled {
for (UITextField *textField in self.textFields) {
textField.secureTextEntry = secureEntryEnabled
}
}
@end
[self updateTextFieldsWithSecureEntry:false]
即可关闭所有6个文本字段的安全输入你应该这样做:
- (IBAction) revealPIN:(id)sender {
if (self.reveal == TRUE) {
self.reveal = FALSE;
}
else {
self.reveal = TRUE;
}
NSArray* allTextFields = [self findAllTextFieldsInView:[self view]];
for tf in allTextFields
{
tf.secureTextEntry = self.reveal; // <--- Check this one
}
}
- (void) updateSecureEntryAttrForAllTextFieldsInView:(UIView*)view {
NSMutableArray* textfieldarray = [[NSMutableArray alloc] init];
for (id x in [view subviews]) {
if ([x isKindOfClass:[UITextField class]])
x.secureTextEntry = self.reveal; // <--- Check this one
if ([x respondsToSelector:@selector(subviews)]) {
[textfieldarray addObjectsFromArray:[self updateSecureEntryAttrForAllTextFieldsInView:x]];
}
}
return;
}
-(iAction)revealPIN:(id)发送方{
如果(self.discover==TRUE){
自我揭露=虚假;
}
否则{
自我揭示=真实;
}
NSArray*allTextFields=[self-FindAllTextFieldsView:[self-view]];
对于所有文本字段中的tf
{
tf.secureTextEntry=self.reveal;//您应该这样尝试:
- (IBAction) revealPIN:(id)sender {
if (self.reveal == TRUE) {
self.reveal = FALSE;
}
else {
self.reveal = TRUE;
}
NSArray* allTextFields = [self findAllTextFieldsInView:[self view]];
for tf in allTextFields
{
tf.secureTextEntry = self.reveal; // <--- Check this one
}
}
- (void) updateSecureEntryAttrForAllTextFieldsInView:(UIView*)view {
NSMutableArray* textfieldarray = [[NSMutableArray alloc] init];
for (id x in [view subviews]) {
if ([x isKindOfClass:[UITextField class]])
x.secureTextEntry = self.reveal; // <--- Check this one
if ([x respondsToSelector:@selector(subviews)]) {
[textfieldarray addObjectsFromArray:[self updateSecureEntryAttrForAllTextFieldsInView:x]];
}
}
return;
}
-(iAction)revealPIN:(id)发送方{
如果(self.discover==TRUE){
自我揭露=虚假;
}
否则{
自我揭示=真实;
}
NSArray*allTextFields=[self-FindAllTextFieldsView:[self-view]];
对于所有文本字段中的tf
{
tf.secureTextEntry=self.reveal;//您的所有文本字段都是通过StoryBoard添加的吗?您使用过StoryBoard还是xib吗?我正在使用StoryBoard设计文本字段。我还为每个文本字段添加了标记1-6。首先我检查了安全文本条目,然后我希望用户在加载视图时能够切换它,您需要设置all文本字段“textField.secureTextEntry=YES”。如果用户单击revelAction,textField.secureTextEntry=NO。不需要bool值您有多少revelAction…意味着每个测试字段作为自己的revelActon或您创建的所有文本字段一个revelAction您的所有文本字段都是通过StoryBoard添加的吗?您使用过StoryBoard或xib吗?我是您使用故事板设计文本字段。我还为每个文本字段添加了标记1-6。首先我检查安全文本条目,然后我希望用户在加载视图时能够切换它。您需要设置所有文本字段“textfield.secureTextEntry=YES”。如果用户单击操作,textfield.secureTextEntry=NO。无需bool值你有多少个RevelAction…表示每个测试字段作为自己的revelActon或所有tex