0

我的 Firstviewcontroller 类中有一个UILabel *employeeNumLabel。现在我想从我的 secondviewcontroller 类的方法中更改该标签的文本。我已将以下代码放入 Secondviewcontroller 类中:

-(IBAction)save:(id)sender{
 number ++;

 NSString * numberString = [NSString stringWithFormat:@"%d",number];

 [employeeNumLabel setText:numberString]; 
}

但是显示错误:use of undeclared identifier employeeNumLabel。虽然我已经在我的 Secondviewcontroller.m 类中导入了 Firstviewcontroller.h。

4

4 回答 4

1

在 Firstviewcontroller 的 @implementaion 下方声明 UILabel *employeeNumLabel。

@implementation Firstviewcontroller
 UILabel *employeeNumLabel;

然后在 secondviewcontroller 中将其外部化

 @implementation secondviewcontroller
 extern UILabel *employeeNumLabel;

之后使用您的代码。

于 2012-07-05T07:21:55.353 回答
0

在您的标题中定义标签

@property (nonatomic, retain) UILable * employeeNumLabel;

在 .m 文件中合成它

@synthesize employeeNumLabel

在您的 init 函数中为标签分配内存并将其添加到您的视图中。或者,您可以创建一个 IBOutlet 并将其链接到此标签。

于 2012-07-05T07:22:14.137 回答
0

要更改标签文本,您需要执行以下操作..

1)您必须在第一个视图控制器中创建标签的属性...

2)您需要在第二个视图控制器中共享第一个视图控制器的引用,就像您可以访问标签并为同一对象设置文本一样。

或..再做一件事..

1) 取一个 NSString *lblString ;在您的 appdelegate 类中并使其属性..

2)您知道您可以使用以下代码在整个应用程序中获取 appdelegate 的共享引用

[[UIApplication sharedApplication] delegate]; //this will give your appdelegate Object..

3)然后在你的 secondViewController 类方法中......

Your_Appdelegate *appdelegate = (Appdelegate *)[[UIApplication sharedApplication] delegate];
appdelgate.lblString = @"YOUR_LABEL_TEXT";

4)然后在你的FirstViewController中..以同样的方式设置lableText......

 Your_Appdelegate *appdelegate = (Appdelegate *)[[UIApplication sharedApplication] delegate];
yourLabel.text = appdelgate.lblString;

最初你想要在你的标签上设置什么文本在你的 appdelegate..

愿这能帮助你...

于 2012-07-05T07:26:44.123 回答
0

在 Firstviewcontroller.h 中将 employeeNumLabel 声明为 extern,

  @interface Firstviewcontroller : UIViewController{}
      extern  UILabel *employeeNumLabel;

然后在 Firstviewcontroller.m (声明全局变量的@place)中添加以下内容,

UILabel *employeeNumLabel; 

然后使用您的代码在 secondviewcontroller.h 中导入 Firstviewcontroller.h。

于 2012-07-05T07:39:09.610 回答