resignFirstResponder only occurs after processing is complete

对着背影说爱祢 提交于 2019-12-08 13:24:17

问题


I want the keyboard to go away as soon as I hit "submit" in my iphone app, and not wait until the function completes. The function takes a long time because I'm doing a synchronous HTTP request.

How come the view doesn't update until the whole function completes? I'm working in the simulator in xcode.

-(IBAction)submit:(id)sender{
    [myTitle resignFirstResponder];
    ...some ASIHTTP setup...
    [request startSynchronous];
    return;
}

回答1:


If you want to keep your current synchronous HTTP request then make these changes:

-(IBAction)submit:(id)sender{
    [myTitle resignFirstResponder];
    [self performSelector:@selector(delayedSubmit:) withObject:sender afterDelay:0];
}
-(void)delayedSubmit:(id)sender {
    ...some ASIHTTP setup...
    [request startSynchronous];
    //return;
}

I commented out return; as it is not needed in methods that don't return anything like void and IBAction (which is really void but lets IB know to pay attention to it).




回答2:


it is how code is executed in threads. "Blocks" of code all get executed to the end before something like resignFirstResponder: "activates" (really completes its execution). Therefore you can either do something like delay the start of your synchronous request, or make it asynchronous (not too difficult since you are using ASIHTTP already).



来源:https://stackoverflow.com/questions/3444480/resignfirstresponder-only-occurs-after-processing-is-complete

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!