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

Osa*_*ahM 0 xcode objective-c

如何从另一个类访问变量或数组

我想dName在class2中使用价值

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;
}
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

class2.h

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

    @property(nonatomic, strong) NSString * imageName;
   @end
Run Code Online (Sandbox Code Playgroud)

更新(解决方案)由Balasubramanian

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];
}
Run Code Online (Sandbox Code Playgroud)

Bal*_*ian 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;
}
Run Code Online (Sandbox Code Playgroud)

要么

您可以使用参数在类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];
Run Code Online (Sandbox Code Playgroud)

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

[cell cellProperties:dName]
Run Code Online (Sandbox Code Playgroud)