2017-07-24 140 views
0

如何訪問變量陣列或從另一個類objective-c:如何從另一個類訪問變量或數組?

我想在等級2

1類

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 
    class1 *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath]; 

    // Configure the cell 

    NSString *dName = [self.menuNames objectAtIndex:indexPath.item]; 

    cell.titleLabel.text = dName; 
     [cell cellProperties:dName] 

    return cell; 
} 

類2

{ 
self = [super initWithFrame:frame]; 
if (self) { 
    // Title (image name) 
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, frame.size.width, frame.size.height)]; 
    self.titleLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; 
    self.titleLabel.textAlignment = NSTextAlignmentCenter; 
    self.titleLabel.font = [UIFont boldSystemFontOfSize:10.0]; 
    self.titleLabel.backgroundColor = [UIColor clearColor]; 
    self.titleLabel.textColor = [UIColor whiteColor]; 
    [self.contentView addSubview:self.titleLabel]; 

    // Image 
    UIImage *image = [UIImage imageNamed:dName]; 
    self.imageView1 = [[UIImageView alloc]initWithImage:image]; 
    [self.contentView addSubview:self.imageView1]; 

    // itemTitle.text = itemTitle; 



} 
return self; 
} 
-(void)cellProperties:(NSString *)imageName { 
self.imageName = imageName; 
} 

class2.h

使用 dName
@interface class2 : UICollectionViewCell 
    @property (strong, nonatomic) UILabel *titleLabel; 
    @property (strong, nonatomic) UIImageView *imageView1; 

    @property(nonatomic, strong) NSString * imageName; 
    @end 

UPDATE(解決方案)通過巴拉蘇布蘭馬尼安

CLASS 2

{ 
self = [super initWithFrame:frame]; 
if (self) { 
    // Title (image name) 
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, frame.size.width, frame.size.height)]; 
    self.titleLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; 
    self.titleLabel.textAlignment = NSTextAlignmentCenter; 
    self.titleLabel.font = [UIFont boldSystemFontOfSize:10.0]; 
    self.titleLabel.backgroundColor = [UIColor clearColor]; 
    self.titleLabel.textColor = [UIColor whiteColor]; 
    [self.contentView addSubview:self.titleLabel]; 



    // itemTitle.text = itemTitle; 



} 
return self; 
} 
-(void)cellProperties:(NSString *)imageName { 
self.imageName = imageName; 
NSLog(@"Image Name2: %@",imageName); 
// Image 
    UIImage *image = [UIImage imageNamed:imageName]; 
    NSLog(@"Image Name1: %@",_imageName); 
    self.imageView1 = [[UIImageView alloc]initWithImage:image]; 
    [self.contentView addSubview:self.imageView1]; 
} 
+0

你有沒有嘗試過在全球範圍內創建'NSString'?而不是本地? –

回答

5

你可以嘗試導入Class 2Class 1。訪問Class 2屬性並分配值。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 
class1 *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath]; 

    // Configure the cell 
    NSString *dName = [self.menuNames objectAtIndex:indexPath.item]; 
    cell.image= [UIImage imageNamed:dName]; 

    return cell; 
} 

OR

可以在類2參數定義方法定製collectionViewCell並分配給屬性。

In Class 2 
// Declare in .h file 
@property(nonatomic, strong) NSString * imageName; 

// Declare in .m file 
-(void)cellProperties:(NSString *)imageName { 
    self.imageName = imageName 
} 
//At this line: 
UIImage *image = [UIImage imageNamed:self.imageName]; 

第1類,調用方法cellForItemAtIndexPath方法,

[cell cellProperties:dName] 
+0

@OsamahM你設法讓它工作? – Balasubramanian

+0

在class1中不存在此錯誤( 'class2'沒有可見的@interface聲明選擇器'cellProperties:') – OsamahM

+0

Plz更新您的代碼。讓我檢查 – Balasubramanian

0

在這種情況下,很可能在1類創建UIImage的比較好,直到你會發現凍結。

但作爲一般規則,你不從1類2類訪問屬性,只需1〜2所以,你需要在類的頭,以創建一個屬性分配給它2