【问题标题】:App Crashes and memoryleaks are all over应用程序崩溃和内存泄漏已经结束
【发布时间】:2011-04-28 11:13:34
【问题描述】:

我的应用在模拟器中运行了一段时间,但几次触摸后,它崩溃并烧毁。

这里是代码

-(IBAction)extraCrisp:(id)sender
{
    myViewController *myView = [[myViewController alloc]
                                    initWithNibName:@"myViewController"
                                    bundle:nil];
    // get the view that's currently showing
    UIView *currentView = self.view;
    // get the the underlying UIWindow, or the view containing the current view
    UIView *theWindow = [currentView superview];

    UIView *newView = myView.view;

    // remove the current view and replace with myView1
    [currentView removeFromSuperview];
    [theWindow addSubview:newView];

    // set up an animation for the transition between the views
    CATransition *animation = [CATransition animation];
    [animation setDuration:0.5];
    [animation setType:kCATransitionPush];
    [animation setSubtype:kCATransitionFromLeft];
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];

    [[theWindow layer] addAnimation:animation forKey:@"SwitchToView1"];
    [myView release];

}

那么,我在这里做错了什么?任何帮助都会受到高度赞扬。谢谢

【问题讨论】:

  • 你想用这段代码做什么,首先显示一个视图,然后在一个按钮上单击带有过渡动画的第二个视图,对吧?
  • 你有我们的崩溃日志吗?
  • @Krishnabhadra 是的,这正是我想要做的。
  • 我收到了 EXC_BAD_ACCESS,对不起,有点初学者......你需要什么样的崩溃日志?或者在哪里可以找到......再次,初学者的东西。仍然在学习。非常感谢。

标签: ios objective-c memory-management memory-leaks


【解决方案1】:

安迪,您的代码确实表明您是初学者。要实现您的要求,可以使用更清洁、更安全的方法。对于多视图应用程序,通常每个人都使用UINavigationControllerUITabBarController。在您的情况下 UINavigationController 是您的朋友,如果您可以花一个小时 this 教程将帮助您入门..我建议您学习使用这些方法,而不是继续您现在的方式...并停止阅读此答案..

现在你的代码好了..这是第一部分..

myViewController *myView = [[myViewController alloc]
                                    initWithNibName:@"myViewController"
                                    bundle:nil];
// get the view that's currently showing
UIView *currentView = self.view;
// get the the underlying UIWindow, or the view containing the current view
UIView *theWindow = [currentView superview];

UIView *newView = myView.view;

// remove the current view and replace with myView1
[currentView removeFromSuperview];
[theWindow addSubview:newView];

我这样重写你的代码..

myViewController *myView = [[myViewController alloc]
                                        initWithNibName:@"myViewController"
                                        bundle:nil];
UIWindow *currentWindow = [[UIApplication sharedApplication]keyWindow];
[currentWindow addSubview:myView.view];

现在您将 myView 添加到 UIWindow 后发布它。如果您评论该行,我认为您的崩溃将会消失。通常,添加到 UIWindow 的 UIViewController 会在应用程序委托文件的 dealloc 中释放。

现在你的动画..

// set up an animation for the transition between the views
CATransition *animation = [CATransition animation];
[animation setDuration:0.5];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromLeft];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];

[[myView layer] addAnimation:animation forKey:@"SwitchToView1"];

再次..忘记这段代码并阅读 UINavigationController..你在那里做错了..

【讨论】:

  • 在使用 UINavigationController 方法时如何添加除“下拉”以外的不同动画?
  • 您正在获取 UINavigationController 的下拉动画吗?通常 UINavigationController 有从左到右的转换..
  • 我没有得到下拉动画,没有。我以错误的方式回答...我想知道是否有可能通过这种转换获得这种效果。我按照您发布的链接以及 Adams 教程进行操作。哪个工作正常。虽然,正如我所说,我的愿望是制作具有下拉效果的动画,从左到右(和反向)并从下到上。我阅读了有关块动画的信息,但我不太确定该怎么做。谢谢
猜你喜欢
  • 1970-01-01
  • 2018-04-04
  • 2011-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-09
  • 2013-05-09
相关资源
最近更新 更多