Receiver (<ViewController>) 没有带有标识符的 segue

Posted

技术标签:

【中文标题】Receiver (<ViewController>) 没有带有标识符的 segue【英文标题】:Receiver (<ViewController>) has no segue with identifier 【发布时间】:2015-07-14 21:52:51 【问题描述】:

我不知道,并尝试过重命名故事板、清理项目、更改模拟器。我无法通过 segue 让我的工作。

我有一个从 ViewController (A) 到 ViewController (B) 的手动建模segue,称为“MoveToLogin”。

当您单击登录按钮时,我会创建一个自定义对象的新实例,其中包含如下登录方法:

#ViewController.m
//If the username and password fields are not blank, attempt to log in.
        [_custom object loginUser:_usernameTxt.text password:_passwordTxt.text]

#custom object's login method (CustomObject.m)
-(void)loginUser:(NSString *)username password:(NSString *)password

    ViewController *viewcontroller = [[ViewController alloc]init];

    //Attempts to log in the user
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    NSDictionary *params = @ @"username" : username, @"password": password;
    [manager POST:_login_url parameters:params
    success:^(AFHTTPRequestOperation *operation, id responseObject)
    
        if([responseObject objectForKey:@"login_okay"])
        
            _valid_login = YES;
            [viewcontroller onSuccessfulLogin];
        
        else
        
            _valid_login = NO;
            [viewcontroller onInvalidLogin];
        
    
          failure:
     ^(AFHTTPRequestOperation *operation, NSError *error) 
         [viewcontroller onRequestFail];
     ];

正如您所见,自定义对象的方法显然在另一个类中,在接收到来自服务器的 json 响应时,调用 ViewController (a) 方法 onSuccessfulLogin。

那个方法看起来像这样:

-(void)onSuccessfulLogin

    //if we get a successful login
    [self performSegueWithIdentifier:@"MoveToLogin" sender:nil];

但每当我成功登录时,都会收到这条令人愉快的消息:

由于未捕获的异常而终止应用程序 'NSInvalidArgumentException',原因:'Receiver () has no segue with identifier 'MoveToLogin''

【问题讨论】:

您不想在自定义对象中创建新的视图控制器,而是希望它通过委托或将完成块传递给您的登录名来引用拥有自定义对象的现有视图控制器方法。您正在执行 segue 的视图控制器不是当前在屏幕上的视图控制器,并且没有使用 instantiateViewControllerWithIdentifier: 从情节提要正确创建,因此它不会设置 segue。 【参考方案1】:

你不能像你正在做的那样创建viewcontroller 的实例。一般来说,让你的异步方法使用块/委托是件好事。 使用完成块做这样的事情怎么样..

    #ViewController.m
    //If the username and password fields are not blank, attempt to log in.
     [_customobject  loginUser:@"" password:@"" completionBlock:^(BOOL validLogin, NSError *error) 
        if(error)
            [self onRequestFail];
        else
            if(validLogin)
                    //if we get a successful login
                 [self performSegueWithIdentifier:@"MoveToLogin" sender:nil];
            else
                //if we get a invalid login
            
                
    ];

    #custom object's login method (CustomObject.m)
    -(void)loginUser:(NSString *)username password:(NSString *)password completionBlock:(void (^)(BOOL validLogin, NSError *error))completionblock

        //Attempts to log in the user
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        manager.requestSerializer = [AFHTTPRequestSerializer serializer];
        manager.responseSerializer = [AFJSONResponseSerializer serializer];
        NSDictionary *params = @ @"username" : username, @"password": password;
        [manager POST:_login_url parameters:params
        success:^(AFHTTPRequestOperation *operation, id responseObject)
        
            if([responseObject objectForKey:@"login_okay"])
            
                _valid_login = YES;
                if (completionblock) completionblock(YES, nil);
            
            else
            
                _valid_login = NO;
                if (completionblock) completionblock(NO, nil);
            
        
              failure:^(AFHTTPRequestOperation *operation, NSError *error) 
             if (completionblock) completionblock(NO, error);
         ];
    

【讨论】:

非常感谢您的帮助和解释!效果很好。

以上是关于Receiver (<ViewController>) 没有带有标识符的 segue的主要内容,如果未能解决你的问题,请参考以下文章

Receiver (<ViewController>) 没有带有标识符的 segue

IOS7 tableview底部viewcontrol中的多余空间

Receiver (<ViewController:>) 没有带有标识符的 segue - 从场景中启动 segue

BroadCast Receiver的使用

Broadcat监视电量变化

Qt文档阅读笔记-Broadcast Receiver Example解析