Iphone 设置来自另一个类的标签文本
我的Firstviewcontroller类中有一个Iphone 设置来自另一个类的标签文本,iphone,ios,Iphone,Ios,我的Firstviewcontroller类中有一个UILabel*employeeNumLabel。 现在,我想从我的secondviewcontroller类的方法中更改该标签的文本。 我已将以下代码放入我的Secondviewcontroller类中: -(IBAction)save:(id)sender{ number ++; NSString * numberString = [NSString stringWithFormat:@"%d",number]; [employe
UILabel*employeeNumLabel
。
现在,我想从我的secondviewcontroller类的方法中更改该标签的文本。
我已将以下代码放入我的Secondviewcontroller类中:
-(IBAction)save:(id)sender{
number ++;
NSString * numberString = [NSString stringWithFormat:@"%d",number];
[employeeNumLabel setText:numberString];
}
但是显示了一个错误:使用未声明的标识符employeeNumLabel
。
尽管我已经在Secondviewcontroller.m类中导入了Firstviewcontroller.h。在Firstviewcontroller的@Implementation下声明UILabel*employeeNumLabel
@implementation Firstviewcontroller
UILabel *employeeNumLabel;
然后在secondviewcontroller中像
@implementation secondviewcontroller
extern UILabel *employeeNumLabel;
然后使用代码。在标题中定义标签
@property (nonatomic, retain) UILable * employeeNumLabel;
在.m文件中合成它
@synthesize employeeNumLabel
在init函数中,为标签分配内存并将其添加到视图中。
或者,您可以创建IBOutlet并将其链接到此标签。要更改标签文本,您需要执行以下操作 1) 您必须在第一视图控制器中设置标签的属性 2) 您需要在第二个视图控制器中共享第一个视图控制器的引用,就像您可以访问标签和设置同一对象的文本一样 或者。。再做一件事 1) 拿一个NSString*lblString;在appdelegate类中,并使其属性 2) 您知道,通过使用以下代码,可以在整个应用程序中获得appdelegate的共享引用
[[UIApplication sharedApplication] delegate]; //this will give your appdelegate Object..
3) 然后在第二个ViewController类方法中
Your_Appdelegate *appdelegate = (Appdelegate *)[[UIApplication sharedApplication] delegate];
appdelgate.lblString = @"YOUR_LABEL_TEXT";
4) 然后在第一个ViewController中。。以相同的方式设置LabletText
Your_Appdelegate *appdelegate = (Appdelegate *)[[UIApplication sharedApplication] delegate];
yourLabel.text = appdelgate.lblString;
最初,标签上需要的文本在appdelegate中设置
这将有助于您在Firstviewcontroller.h中将employeeNumLabel声明为extern
@interface Firstviewcontroller : UIViewController{}
extern UILabel *employeeNumLabel;
然后在Firstviewcontroller.m(@声明全局变量的位置)中添加以下内容
然后在secondviewcontroller.h中导入Firstviewcontroller.h,然后使用代码..在secondviewcontroller中装箱Firstviewcontroller的对象,并在Firstviewcontroller中创建employeeNumLabel的@property。在这样的访问之后-[firstViewControllerObject.employeeNumLabel setText:numberString];接受对你有帮助的答案。
UILabel *employeeNumLabel;