如何访问变量阵列或从另一个类objective-c:如何从另一个类访问变量或数组?
我想在等级2
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
使用dName
值
@interface class2 : UICollectionViewCell
@property (strong, nonatomic) UILabel *titleLabel;
@property (strong, nonatomic) UIImageView *imageView1;
@property(nonatomic, strong) NSString * imageName;
@end
UPDATE(解决方案)通过巴拉苏布兰马尼安
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];
}
你有没有尝试过在全球范围内创建'NSString'?而不是本地? –