在'UIView'类型的对象上找不到属性'text'

时间:2016-06-22 08:29:13

标签: objective-c

我想从viewController发送一些东西给另一个使用NSNotification的viewController。

我在第一个vc中定义了按钮,我在第二个vc中定义了标签。当我点击按钮时,我想看标签上的点击次数但是有问题。我在'UIView'类型的对象上找不到'属性'文本'错误。

我该如何解决这个问题?

--- --- firstVC.h

import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController

- (IBAction)btnTap:(id)sender;

@end

--- --- firstVC.m

import "FirstViewController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController

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

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

- (IBAction)btnTap:(id)sender {

    [[NSNotificationCenter defaultCenter] postNotificationName:@"not" object:nil];
}
@end

--- --- secondVC.h

import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController

@property (strong, nonatomic) IBOutlet UIView *lbl;
@property int number;

@end

--- --- secondVC.m

import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController
@synthesize number,lbl;

- (void)viewDidLoad {
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationMethod:) name:@"not" object:nil];
    number=0;
}

-(void) notificationMethod :(NSNotificationCenter *) notification{

    number++;
    lbl.text = [NSString stringWithFormat: @"%d",number];
}

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

@end

1 个答案:

答案 0 :(得分:4)

只有当您尝试在UIView上设置文字时才会出现错误。

由于UIView没有文字属性,而您尝试在UIView上设置文字。

因此,您可能已将lbl声明为UIView,将其更改为UILabel并在此之后进行检查。

<强> 编辑:

替换secondVC.h

@property (strong, nonatomic) IBOutlet UIView *lbl;

使用

@property (strong, nonatomic) IBOutlet UILabel *lbl;