objective-c:如何从另一个类访问变量或数组?

时间:2017-07-24 13:07:39

标签: objective-c xcode

如何从其他类访问变量或数组

我想在class2中使用dName

第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

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

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

UPDATE(解决方案)作者:Balasubramanian

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];
}

2 个答案:

答案 0 :(得分:5)

您可以尝试导入Class 2中的Class 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;
}

您可以使用参数在类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];

在Class 1中,在cellForItemAtIndexPath方法中调用Method,

[cell cellProperties:dName]

答案 1 :(得分:0)

在这种情况下,可能在第1类中创建UIImage会更好,直到你注意到冻结。

但是作为一般规则,你不能从类2中的类1访问该属性,只需将它从1分配给2.所以你需要在类2的标题中创建一个属性。