gpt4 book ai didi

objective-c - 以编程方式调用 segue 原因 NSInvalidArgumentException,原因 : 'UILabel length' unrecognized selector

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:23:27 24 4
gpt4 key购买 nike

我正在尝试进行有条件的转场。但我得到:

    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”后,我得到了上面的异常。如果我不使用条件 segue,只是简单地从 Button 拖到 IB 上的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com