我正在尝试做一个有条件的转场。但我得到:

    Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UILabel length]: unrecognized selector sent to instance 0x763c8e0'

设计是,当我按下按钮时,应用程序将使用 AFNetworking 的 AFJSONRequestOperation 从互联网检索一些数据。如果请求成功,它将从我当前的 UIViewController 调用另一个 UIViewController。我使用 this method 做了我的条件启动 segue,下面是我的代码:
-(void) login:(NSString*)pinString{
NSString* urlString = [NSString stringWithFormat:@"%@%@%@", [super.serverResource getURL], [super.serverResource getRequestByKey:@"login"] ,pinString];
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation *operation = [MenuletJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    [self performSegueWithIdentifier:@"login" sender:self];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON){
    switch([response statusCode]){
        case (403):{
            UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Log in failed"
                                                      message:@"Log in denied, check your PIN."
                                                      delegate:nil cancelButtonTitle:@"OK"
                                                      otherButtonTitles:nil];
            [alert show];
            break;
        }
    }
}];
[operation start];

}
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
SettingViewController *settingVC = (SettingViewController*)[storyboard instantiateViewControllerWithIdentifier:@"SettingViewController"];
[self presentViewController:settingVC animated:YES completion:nil];

}

在 SettingViewController 中执行“ViewDidLoad”之后,我得到了上面的异常。如果我不使用条件转场,而只是在 IB 上从 Button ctl 拖动到 SettingViewController,一切正常...请给我一些建议...我真的没有想法...

下面是我来自 SettingViewController 的代码:
- (void)viewDidLoad{
[super viewDidLoad];
// Do any additional setup after loading the view.
//StringResources* resource = [[StringResources alloc]init];
[self.titleLabel setText:[super.stringResource getStringByKey:@"screen_setting_title"]];
[self.createOrderLabel setText:[super.stringResource getStringByKey:@"screen_setting_create_order_label"]];
[self.refreshMenuLabel setText:[super.stringResource getStringByKey:@"screen_setting_refresh_menu_label"]];
[self.tableNumberLabel setText:[super.stringResource getStringByKey:@"screen_setting_table_number_label"]];

}

实际上,我并没有在 SettingViewController 中放置太多东西,我所做的只是以编程方式设置一些标签。我在每个 setText 上放置了断点,它们都没有错误地执行。所以异常必须发生在 ViewDidLoad 之后...

这是发生错误的地方:
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
int main(int argc, char *argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); //error occur here
    }
}

解决了......原来Segue处理一切。我需要做的就是设置一个通用的 segue。一旦我调用 [self performSegueWithIdentifier:@"login"sender:self],一切都完美无缺。谢谢大家......(覆盖prepareForSegue只会导致问题......)

最佳答案

选项 1:信任 segue

确保 segue 从您当前的 View Controller 转到 SettingViewController。

然后你保留 -performSegueWithIdentifier:sender:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
   // load data from current view controller to segue.destinationViewController
}

选项 2:手动加载模态视图 Controller

一起扔掉segue,只需从 Storyboard加载 View Controller 。
AFJSONRequestOperation *operation = [MenuletJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    SettingViewController *settingVC = (SettingViewController*)[storyboard instantiateViewControllerWithIdentifier:@"SettingViewController"];
    // load data from current view controller to settingVC
    [self presentViewController:settingVC animated:YES completion:nil];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON){
    // Error handler
}];

关于objective-c - 以编程方式调用 segue 导致 NSInvalidArgumentException,原因 : 'UILabel length' unrecognized selector,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12728564/

10-13 03:52