抛出终止异常

时间:2013-07-07 22:28:56

标签: objective-c

我正在使用在线树屋教程学习Objective C的过程我一步一步地按照所有步骤进行操作,并在按下按钮时出现错误。我希望有人能告诉我这里做错了什么。到目前为止应用程序应该在按钮被按下时在顶部有一个标签,在故事板中间有一个按钮,它应该改变标签的文本。我的代码如下。

.m文件

//
//  ViewController.m
//  Prediction
//
//  Created by Brandon Brown on 7/7/13.
//  Copyright (c) 2013 Brandon Brown. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize predictionLabel;

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

- (void)viewDidUnload
{
    [self setPredictionLabel:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (IBAction)buttonPressed:(id)sender {
    self.predictionLabel.text = @"Yep";
}
@end

.h文件

//
//  ViewController.h
//  Prediction
//
//  Created by Brandon Brown on 7/7/13.
//  Copyright (c) 2013 Brandon Brown. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
- (IBAction)buttonPressed:(id)sender;
@property (strong, nonatomic) IBOutlet UILabel *predictionLabel;

@end

这是我在控制台中的错误

2013-07-07 17:20:09.717 Prediction[14457:f803] -[UIView setText:]: unrecognized selector sent to instance 0x6a44690
2013-07-07 17:20:09.719 Prediction[14457:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView setText:]: unrecognized selector sent to instance 0x6a44690'
*** First throw call stack:
(0x14b0022 0xeb0cd6 0x14b1cbd 0x1416ed0 0x1416cb2 0x22cf 0x14b1e99 0x1614e 0x160e6 0xbcade 0xbcfa7 0xbc266 0x3b3c0 0x3b5e6 0x21dc4 0x15634 0x139aef5 0x1484195 0x13e8ff2 0x13e78da 0x13e6d84 0x13e6c9b 0x13997d8 0x139988a 0x13626 0x1f0d 0x1e75)
terminate called throwing an exception

1 个答案:

答案 0 :(得分:1)

您的错误是说setText方法已发送到UIView,而不是UILabel。因此,您似乎可能意外地将IBOutletUIView相关联,而不是UILabel(如果您手动编写IBOutlet,这很容易做到线)。因此,进入Interface Builder,删除&#34; outlet inspector&#34;中的IBOutlet链接。 ({1}}(可能在主视图上)的最后一个标签(右侧的最后一个标签),并链接UIView标签。

顺便说一句,如果你不想手动将IBOutlet代码,而是 control -drag从标签写入到.h文件中,你可以避免这种混淆。助理编辑器(和Interface Builder将为您编写IBOutlet代码行,如果您不小心抓住了错误的控件,它将很容易识别。请参阅 Xcode用户指南中的Make Connections Directly Between User Interface Objects and Your Source Files