首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >becomeFirstResponder非常慢

becomeFirstResponder非常慢
EN

Stack Overflow用户
提问于 2014-07-11 15:23:31
回答 1查看 2K关注 0票数 3

我有以下viewDidLoad方法:

代码语言:javascript
复制
- (void)viewDidLoad {
    NSLog(@"didLoad");
    if (self.loginField.text.length > 0) [self.passwordField becomeFirstResponder];
    else [self.loginField becomeFirstResponder];
}

我还在viewWillAppearviewDidAppear中添加了日志时间。

在某些情况下,推送动画需要很长时间。我已经用注释(和没有) if-else行来度量时间(参见:时间显示在下面)。我不知道什么能减缓我的应用程序之间的viewWillAppearviewDidAppear调用。

我试着用Time Profiler (仪器)对这段代码进行分析,但它什么也没有显示。我不知道该怎么做,以便更快地展示我的观点。有什么想法吗?

与becomeFirstResponder,first call

代码语言:javascript
复制
2014-07-11 16:51:41.090 didLoad
2014-07-11 16:51:41.133 willAppear
2014-07-11 16:51:44.223 did appear
diffAppear = 3090ms

与becomeFirstResponder,第二次调用

代码语言:javascript
复制
2014-07-11 16:52:01.370 didLoad
2014-07-11 16:52:01.400 willAppear
2014-07-11 16:52:02.109 did appear
diffAppear = 709ms

不带becomeFirstResponder,先调用

代码语言:javascript
复制
2014-07-11 16:57:21.720 didLoad
2014-07-11 16:57:21.754 willAppear
2014-07-11 16:57:22.420 did appear
diffAppear = 666ms

不带becomeFirstResponder,第二次调用

代码语言:javascript
复制
2014-07-11 16:57:31.851 didLoad
2014-07-11 16:57:31.870 willAppear
2014-07-11 16:57:32.541 did appear
diffAppear = 671ms
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-07-11 16:27:12

评论中的@holex metion:

-becomeFirstResponser通常加载实际对象的输入视图,这需要时间。另一方面,在视图位于导航堆栈和视图-层次结构之后,您应该正确地调用该方法,这意味着在这种情况下:在-viewDidAppear:方法中或之后,不要更早。

我把–becomeFirstResponser搬到了-viewDidAppear:。现在,根据这个question,我添加了这些观察者,如下所示,现在它应该应用程序。

代码语言:javascript
复制
- (void)willShowKeyboard:(NSNotification *)notification {
    [UIView setAnimationsEnabled:NO];
}

- (void)didShowKeyboard:(NSNotification *)notification {
    [UIView setAnimationsEnabled:YES];
}

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willShowKeyboard:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didShowKeyboard:) name:UIKeyboardDidShowNotification object:nil];

这不是一个完美的解决方案(推动画没有键盘),但对我来说已经足够好了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24701220

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档