如何訪問變量陣列或從另一個類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'?而不是本地? –