【问题标题】:Dynamic object creation and release动态对象创建和释放
【发布时间】:2009-09-04 16:53:47
【问题描述】:

我有一个实例化其他类的类。它正在努力控制它们的生命周期,或者应该这样做;) 一个根类管理包含事件并且是根视图的子视图的其他类。

每个视图都涉及很多图形,需要在加载下一个视图之前清除它们。

知道如何从“触摸结束”方法卸载当前子视图以及如何加载下一个子视图,同时保持对它的命名引用以用于事件处理?

谢谢 // :)

【问题讨论】:

  • 您想无缝交换子视图吗?过渡?滚动?
  • 我不明白为什么您需要继续从正在卸载的子视图接收事件? “下一个视图”的加载是否花费了用户想要取消的时间?您是否正在创建类似天气或股票应用程序的东西,您希望在其中加载当前可见屏幕 + 左右两个潜在的下一个屏幕。但不是其他潜在的 20 页?
  • 无缝交换子视图似乎最接近我想要做的。

标签: iphone objective-c memory-management


【解决方案1】:

假设您想转储旧子视图并持有新子视图的句柄,这实际上很容易。你想要这样的东西:

@interface YourView : UIView
{
    // Create an ivar in your class
    UIView *_subview;
}

// Propertize it as retain to take care of most of the heavy lifting
@property(nonatomic, retain) UIView *subview;

@end


@implementation YourView

// Map the ivar to the property
@synthesize subview = _subview;

// Call this to put in a new subview
-(void) switchToNewSubview:(UIView*)newSubview
{
    // Remove the old subview, set the new one, and if the new one isn't nil
    // add it as a subview
    [self.subview removeFromSuperview];
    self.subview = newSubview;
    if(newSubview)
        [self addSubview:self.subview];
}

// Don't forget to nil out the subview on dealloc to release it
-(void) dealloc
{
    self.subview = nil;
    [super dealloc];
}

@end

【讨论】:

    猜你喜欢
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 2011-05-15
    • 1970-01-01
    • 2013-09-14
    • 2011-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多