2017-07-24 146 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