2013-10-18 28 views
6

我构建了一个UIView,它连接到一个包含商店项目列表的UITableView,并由与一个关联的数据数组对象类(shopObjects)。目标C - IOS - 在类型对象上找不到属性 - 访问对象变量

这里是我的店里对象.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 [email protected]"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 

我试图用连接起来,标签/文本字段相关的对象变量 - 但shopObjects对象/相关变量不会出现在预测文本中,并且我得到以下属性未找到错误 - 我不知道为什么!任何人都可以提供一些建议吗?

enter image description here

+0

属性是否连接到Interfacebuilder中的UI对象?顺便说一下,Objective-C中的Apple命名约定是以大写字母开头的类。 – zaph

+1

请复制/粘贴(相关)代码而不是截图。这使得其他人更容易在代码中搜索潜在的问题。 –

+0

我看不到在您的*视图控制器*中的属性'shopITitle' ... –

回答

5

您没有继承您的viewController中的shopObjects。它是一个独立的对象。您需要创建一个类型为shopObjects的变量,设置其属性,然后调用它。例如

shopVcSelectionViewController.h 

@property (strong, nonatomic) shopObjects *shopObject; 




shopVcSelectionViewController.m 

... 
self.shopItemTitle.text = self.shopObject.shopTitle 
... 
0

尝试@synthesize在ShopObjects的.m文件中的变量。

+5

由于Xcode 4.4(LLVM编译器4.0),如果需要,属性会由编译器自动合成。 –

1

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

此行

self.shopItemTitle.text = self.shopITitle; 

没有道理。

如果你想获取从类“shopObjects”对象的标题,那么你就需要这样的事:

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

您还可以看到这个错误,如果你有一个对象实例,该实例和一个类是同一个名字,你错误地操作类,而不是你的实例(谢谢,Xcode自动完成)。

例如,假设你有一个视图控制器SetSignsViewController并创建一个名为setSignsViewController视图控制器的实例:

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

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

​​

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

setSignsViewController.openHouseLocation = self.openHouseLocation; 
相关问题