繁体   English   中英

Xcode-如何从另一个类调用方法

[英]Xcode - How to call method from another class

我的第一堂课.h:

@interface CollectionViewController : UICollectionViewController <..>

  @property (strong, nonatomic) IBOutlet UICollectionView *collectionView;

  -(void)refreshView;

@end

我的第一堂课.m

@implementation CollectionViewController

  -(void)refreshView
  {
      [self.collectionView reloadData];
  }

@end

我需要从第二堂课中调用此方法

我的第二堂课.h

@interface CollectionViewCell : UICollectionViewCell

   - (IBAction)buttonAction:(id)sender;

@end

我第二节课.m

@implementation CollectionViewCell

  - (IBAction)myButtonAction:(id)sender {

  //i need to call the method "refreshView" here

  }

@end

我试图将refreshView方法从私有编辑为公共,但与“ [self.collectionView reloadData]; ”不兼容[self.collectionView reloadData];

感谢帮助。

让您的单元“了解”其集合视图不是一个好主意。

改用委派...

CollectionViewCell.h

@protocol CollectionViewCellDelegate <NSObject>
- (void)refreshButtonPressedForCell:(CollectionViewCell *)cell;
@end

@interface CollectionViewCell : UICollectionViewCell
@property (nonatomic, weak) id<CollectionViewCellDelegate> delegate;
@end

CollectionViewController.m

@interface CollectionViewController : UICollectionViewController <CollectionViewCellDelegate>
@end

@implementation CollectionViewController

- (void)refreshButtonPressedForCell:(CollectionViewCell *)cell {
    [self.collectionView reloadData];
}

@end

不要忘记在每个单元上设置委托属性!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM