“在类方法中访问实例变量'xxx'.为什么? [英] "Instance variable 'xxx' accessed in class method....why?
问题描述
因此,请原谅我,因为我对stackoverflow和ios编程一般还很陌生.
So please forgive me as I am very new to stackoverflow and to ios programming in general.
我正在尝试更改我拥有的按钮的文本,用我从另一个类传入的日期字符串(dateStringForInput
)替换当前文本.该按钮称为myTodayButton
,并在我的.h
文件中为我的类inputMilesViewController
声明.
@property (strong, nonatomic) IBOutlet UIButton *myTodayButton
;
I am attempting to change the text of a button i have, replacing current text with a date string (dateStringForInput
) I am passing in from another class. The button is called myTodayButton
and is declared in my .h
file for my class inputMilesViewController
...
@property (strong, nonatomic) IBOutlet UIButton *myTodayButton
;
下面是我.m中的代码.
Below is the code from my .m.
+(void) changeButtonText:(NSString*) dateStringForInput{
NSLog(@"we got to changebuttontext");
[_myTodayButton setTitle:dateStringForInput forState:UIControlStateNormal];
}
在类方法中,我收到以下错误实例变量"_myTodayButton
"".
I am getting the following error "Instance variable "_myTodayButton
" accessed in class method.
我知道这是用类方法而不是实例方法编写的,但我不知道如何使此更改发生.提示此呼叫changeButtonText
的按钮在另一个正在创建dateStringForInput
的类中.
I understand that this is written in a class method rather than an instance method but i cant figure out how to get this change to happen. the button which prompts this call to changeButtonText
is in the other class which is creating dateStringForInput
.
推荐答案
像这样尝试...
用于(+)实例.
+ (void) changeButtonText
:仅具有访问自身对象的权限.
+ (void) changeButtonText
: Only have access to the self object.
-步骤1:从标题文件中删除以下行
@property (strong, nonatomic) IBOutlet UIButton *myTodayButton;
-步骤2:
- 在您的班级文件中全局添加
UIButton *myTodayButton
. - 这意味着在
@implementation
之前声明
- Add
UIButton *myTodayButton
in your class file globally.. - That means declare before
@implementation
例如:在 .m文件
#import "Controller.h"
UIButton *myTodayButton // Add like this before @implementation
@implementation Controller
对于(-)实例
- (void) changeButtonText
:仅具有访问实例方法的权限
- (void) changeButtonText
: Only have access to the instance methods
这篇关于“在类方法中访问实例变量'xxx'.为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!