xcode iOS:在 ViewController 类型的对象上找不到属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22882345/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 04:51:11  来源:igfitidea点击:

iOS: Property not found on object of type ViewController

iosobjective-cxcode

提问by user3483394

so I'm trying to set a float value into another view controller, NewLinkViewController.

所以我试图将一个浮点值设置到另一个视图控制器 NewLinkViewController 中。

Here is my code:

这是我的代码:

    - (IBAction)selectSize:(id)sender {
UIButton *selectSize = (UIButton *) sender;
sizeChosen = [[[selectSize titleLabel] text] floatValue];
[self dismissViewControllerAnimated:YES completion:^{
    [mainController setMySizeLabel:sizeChosen];
    [NewLinkViewController.sizeChosen = sizeChosen];

}];

}

}

I'm getting this error: http://puu.sh/7WSA9.png(says "Property 'sizeChosen' not found on object of type 'NewLinkViewController'")

我收到此错误:http://puu.sh/7WSA9.png(说“在'NewLinkViewController'类型的对象上找不到属性'sizeChosen'”)

I clearly have the @property in my NewLinkViewController.h like this:

我的 NewLinkViewController.h 中显然有@property,如下所示:

    @property float sizeChosen;

What am I doing wrong?

我究竟做错了什么?

回答by rounak

It looks like you're setting a property on the class and not the object of the class [NewLinkViewController.sizeChosen = sizeChosen];

看起来您是在类上设置属性而不是类的对象 [NewLinkViewController.sizeChosen = sizeChosen];

More over, if you use the dot syntax, you don't need the square brackets.

此外,如果您使用点语法,则不需要方括号。

回答by Hussain Shabbir

You need to access the float variable property with instance of class NewLinkViewController and also as rounak suggested no need to use square brackets when accessing the properties method. Use like this below:-

您需要使用类 NewLinkViewController 的实例访问浮点变量属性,并且 rounak 建议在访问属性方法时无需使用方括号。像下面这样使用:-

  NewLinkViewController *newLinkVw=[[NewLinkViewController alloc] initWithNibName:@"yourVwNibnm" bundle:nil];
 //now access your property
   newLinkVw.sizeChosen=sizeChosen;

Note:- Make sure before setting or accessing properties of class. Your class instance should exist.

注意:- 在设置或访问类的属性之前确保。您的类实例应该存在。

回答by Erhan

You can try this

你可以试试这个

- (IBAction)selectSize:(id)sender {
UIButton *selectSize = (UIButton *) sender;
sizeChosen = [[[selectSize titleLabel] text] floatValue];
[self dismissViewControllerAnimated:YES completion:^{
    [mainController setMySizeLabel:sizeChosen];
    NewLinkViewController *newLinkController = [[NewLinkViewController alloc] init];
    newLinkController.sizeChosen = sizeChosen;

}];

回答by Rajesh

Declare your property like

声明您的财产,例如

@property (nonatomic, assign) float sizeChosen;

and set your sizeChosen property like below

并设置您的 sizeChosen 属性,如下所示

  - (IBAction)selectSize:(id)sender {
      UIButton *selectSize = (UIButton *) sender;
      sizeChosen1 = [[[selectSize titleLabel] text] floatValue];
      [self dismissViewControllerAnimated:YES completion:^{
        [mainController setMySizeLabel:sizeChosen1];
        newLinkViewController.sizeChosen = sizeChosen1;//newLinkViewController should be the instance of NewLinkViewController 

     }];