0

我制作了自己的自定义照片库控件,用户可以在其中点击照片以获取更大的版本,或者您想要的任何其他操作。

我希望这样做,以便持有这个画廊的 UIViewController 包含选择器,例如

- (IBAction)photoThumbnailClicked(id)sender
{
    // Enlarge the photo here
}

我想以某种方式在照片库对象中保留视图控制器和选择器的引用,因此当生成每个照片缩略图按钮时,可以像这样设置目标:

[thumbnail addTarget:self.target
                   action:self.action
         forControlEvents:UIControlEventTouchUpInside];

我目前正在保留这样的参考资料,尽管我不确定这是最好的方法,因为我有理由相信可能存在内存问题:

照片画廊.h:

@interface PhotoGallery : UIScrollView

@property UIViewController *target;
@property SEL action;

- (void)setTarget:(UIViewController*)viewController
     withAction:(SEL)action;
@end

PhotoGallery.m 中的 setTarget 方法:

- (void)setTarget:(UIViewController*)target withAction:(SEL)action
{
    self.target = target;
    self.action = action;
}

这是最好的方法吗?如果没有,我应该怎么做?

提前感谢您的帮助!

4

1 回答 1

1

我假设ARC

如果您的视图层次结构如下所示:

someViewControllerOrView   <------------|
       -                                |
  <any number of views>                 |
            -                           |
        photoGalleryInstance            |
                             -target ----

那么你可能希望你target成为一个弱参考,否则你可能会遇到一个“保留周期”。那就是someViewController坚持photoGalleryInstancephotoGalleryInstance坚持,someViewController也永远不会被释放。

@property (weak) UIViewController *target;
//or @property (unsafe_unretained) UIViewController *target; if target is < 5.0

否则,您的代码看起来像是实现此模式的标准方式。

于 2013-01-29T19:07:54.930 回答