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

时间:2014-10-13 10:04:44

标签: ios objective-c

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

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

首先,如上所述,我不得不评论超级电话 - 应用程序不想启动。 第二:分配     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;
    }
}

@end

文件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

我会非常乐意帮助

3 个答案:

答案 0 :(得分:1)

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

您的实际问题来自于您希望在setText:类型的对象上调用方法UIView,这由消息-[UIView setText:]: unrecognized selector sent to instance 0x8f97470指示。如果您查看documentation of the UIView class,您会看到它没有公开名为setText:的方法(或者更确切地说,它没有text属性,并且它的setter方法)。这就是应用程序崩溃的原因。

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

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

答案 1 :(得分:0)

不要评论[super viewWillAppear:animated];

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

要使用UIElements,最好使用:

- (void)viewDidLoad
{
    [super viewDidLoad];
}

self.firstNameid的{​​{1}}类型是什么?

如果是NSString的{​​{1}},您应该使用id属性。

答案 2 :(得分:0)

好的伙计们,问题解决了。 教程如下,我必须将UI元素(TextInput或Button)中的行拖到代码中以实现它。事实上,它应该是:MARK IT FIRST,然后拖动线,否则UIView将被传递。

对于专业人士来说很简单,很难为初学者检测。 无论如何,非常感谢。