将ViewController添加为subView并显示发送到解除分配的实例0x8a6efc0的消息

时间:2013-06-13 07:13:40

标签: iphone ios objective-c xcode

ViewControllerUIButton

- (IBAction)buttonPressed:(id)sender {
    FirstTopViewController *controller = [[FirstTopViewController alloc]init];
    controller = [self.storyboard instantiateViewControllerWithIdentifier:@"Upper"];
    controller.view.frame = CGRectMake(300,0, 320, 460);


    [UIView animateWithDuration:0.5
                          delay:0.1
                        options: UIViewAnimationCurveEaseIn
                     animations:^{
                         controller.view.frame = CGRectMake(20, 0, 320, 460);
                     }
                     completion:^(BOOL finished){
                     }];
      [self.view addSubview:controller.view];
}

在这里,我将newViewController添加为MenuViewController的子视图。

使用FirstViewController

UIButton
- (IBAction)backToMain:(id)sender {
    [UIView animateWithDuration:1.5
                          delay:0.5
                        options: UIViewAnimationCurveEaseIn
                     animations:^{
                         self.view.frame = CGRectMake(0, 490, 320, 460);
                     }
                     completion:^(BOOL finished){
                     }];
}

ISSUE 当我按下MenuViewController上的按钮时,它将新FirstViewController显示为带有动画的subView,但是当我在FirstViewController上按下按钮时,它会崩溃

-[FirstViewController performSelector:withObject:withObject:]: message sent to deallocated instance 0x8a6efc0 

请查看。

2 个答案:

答案 0 :(得分:1)

FirstTopViewController *controller作为类级别变量。问题是控制器对象是本地的,并在btn_funtion完成后被解除分配。然后,当您点击按钮时,它会向已解除分配的实例发送消息。

答案 1 :(得分:0)

在MenuViewController的.h文件中创建一个FirstViewController实例。 现在,您的FirstViewController实例将由父视图保存。