单击按钮时EXC_BAD_ACCESS

时间:2013-12-31 09:47:54

标签: ios uiscrollview

我并排添加了2个视图。但是当我尝试点击第二个视图上的按钮时,我得到EXC_BAD_ACCESS。如果我用另一个视图切换视图,那么它表现良好。但在第二种观点上它不会。

UIViewController *AddDataViewController=[self.storyboard instantiateViewControllerWithIdentifier:@"AddData"];
UIViewController *ShowDataViewController=[self.storyboard instantiateViewControllerWithIdentifier:@"ShowData"];


CGRect Frame;

Frame.origin.x=0;
Frame.origin.y=0;
Frame.size=CGSizeMake(320, 250);

AddDataViewController.view.frame=Frame;

[self.scrollView2 addSubview:AddDataViewController.view];

CGRect Frame1;

Frame1.origin.x=320;
Frame1.origin.y=0;
Frame1.size=CGSizeMake(320, 250);

ShowDataViewController.view.frame=Frame1;

[self.scrollView2 addSubview:ShowDataViewController.view];

错误是:

2013-12-31 16:17:56.856 wheel_With_images [439:c07] * - [ShowDataViewController performSelector:withObject:withObject:]:消息发送到解除分配的实例0x767de70

1 个答案:

答案 0 :(得分:3)

问题是在执行方法后会释放AddDataViewControllerShowDataViewController。您必须将它们添加为实例变量

在.h文件中

@property (nonatomic, strong) UIViewController *AddDataViewController;
@property (nonatomic, strong) UIViewController *ShowDataViewController;

在.m文件中

self.AddDataViewController=[self.storyboard instantiateViewControllerWithIdentifier:@"AddData"];
self.ShowDataViewController=[self.storyboard instantiateViewControllerWithIdentifier:@"ShowData"];