【问题标题】:Property 'text' not found on object of type 'UIView'在“UIView”类型的对象上找不到属性“文本”
【发布时间】:2016-10-24 01:44:34
【问题描述】:

我想从 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

【问题讨论】:

  • 什么是lbl?你能出示lbl的声明吗?
  • 看起来您已将lbl 声明为UIView
  • lbl 只是 secondViewController @Ronak Chaniyara 中标签的名称
  • 显示您声明它的 secondViewController 的 .h 文件的代码。
  • 我写了所有有问题的代码,我写了'lbl',当我写'lbl'时。我看不到“文本”,我认为文本必须在“lbl”之后。

标签: objective-c


【解决方案1】:

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

因为UIView 没有文本属性,而您正试图在UIView 上设置文本。

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

编辑:

替换成secondVC.h

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

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

【讨论】:

    猜你喜欢
    • 2020-07-31
    • 2013-05-31
    • 2014-04-27
    • 2012-03-04
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 2019-06-25
    • 2021-06-30
    相关资源
    最近更新 更多