简体   繁体   English

iOS UITextField和NSTimer奇数

[英]iOS UITextField & NSTimer Oddity

In each view I instantiate a "KeyHandler" object that enables the app to handle physical keyboard input. 在每个视图中,我都实例化一个“ KeyHandler”对象,该对象使应用程序能够处理物理键盘输入。

When the object is instantiated it creates a hidden textField that acts as first responder. 实例化对象时,它将创建一个充当第一响应者的隐藏文本字段。

During instantiation the following code is called: 在实例化期间,将调用以下代码:

[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(pollTextField:) userInfo:nil repeats: YES];

Poll text field looks like: 投票文本字段如下所示:

-(void) pollTextField:(NSTimer*) timer{
  NSString * str = self.textField.text;
  NSString * lastChar;
  if (!str || [str isEqualToString:@""]) {
    //no new text
  }else{

    lastChar = [str substringFromIndex:[str length] - 1];
    [self determineAction:lastChar];
    lastChar = @"";
    str = nil;
  }
  self.textField.text = @"";
}

When I press u on the physical keyboard, self.textField.text == u . 当我在物理键盘上按u时,self.textField.text == u。 However, the textField does not become blank -- even though I set it to @"". 但是,即使我将其设置为@“”,textField也不会为空。 The second time pollTextField is called self.textField.text == uu, the third time it is == u and it cycles between u and uu even though I'm not pressing any keys. 第二次pollTextField称为self.textField.text == uu,第三次称为== u,即使我没有按任何键,它也会在u和uu之间循环。 Another oddity is that the first time pollTextField is called self always includes all the correct instance variables, the next time it's called they are missing, even though self is still a KeyHandler instance and still has the same memory address. 另一个奇怪的是,即使self仍然是KeyHandler实例并且仍具有相同的内存地址,第一次调用pollTextField总是包含所有正确的实例变量,而在下一次调用它们时,它们将丢失。

EDIT: Here's how I'm creating the textField and keyhandler: 编辑:这就是我创建textField和keyhandler的方式:

UITextField* textField = [[UITextField alloc] init];
[self.view addSubview:textField];

keyHandler = [[KeyHandler alloc] initWithTextField:textField];
keyHandler.delegate = self;
[textField release];

This looks like a memory issue. 这看起来像是内存问题。

How are you creating the textField in your code? 您如何在代码中创建textField? Are you properly allocating it? 您分配正确吗?

Did you trying step-debugging and checking if the textField is a valid object in the pollTextField function? 您是否尝试过逐步调试并检查textField在pollTextField函数中是否为有效对象?

just set a breakpoint and type in the gdb console po textField to see what it gives you 只需设置一个断点并在gdb控制台中输入po textField即可查看它为您提供了什么

Edit: *Well there you go, your answer is there. 编辑:*好吧,您的答案就在那里。 The Textfield is created, assigned to the view and then released, so it's life cycle ends when you release it. 将创建文本字段,将其分配给视图,然后释放该文本字段,因此释放它的生命周期将结束。

The view is now owning it, but you can't reference it directly. 该视图现在拥有它,但是您不能直接引用它。

Add a tag value to the textfield then cast it from the tagvalue: 将标记值添加到文本字段,然后从标记值强制转换它:

 UITextField* textField = [[UITextField alloc] init];
textField.tag = 200;
[self.view addSubview:textField];

keyHandler = [[KeyHandler alloc] initWithTextField:textField];
keyHandler.delegate = self;
[textField release];

Then in your pollTextField function do this: 然后在您的pollTextField函数中执行以下操作:

UITextField* textField = (UITextField*)[self.view viewWithTag:200]; UITextField * textField =(UITextField *)[self.view viewWithTag:200];

So it looks like: 所以看起来像:

-(void) pollTextField:(NSTimer*) timer{
    UITextField* textField = (UITextField*)[self.view viewWithTag:200];
    NSString * str = textField.text;
    NSString * lastChar;
    if (!str || [str isEqualToString:@""]) {
        //no new text
    }else{

        lastChar = [str substringFromIndex:[str length] - 1];
        [self determineAction:lastChar];
        lastChar = @"";
        str = nil;
    }
    textField.text = @"";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM