Segue 未以编程方式执行

Segue Not Being Performed Progmatically

我有一个由 performSegueWithIdentifier: 以编程方式调用的 segue,但它不会触发。但是,当我使用按钮创建 segue 时,按下 segue 可以正常工作。我还尝试在代码中更改我的 segue 的名称,它会产生 no segue with identifier 错误。

这是我的代码(注意:在两个不同的地方调用 segue 以检查它是否可以在代码的其他地方工作。)

#import "SignUpViewController.h"
#import "ProgressHUD.h"

@interface SignUpViewController ()

@end

@implementation SignUpViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self performSegueWithIdentifier:@"profilePic" sender:self];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)createAnAccount:(id)sender {

    [ProgressHUD show:@"Please Wait"];

    if ([self.passwrod.text isEqualToString:self.confirmPassword.text]){
        // Register User
        PFUser *user = [PFUser user];
        user.username = self.username.text;
        user.password = self.passwrod.text;
        user.email = self.eMail.text;

        // other fields can be set if you want to save more information

        NSString *name = [self.firstName.text stringByAppendingString:@" "];
        NSString *fullName = [name stringByAppendingString:self.lastName.text];

        user[@"name"] = fullName;

        user[@"posts"]     = @0;
        user[@"score"]     = @5;
        user[@"followers"] = @0;
        user[@"following"] = @0;

        [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if (!error) {

                // Hooray! Let them use the app now.

                [self performSegueWithIdentifier:@"profilePic" sender:self];

                [ProgressHUD showSuccess:nil];
                NSLog(@"Perform Segue");

            } else {
                NSString *errorString = [error userInfo][@"error"];
                // Show the errorString somewhere and let the user try again.

                [ProgressHUD showError:errorString];
            }
        }];

    } else {
        // Alert User
        [ProgressHUD showError:@"Please check your passwords as they do not match."];
    }

}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    NSLog(@"Preparing for segue");
    NSLog(@"Segue: %@", segue.identifier);
}

@end

澄清更新:正在调用和记录 prepareForSegue 方法。

感谢您的帮助!

您不应该在 viewDidLoad 中调用 performSegueWithIdentifier。事实上,我很确定如果这样做,您会在控制台中看到警告或错误。将呼叫移至 viewDidAppear

对我有用的解决方案是删除有问题的视图控制器的所有转场,然后重新添加它们。这可能不适合所有人,但值得一试。