OsamahM
OsamahM

Reputation: 347

objective-c : How can I access variables or array from another class?

How can I access variables or array from another class

i wanna use dName value in class2

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

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

    // 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 (solution) By 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];
}

Upvotes: 0

Views: 136

Answers (2)

Balasubramanian
Balasubramanian

Reputation: 5510

You could try to import the Class 2 in Class 1. Access the Class 2 properties and assign the value.

- (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

You can define method custom collectionViewCell in class 2 with parameter and assign to properties.

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

In Class 1, call Method in cellForItemAtIndexPath Method,

[cell cellProperties:dName]

Upvotes: 5

Cristi Băluță
Cristi Băluță

Reputation: 1305

In this case probably creating the UIImage in class 1 is better, till you'll notice the freezes.

But as a general rule, you don't access the property from class 1 in class 2, you simply assign it from 1 to 2. So you need to create a property in the header of class 2.

Upvotes: 0

Related Questions