【问题标题】:Segue Not Being Performed ProgmaticallySegue 没有以编程方式执行
【发布时间】:2015-01-04 23:44:31
【问题描述】:

我有一个被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 方法正在被调用并记录。

感谢您的帮助!

【问题讨论】:

  • 您收到错误是因为您没有在 IB 中命名 segue 标识符。请参阅HERE,如果您有命名的标识符,那么您可以实例化 VC

标签: ios objective-c segue


【解决方案1】:

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

【讨论】:

  • 另外,从后台块调用它是个坏主意
  • 这不是一个可行的选择,因为 OP 希望它基于 IBAction 来完成。每次出现 VC 屏幕时,您的建议都会强制进行 segue。不是什么好事。 OP 说他们只是把它放在那里看看它是否会起作用。
  • 它在 IBAction 中不起作用。它仅在我在storybaord中创建从按钮到视图控制器的segue(不将其连接到IBAction)时才有效。编辑:我也没有在控制台中收到任何错误。正在调用 prepareForSegue 方法。
  • 你真的在storyboard中设置了segue的标识符吗?单击 segue 中间的图标,然后在检查器中输入 profilePic 作为标识符。还要确保从视图控制器本身而不是按钮创建 segue。
  • 是的,我已经完成了所有这些事情,并且仍然注意到。如果我在 segue 调用上方放置一个 NSLog 语句,它几乎会立即在调试器中显示它。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2017-05-12
    • 2017-10-03
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多