xcode “在类方法中访问的实例变量'xxx'......为什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13223847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Instance variable 'xxx' accessed in class method....why?
提问by Michael Levy
So please forgive me as I am very new to stackoverflow and to ios programming in general.
所以请原谅我,因为我对 stackoverflow 和一般的 ios 编程非常陌生。
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
;
我正在尝试更改我拥有的按钮的文本,用dateStringForInput
我从另一个类传入的日期字符串 ( )替换当前文本。该按钮被调用myTodayButton
并在我的.h
文件中为我的班级声明inputMilesViewController
......
@property (strong, nonatomic) IBOutlet UIButton *myTodayButton
;
Below is the code from my .m.
下面是我的 .m 中的代码。
+(void) changeButtonText:(NSString*) dateStringForInput{
NSLog(@"we got to changebuttontext");
[_myTodayButton setTitle:dateStringForInput forState:UIControlStateNormal];
}
I am getting the following error "Instance variable "_myTodayButton
" accessed in class method.
我收到以下错误“实例变量” _myTodayButton
“在类方法中访问。
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
.
我知道这是用类方法而不是实例方法编写的,但我不知道如何实现这种更改。提示此调用的按钮changeButtonText
位于正在创建dateStringForInput
.
回答by Rajesh Loganathan
Try like this...
试试这样...
For (+) instance.
对于 (+) 实例。
+ (void) changeButtonText
: Only have access to the self object.
+ (void) changeButtonText
: 只能访问 self 对象。
--Step 1:Remove follwing line from headerfile
-步骤1:删除从follwing线头文件
@property (strong, nonatomic) IBOutlet UIButton *myTodayButton;
--Step 2:
--第 2 步:
- Add
UIButton *myTodayButton
in your class file globally.. - That means declare before
@implementation
UIButton *myTodayButton
全局添加您的类文件..- 这意味着先声明
@implementation
Eg: In .m File
例如:在.m 文件中
#import "Controller.h"
UIButton *myTodayButton // Add like this before @implementation
@implementation Controller
For (-) instance
例如
- (void) changeButtonText
: Only have access to the instance methods
- (void) changeButtonText
: 只能访问实例方法
回答by bennythemink
change method to an instance method instead of a class method ("-" instead of "+") create an instance of the class and then simply call it like so:
将方法更改为实例方法而不是类方法(“-”而不是“+”)创建类的实例,然后像这样简单地调用它:
MyClass *classInstance = [[MyClass alloc] init];
[classInstance changeButtonText:@"stringText"];
Also its recommended to have weak properties for IBOutlets.
还建议为 IBOutlets 设置弱属性。
@property (nonatomic, weak) IBOutlet UIButton *myButton;
[EDIT]
[编辑]
Regarding your second issue with segues. Firstly do you really need a different view controller for simply displaying a UIDatePicker? I normally display them in the same UIViewController using an actionSheet, its a little work to get just right but solves this problem. I can post the code if you like?
关于你的第二个问题与segue。首先你真的需要一个不同的视图控制器来简单地显示一个 UIDatePicker 吗?我通常使用 actionSheet 在同一个 UIViewController 中显示它们,它的一些工作使它恰到好处但解决了这个问题。如果你愿意,我可以发布代码吗?
Anyway if you want to keep your current setup I would do the following:
无论如何,如果您想保留当前设置,我会执行以下操作:
In the ViewController that contains the button create a new public NSString property in the .h file.
@property (nonatomic, strong) NSString *dateString;
In the View Controller that contains the date picker, after the user has selected the date override the perpareForSegue method. In here get the destination view controller (which is the one with the button and new string property created in point one above) and set the dateString.
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { MyViewController *todayViewController = (MyViewController*)[segue destinationViewController]; [todayViewController setDateString:@"valueYouRequire"];
}
In the ViewController with the button set the button's title in the viewDidLoad to be the dateString.
- (void)viewDidLoad { [super viewDidLoad];
if (self.dateString) [self.myTodayButton setTitle:self.dateString forState:UIControlStateNormal]; }
在包含按钮的 ViewController 中,在 .h 文件中创建一个新的公共 NSString 属性。
@property (nonatomic, strong) NSString *dateString;
在包含日期选择器的视图控制器中,在用户选择日期后覆盖 perpareForSegue 方法。在此处获取目标视图控制器(即上面第一点中创建的带有按钮和新字符串属性的控制器)并设置 dateString。
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { MyViewController *todayViewController = (MyViewController*)[segue destinationViewController]; [todayViewController setDateString:@"valueYouRequire"];
}
在带有按钮的 ViewController 中,将 viewDidLoad 中按钮的标题设置为 dateString。
- (void)viewDidLoad { [super viewDidLoad];
if (self.dateString) [self.myTodayButton setTitle:self.dateString forState:UIControlStateNormal]; }
[EDIT]
[编辑]
If you want to go down the actionSheet route then add this method to the ViewController with the button and play around with the frame sizes to fit your needs. The method below creates a date picker, adds it to an actionSheet and displays the actionSheet.
如果你想沿着 actionSheet 路由,然后使用按钮将此方法添加到 ViewController 并调整框架大小以满足你的需要。下面的方法创建一个日期选择器,将其添加到 actionSheet 并显示 actionSheet。
- (IBAction) datePressed:(id)sender {
if (!_actionSheet) {
_actionSheetFrame = CGRectMake(0, self.view.frame.size.height - 373, 320, 403);
_actionSheetBtnFrameOk = CGRectMake(20, 330, 280, 50);
_actionSheetBtnFreameCancel = CGRectMake(20, 275, 280, 50);
_pickerFrame = CGRectMake(0, 50, 320, 216);
_actionSheet = [[UIActionSheet alloc] initWithTitle:@"Select Date" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"OK", nil];
_datePicker = [[UIDatePicker alloc] init];
[_datePicker setDatePickerMode:UIDatePickerModeDate];
[_actionSheet addSubview:_datePicker];
}
[_datePicker setMaximumDate:[NSDate date]];
[_actionSheet showInView:self.view.superview];
[[[_actionSheet subviews] objectAtIndex:kActionBtnIndexOk] setFrame:_actionSheetBtnFrameOk];
[[[_actionSheet subviews] objectAtIndex:kActionBtnIndexCancel] setFrame:_actionSheetBtnFreameCancel];
[_actionSheet setFrame:_actionSheetFrame];
[_datePicker setFrame:_pickerFrame];
}
}
You will also have to implement the UIActionSheetDelegate method to set the button's text once the user has selected his desired date.
一旦用户选择了他想要的日期,您还必须实现 UIActionSheetDelegate 方法来设置按钮的文本。
#pragma mark - UIActionSheet Delegate Methods
- (void) actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
[self.myTodayButton setTitle:self.dateStringFromSelectedDate forState:UIControlStateNormal];
}
}
回答by Ian L
Class methods do not have access to instance variables, that's why you're getting the error you're seeing.
类方法无权访问实例变量,这就是您收到所看到错误的原因。
You need to change the method to an instance method:
您需要将方法更改为实例方法:
- (void)changeButtonText:(NSString *)dateStringForInput;
Then you will need to pass or obtain a reference to the instanceof the class containing the method to the class calling the method.
然后,您需要向调用该方法的类传递或获取对包含该方法的类的实例的引用。
I hope that makes sense... Add a comment if you need any clarification!
我希望这是有道理的...如果您需要任何说明,请添加评论!