Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/kubernetes/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Release 圆弧-插入多个子视图并处理动作_Release_Scrollview_Automatic Ref Counting_Retain_Subviews - Fatal编程技术网

Release 圆弧-插入多个子视图并处理动作

Release 圆弧-插入多个子视图并处理动作,release,scrollview,automatic-ref-counting,retain,subviews,Release,Scrollview,Automatic Ref Counting,Retain,Subviews,我对ARC有一些问题。我正在尝试向ScrollView添加多个视图,然后如果用户点击一个视图,就会调用一个操作 但当用户点击视图时,我会收到这样一条消息:“消息发送到解除分配的实例” 我怎样才能保留这些观点 这是我在ViewController中的代码: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. int

我对ARC有一些问题。我正在尝试向ScrollView添加多个视图,然后如果用户点击一个视图,就会调用一个操作

但当用户点击视图时,我会收到这样一条消息:“消息发送到解除分配的实例”

我怎样才能保留这些观点

这是我在ViewController中的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    int i;
    for (i=0;i<10;i++) {
        ChannelViewController *channelView = [[ChannelViewController alloc] init];
        [channelView.view setFrame:CGRectMake(i*175, 0, 175, 175)];
        //channelsScrollView is a ScrollView
        [self.channelsScrollView addSubview:channelView.view];
    }
    [self.channelsScrollView setContentSize:CGSizeMake(i*175, 175)];
}
-(void)viewDidLoad
{
[超级视图下载];
//从nib加载视图后,执行任何其他设置。
int i;

对于(i=0;i您需要保留对ViewController中所有ChannelViewController实例的引用。在代码中,在每次for循环迭代后,ARC都会释放您的ChannelViewController实例。避免这种情况的最简单方法是在ViewController中准备一个数组属性

// In ViewController.h
@property (nonatomic, retain) NSMutableArray * channelViews;

// In ViewController.m
@synthesize channelViews;

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.channelViews = [[NSMutableArray alloc] initWithCapacity:10];

    // Do any additional setup after loading the view from its nib.
    int i;
    for (i=0;i<10;i++) {
        ChannelViewController *channelView = [[ChannelViewController alloc] init];
        [channelView.view setFrame:CGRectMake(i*175, 0, 175, 175)];
        //channelsScrollView is a ScrollView
        [self.channelsScrollView addSubview:channelView.view];
        [self.channelViews addObject:channelView];     // <-- Add channelView to the array
    }
    [self.channelsScrollView setContentSize:CGSizeMake(i*175, 175)];
}
//在ViewController.h中
@属性(非原子,保留)NSMutableArray*通道视图;
//在ViewController.m中
@综合渠道观点;
-(无效)viewDidLoad
{
[超级视图下载];
self.channelview=[[NSMutableArray alloc]initWithCapacity:10];
//从nib加载视图后,执行任何其他设置。
int i;

对于(i=0;我看到了类似的情况,但我不明白为什么需要这样做。Kazuki正在调用
[self.channelsScrollView addSubview:channelView.view]
,所以
self.channelsScrollView
现在保存了对
channelView.view
的引用。UIView没有保存对自己控制器的引用吗?这似乎很奇怪。我不知道UIView内部是否保存了对其UIViewController的引用,但即使它保存了,也应该是一个弱引用,以避免保留周期。@but如果将许多ChannelViewController对象添加到阵列中,那么这可能会由于内存问题导致应用程序崩溃。是否有更好的替代方案?@Euroboy那么您可能不需要一次显示所有ChannelViewController,因此会根据可见区域进行延迟加载。内存问题不是来自ARC,而是来自加载不必要的对象。