【问题标题】:How to make shouldPerformSegueWithIdentifier to wait until block finished如何让 shouldPerformSegueWithIdentifier 等到块完成
【发布时间】:2014-08-07 13:19:23
【问题描述】:

我创建了一个推送序列并将标识符命名为“CustomIdentifier”之类的名称。用户注册成功后, shouldPerformSegueWithIdentifier 执行并推送到另一个视图控制器。如何让推送动作等到用户注册块返回成功后再执行推送动作?我现在正在做的事情如下所示,但无论注册是否成功,都会执行推送。对此有什么更好的解决方案?顺便说一句,我知道我可以创建一个 IBAction,但我使用的是 RBStoryboardLink,所以它必须是一个 push segue。并且 IBAction 不适用于 RBStoryboardLink。

- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
{
    __block BOOL should;
    if ([identifier isEqualToString:@"Question"]) {
        AVUser *newUser = [AVUser user];
        newUser.username = self.usernameField.text;
        newUser.password = self.passwordField.text;
        newUser.email = self.emailField.text;

        [newUser signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if (succeeded) {
                should = YES;
            } else {
                should = NO;
            }
        }];
    }
    return should;
}

【问题讨论】:

  • @Rushabh 似乎没有公认的答案。
  • 如果不阻塞主线程,您就无法做到这一点。您可能需要调用您的方法之前收集该信息。
  • @holex 是正确的。执行signUpInBackgroundWithBlock,如果成功则只执行segue。

标签: ios objective-c uistoryboard uistoryboardsegue


【解决方案1】:

在要执行 segue 的位置使用 performSegueWithIdentifier。这样,您只会在实际需要时才调用 segue。

[self performSegueWithIdentifier:@"yourSegue" sender:nil];

prepareForSegue 方法将在该方法被调用后立即被调用。在prepareForSegue 中,您可以编写要在执行segue 之前执行的代码。 诸如设置变量值或传输其他数据等。

希望这会有所帮助。

【讨论】:

  • 我知道 performSegueWithIdentifier。因为我没有向这个条形按钮项添加 IBAction,只有一个 push segue。那么,我应该在哪里调用这个 performSegueWithIdentifier 方法呢?在 shouldPerformSegueWithIdentifier 中?
  • 在你的块中!..当你想这样做时,在块的末尾
  • 您需要将按钮连接到执行登录的 IBAction,然后执行“手动”segue。
  • @yong ho,Mike 说的完全正确。您只需连接和 IBAction 事件,然后在您想要使用 segue 转换到下一个视图的位置使用此功能。
  • 我已经知道了。大家可以看看我的问题。因为我正在使用 rbstoryboardlink 推送另一个故事板。那么如何使用 rbstoryboardlink 进行手动转场呢?
【解决方案2】:

编辑 1:也许您应该选择:

  1. 让您的登录密码(在块中)在登录和登录成功时使用NSNotificationCenter 发送NSNotifications

    [newUser signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"LoginSuccess" object:self];
        } else {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"LoginFailure" object:self];
        }
    }];
    
  2. 在加载视图控制器时在NSNotificationCenter 上注册这些通知。

    - (void)viewDidAppear
    {
        [[NSNotificationCenter defaultCenter] void)addObserver:self selector:@selector(loginSuccess:) name:@"LoginSuccess" object:nil]
        [[NSNotificationCenter defaultCenter] void)addObserver:self selector:@selector(loginFailure:) name:@"LoginFailure" object:nil]
    }
    
  3. 在发送登录成功通知时收到通知的操作方法 (- (void)loginSuccess:(NSNotification *)notification) 中,您调用 [self performSegueWithIdentifier:@"YourIdentifier" sender:self]

  4. 在调用失败通知的操作方法- (void)loginFailure:(NSNotification *)notification 中,您可能会显示警报视图或取消阻止 UI(如果之前阻止用户不点击)。

...使用您的方法,我将使用如下调度组:

- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
{
    dispatch_group_t dispatchGroup = dispatch_group_create();
    dispatch_group_enter(dispatchGroup);

    __block BOOL should;
    if ([identifier isEqualToString:@"Question"]) {
        AVUser *newUser = [AVUser user];
        newUser.username = self.usernameField.text;
        newUser.password = self.passwordField.text;
        newUser.email = self.emailField.text;

        [newUser signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if (succeeded) {
                should = YES;
            } else {
                should = NO;
            }
            // leave group to signal end of processing
            dispatch_group_leave(dispatchGroup);
        }];
    }

    // wait until processing has finished
    dispatch_group_wait(dispatchGroup, DISPATCH_TIME_FOREVER);

    return should;
}

【讨论】:

  • 如果应该等于 NO,UI 没有响应,它被冻结。需要做什么?
  • 也许您在登录成功之前阻止了 UI。如果是这样,请确保在发生登录失败时也取消阻止它。编辑了我的帖子以提供另一个选项,该选项在设计和行为的含义上可能更加“透明”。
猜你喜欢
  • 2015-11-16
  • 2021-08-10
  • 1970-01-01
  • 1970-01-01
  • 2010-11-07
  • 2018-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多