Objective C – IOS – 在访问对象variables时,在types对象上找不到属性

我正在构build一个UIView,它连接到一个包含商店项目列表的UITableView,并由一个与对象类(shopObjects)关联的数据数组填充。

这里是我的商店对象H文件 –

#import <Foundation/Foundation.h> @interface shopObjects : NSObject @property(strong) NSString *shopITitle; @property(strong) NSString *shopIGroup; @property(strong) NSString *shopIDesc; @property(strong) NSString *shopIPrice; @property Boolean offer; -(id)initWithshopITitle:(NSString *)shopITitleD shopIGroup:(NSString *)shopIGroupD shopIDesc: (NSString *)shopIDescD shopIPrice:(NSString *)shopIPriceD offer:(Boolean )offerD; @end 

商店对象。 M文件

 #import "shopObjects.h" @implementation shopObjects -(id)initWithshopITitle:(NSString *)shopITitleD shopIGroup:(NSString *)shopIGroupD shopIDesc:(NSString *)shopIDescD shopIPrice:(NSString *)shopIPriceD offer:(Boolean )offerD{ self= [super init]; if(self){ self.shopITitle = shopITitleD; self.shopIGroup = shopIGroupD; self.shopIDesc = shopIDescD; self.shopIPrice = shopIPriceD; self.offer = (offerD); } return self; } @end 

这是我的视图控制器.h文件 –

 #import <UIKit/UIKit.h> #import "shopObjects.h" @interface shopVCSelectionViewController : UIViewController @property (weak, nonatomic) IBOutlet UIScrollView *shopResScroller; @property (weak, nonatomic) IBOutlet UILabel *ShopItemTitle; @property (weak, nonatomic) IBOutlet UITextView *ShopItemDesc; @property (weak, nonatomic) IBOutlet UILabel *ShopItemPrice; @end 

和VC .m文件 –

 #import "shopVCViewController.h" @interface shopVCViewController () @end @implementation shopVCViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. //Set Scroller [self.shopScroll setScrollEnabled:YES]; [self.shopScroll setContentSize:CGSizeMake(320, 1100)]; self.title = @"Shop"; self.shopArray = [NSArray arrayWithObjects:@"1 x PT session - £25",@"3 for 2 PT sessions - £50 ",@"1 x Running Club - £15",@"1 x PT session", nil]; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [self.shopArray count]; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier =@"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; cell.textLabel.text=[self.shopArray objectAtIndex:indexPath.row]; return cell; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end 

我试图链接标签/文本字段与相关的对象variables – 但shopObjects对象/相关的variables不出现在预测文本,我得到以下属性未find错误 – 我不知道为什么! 任何人都可以提供一些build议吗?

在这里输入图像说明

您没有inheritanceshopObjects中的shopObjects。 它是一个单独的对象。您需要创build一个types为shopObjects的variables,设置其属性,然后调用它。 例如

 shopVcSelectionViewController.h @property (strong, nonatomic) shopObjects *shopObject; shopVcSelectionViewController.m ... self.shopItemTitle.text = self.shopObject.shopTitle ... 

尝试@合成ShopObjects的.m文件中的variables。

您发布的代码位于类shopSelectionViewController的实例方法中。 shopSelectionViewController有一个属性shopItemTitle,但不是shopITitle属性。

这条线

 self.shopItemTitle.text = self.shopITitle; 

没有意义。

如果你正试图从类“shopObjects”的对象中获取标题,那么你需要这样的东西:

 shopObjects *someShopObject = //code to fetch a shopObject self.shopItemTitle.text = someShopObject.shopITitle; 

如果你有一个与类相同的对象实例,你会看到这个错误,你错误地操作类,而不是你的实例(谢谢,Xcode自动完成)。

例如,假设您有一个视图控制器SetSignsViewController并且您创build了一个名为setSignsViewController的视图控制器实例:

 SetSignsViewController *setSignsViewController = (SetSignsViewController *) uiNavigationController.topViewController; 

你想设置一个名为openHouseLocation的属性:

 SetSignsViewController.openHouseLocation = self.openHouseLocation; 

这会产生在Property 'openHouseLocation' not found on object of type 'SetSignsViewController'因为你在SetSignsViewController而不是setSignsViewController 。 你真正打算做的是:

 setSignsViewController.openHouseLocation = self.openHouseLocation;