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];
}
NSStringglobally? instead on local?