0

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];
}
1
  • have you tried to create NSString globally? instead on local? Commented Jul 24, 2017 at 13:42

2 Answers 2

5

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]
Sign up to request clarification or add additional context in comments.

4 Comments

no still have this error in class1 ( No visible @interface for ‘class2’ declares the selector 'cellProperties:')
@OsamahM Please declare -(void)cellProperties:(NSString *)imageName;in class 2 .h file. It will work
thinks you it is almost done ,, please check updated my code NSlog result
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.