Ios Xcode-如何从另一个类调用方法
我的第一节课。h:Ios Xcode-如何从另一个类调用方法,ios,objective-c,Ios,Objective C,我的第一节课。h: @interface CollectionViewController : UICollectionViewController <..> @property (strong, nonatomic) IBOutlet UICollectionView *collectionView; -(void)refreshView; @end 我需要从第二个类调用此方法 我的第二节课。h @interface CollectionViewCell : UIC
@interface CollectionViewController : UICollectionViewController <..>
@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;
-(void)refreshView;
@end
我需要从第二个类调用此方法
我的第二节课。h
@interface CollectionViewCell : UICollectionViewCell
- (IBAction)buttonAction:(id)sender;
@end
我的第二节课。m
@implementation CollectionViewController
-(void)refreshView
{
[self.collectionView reloadData];
}
@end
@implementation CollectionViewCell
- (IBAction)myButtonAction:(id)sender {
//i need to call the method "refreshView" here
}
@end
我试图将方法refreshView
从private编辑为public,但它与“[self.collectionView reloadData];
不兼容”
谢谢您的帮助。让您的手机“了解”其收藏视图不是一个好主意 使用委派代替 CollectionViewCell.h
@协议集合ViewCellDelegate
-(无效)刷新按钮按forcell:(CollectionViewCell*)单元格;
@结束
@接口CollectionViewCell:UICollectionViewCell
@属性(非原子,弱)id委托;
@结束
CollectionViewController.m
@接口CollectionViewController:UICollectionViewController
@结束
@实现集合视图控制器
-(无效)刷新按钮按Forcell:(CollectionViewCell*)单元格{
[self.collectionView-reloadData];
}
@结束
不要忘记在每个单元格上设置delegate属性 为什么这样?您可以在
UICollectionViewController
内部创建UICollectionViewCell
的实例,并在选定的单元格上重新加载数据,您可以调用UICollectionViewDelegate
中的方法,因为这些方法只是标题。
@protocol CollectionViewCellDelegate <NSObject>
- (void)refreshButtonPressedForCell:(CollectionViewCell *)cell;
@end
@interface CollectionViewCell : UICollectionViewCell
@property (nonatomic, weak) id<CollectionViewCellDelegate> delegate;
@end
@interface CollectionViewController : UICollectionViewController <CollectionViewCellDelegate>
@end
@implementation CollectionViewController
- (void)refreshButtonPressedForCell:(CollectionViewCell *)cell {
[self.collectionView reloadData];
}
@end