gpt4 book ai didi

iOS NSInvalidArgumentException,无法识别的选择器发送到实例

转载 作者:行者123 更新时间:2023-11-29 02:36:08 25 4
gpt4 key购买 nike

我是 iOS 的新手,正在学习教程。现在,我有这段代码:

- (void)viewWillAppear:(BOOL)animated
{
// [super viewWillAppear:animated];
if(self.firstName)
{
NSLog(@"%@",self.firstName);
self.textFieldFirstName.text = self.firstName;
}
}

首先,如上所述,我不得不注释掉对 super 的调用 - 该应用不想启动。第二:任务 self.textFieldFirstName.text = self.firstName;使用 SIGABORT 使应用程序崩溃并输出:

2014-10-13 11:50:19.631 PersonalStoryboard[1451:60b] firstName = John
2014-10-13 11:50:19.636 PersonalStoryboard[1451:60b] John
2014-10-13 11:50:19.636 PersonalStoryboard[1451:60b] -[UIView setText:]: unrecognized selector sent to instance 0x8f97470
2014-10-13 11:50:19.639 PersonalStoryboard[1451:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView setText:]: unrecognized selector sent to instance 0x8f97470'
*** First throw call stack:

为什么?提前致谢。


谢谢大家。文件1:

//

#import "FirstNameEditViewController.h"

@interface FirstNameEditViewController ()
@property (strong, nonatomic) IBOutlet UITextField *textFieldFirstName;
@end

@implementation FirstNameEditViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)viewWillAppear:(BOOL)animated
{
// [super viewWillAppear:<#animated#>];
if(self.firstName)
{
NSLog(@"%@",self.firstName);
self.textFieldFirstName.text = self.firstName;
}
}

@结束

文件2:

#define LogPretty NSLog(@"LogPretty:%s %d %s",__FILE__,__LINE__,__PRETTY_FUNCTION__);
#import "PersonalDetailViewController.h"
#import "FirstNameEditViewController.h"
#import "Person.h"

@interface PersonalDetailViewController ()

@property (strong, nonatomic) IBOutlet UIButton *buttonFirstName;
@property (strong, nonatomic) IBOutlet UIButton *buttonName;
@property (strong, nonatomic) IBOutlet UIButton *buttonAge;
@property (strong, nonatomic) IBOutlet UIButton *buttonJob;
@property (strong, nonatomic) IBOutlet UIButton *buttonSalary;

@end

@implementation PersonalDetailViewController
{
Person *person;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
person = [[Person alloc] init];
[self showPerson];
}

- (void)showPerson
{
LogPretty
/*
IT CRASHES HERE !!!

[self.buttonFirstName setTitle:person.firstName forState:UIControlStateNormal];
[self.buttonName setTitle:person.name forState:UIControlStateNormal];
[self.buttonJob setTitle:person.job forState:UIControlStateNormal];
[self.buttonAge setTitle:person.age forState:UIControlStateNormal];
[self.buttonSalary setTitle:person.salary forState:UIControlStateNormal];
*/
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)buttonFirstNameTouched:(id)sender {
[self performSegueWithIdentifier:@"firstNameEditSegue" sender:(self)];
}
- (IBAction)buttonNameTouched:(id)sender {
}
- (IBAction)buttonAgeTouched:(id)sender {
}
- (IBAction)buttonJobTouched:(id)sender {
}
- (IBAction)buttonSalaryTouched:(id)sender {
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSLog(@"identifier = %@",segue.identifier);

if([segue.identifier isEqualToString:@"firstNameEditSegue"])
{
NSLog(@"firstName = %@",person.firstName);
FirstNameEditViewController *editController = segue.destinationViewController;
editController.firstName = person.firstName;
}
}
@end

最后是人:

#define LogPretty NSLog(@"%s %d %s",__FILE__,__LINE__,__PRETTY_FUNCTION__);
#import "Person.h"

@implementation Person

- (id) init
{
LogPretty
self = [super init];
if(self)
{
self.firstName = @"John";
self.name = @"Brown";
self.job = @"Developer";
self.age = 35;
self.salary = 35000;
}
return self;
}
@end

我将非常感谢您的帮助

最佳答案

好的,首先,对 super 的调用很重要,如果您必须将其注释掉以使您的代码正常工作,则其他地方也一定有问题。它很重要,因为它负责在您的类的父类(super class) 中执行操作,即UIViewController。此方法是 UIViewController 生命周期的一部分。

您的实际问题来自于您想要在 UIView 类型的对象上调用方法 setText:,消息表明了这一点: -[UIView setText:]: 无法识别的选择器发送到实例 0x8f97470。如果你看一下 documentation of the UIView class ,您会看到它没有公开一个名为 setText: 的方法(或者更确切地说,它没有 text 属性以及它的 setter 方法) .这就是应用程序崩溃的原因。

我假设您想在 UILabelUITextField 上执行此操作。我还可以想象的是,您实际上已经在使用这些类中的任何一个,但它不起作用,因为您评论了 viewWillAppear。在那种情况下,您应该显示更多代码和 Storyboard设置,否则将很难找到您的问题或给您进一步的指导。

最后的简短说明,这种初始化最好在 viewDidLoad 而不是 viewWillAppear 中完成。

关于iOS NSInvalidArgumentException,无法识别的选择器发送到实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26337378/

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