在执行选择器"selectAll"时出现奇怪的问题.在UITextField上 [英] Wierd issues when performing selector "selectAll" on UITextField

查看:193
本文介绍了在执行选择器"selectAll"时出现奇怪的问题.在UITextField上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在面对有史以来最可怕的错误(无论是在我的应用程序中还是在IOS 7.1中). 几个小时后,我设法创建了一个简单的应用程序来演示问题.

I am facing the the wierdest bug (ether in my app or in IOS 7.1) ever. After many hours I managed to create a simple app that demonstrate the problem.

两个UITextField-从接口构建器中拖放并连接到t1,t2. ViewController:

Two UITextField - Dragged and dropped from interface builder and wired to t1, t2. the ViewController:

@implementation ViewController
@synthesize t1;
@synthesize t2;
#pragma mark - UITextFieldDelegate


-(void)textFieldDidBeginEditing:(UITextField *)iTextField {
    NSLog(@"textFieldDidBeginEditing");
    [iTextField performSelector:@selector(selectAll:) withObject:iTextField afterDelay:0.0];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    return YES;
}

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

@end

相同时间上同时点击t1和t2时,两个textField都将成为无休止的循环中的第一响应者! 当省略PerformSelector语句或textField:shouldChangeCharactersInRange:实现时,问题就消失了.

When tapping on t1 and t2 at the same time, both textFields become first responder in an endless loop! When omitting ether the PerformSelector statement or the textField:shouldChangeCharactersInRange: implementation, problem is gone.

有人可以解释为什么会发生吗?

Can someone explain why does it happen?

推荐答案

还将每个UITextField的ExclusiveTouch属性设置为:是,以防止它们同时进行编辑.

Also set the exclusiveTouch property of each UITextField to: YES to prevent them from editing at the same time.

- (void)viewDidLoad
{
    [super viewDidLoad];
    t1.exclusiveTouch = YES;
    t2.exclusiveTouch = YES;
    t1.delegate = self;
    t2.delegate = self;
}

- (void)textFieldDidBeginEditing:(UITextField *)iTextField
{
    [iTextField performSelector:@selector(selectAll:) withObject:nil afterDelay:0.0];
}

或更简单的方法是不使用ExclusiveTouch属性:

Or more simply without using the exclusiveTouch properties:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)iTextField
{
    if (iTextField == t1 && t2.isFirstResponder == NO)
    {
        return YES;
    }
    else if (iTextField == t2 && t1.isFirstResponder == NO)
    {
        return YES;
    }

    return NO;
}

这篇关于在执行选择器"selectAll"时出现奇怪的问题.在UITextField上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆