如何使shouldPerformSegueWithIdentifier等到块完成

时间:2014-08-07 13:19:23

标签: ios objective-c uistoryboard uistoryboardsegue

我创建了一个push segue,并将标识符命名为" CustomIdentifier"。用户成功注册后,执行shouldPerformSegueWithIdentifier并推送到另一个视图控制器。如何使推送操作等到用户注册块返回成功然后执行推送操作?我现在正在做的就像下面一样,但无论注册不成功,都会执行推送。对此更好的解决方案是什么?顺便说一句,我知道我可以创建一个IBAction,但我使用的是RBStoryboardLink,因此它必须是一个推送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;
}

2 个答案:

答案 0 :(得分:2)

在要执行segue的位置使用performSegueWithIdentifier。通过这种方式,只有在实际需要IT时才会调用segue。

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

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

希望这有帮助。

答案 1 :(得分:1)

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

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

    [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(如果在用户未单击之前被阻止)。

  5. ...使用您的方法,我会使用一个调度组,如:     

    - (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;
    }