UIButton点击崩溃应用

Posted

技术标签:

【中文标题】UIButton点击崩溃应用【英文标题】:UIButton click crashes app 【发布时间】:2013-10-30 09:47:26 【问题描述】:

我在page 1 上有按钮,当我点击按钮时,我正在执行一些动画并导航到新页面说Page 2

page 2 上,当我点击我想返回上一个页面时有取消按钮,即Page 1,但在点击取消按钮时,我的应用程序崩溃了。

这是我在 Page 1:页面名称 CRViewController

上的示例代码
- (IBAction)ClickMe:(id)sender 
    RegistraionViewController *secondController = [[RegistraionViewController alloc] init];

    CATransition *transitionAnimation = [CATransition animation];
    [transitionAnimation setDuration:1];
    [transitionAnimation setType:@"push"];
    [transitionAnimation setSubtype:kCATransitionFromBottom];
    [transitionAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];

    [self.view.layer addAnimation:transitionAnimation forKey:kCATransitionFromBottom];
    [self.view addSubview:secondController.view];

Page 2: 上的代码 RegistraionViewController 页面的名称(点击时崩溃。)

- (IBAction)Click:(id)sender 
    CRViewController *secondController = [[CRViewController alloc] init];

    CATransition *transitionAnimation = [CATransition animation];
    [transitionAnimation setDuration:1];
    [transitionAnimation setType:@"push"];
    [transitionAnimation setSubtype:kCATransitionFromTop];
    [transitionAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];

    [self.view.layer addAnimation:transitionAnimation forKey:kCATransitionFromBottom];
    [self.view addSubview:secondController.view];

【问题讨论】:

而当app崩溃时,有没有报错信息? @Krunal:展示如何创建“取消”按钮。 不,我的日志中只显示lldb 尝试在按钮操作中放置一个断点。然后逐行执行,看看在哪一行崩溃。 @Krunal: [self.view addSubview:secondController.view];而不是这个使用 [self presentViewController:secondController animated:YES]; 【参考方案1】:

试试这个:

- (IBAction)ClickMe:(id)sender    
    RegistraionViewController *secondController = [[RegistraionViewController alloc] init];   
    [self.navigationController pushViewController:secondController animated:YES];   



- (IBAction) Click:(id)sender     
   [self.navigationController popViewControllerAnimated:YES];    

编辑: 对不起。如果您想自定义一些 UIViewController 过渡动画,请参见下文:UIViewController transition - objective-chttp://www.teehanlax.com/blog/custom-uiviewcontroller-transitions/ 或者你可以试试这个:

- (IBAction)ClickMe:(id)sender 
    RegistraionViewController *secondController = [[RegistraionViewController alloc] init];

    CATransition *transitionAnimation = [CATransition animation];
    [transitionAnimation setDuration:1];
    [transitionAnimation setType:@"push"];
    [transitionAnimation setSubtype:kCATransitionFromBottom];
    [transitionAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];

    [self.navigationController.view.layer addAnimation:transitionAnimation forKey:nil];
    [self.navigationController pushViewController:secondController animated:NO]; 


- (IBAction)Click:(id)sender 
    CRViewController *secondController = [[CRViewController alloc] init];

    CATransition *transitionAnimation = [CATransition animation];
    [transitionAnimation setDuration:1];
    [transitionAnimation setType:@"push"];
    [transitionAnimation setSubtype:kCATransitionFromTop];
    [transitionAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];

    [self.navigationController.view.layer addAnimation:transitionAnimation forKey:nil];
    [self.navigationController popToViewController:secondController animated:NO];

【讨论】:

我想要一些动画。不要推送和弹出 我试过这段代码,它可以工作。您应该确保您的控制器层次结构中有 navigationController。 动画来了,但动画后它会加载相同的文件 这是我的代码:dropbox.com/s/tmh8ik23t54w63g/TestTransitionAnimation.zip 您提供的项目中的所有文件都丢失了。【参考方案2】:

由于子视图,您的问题出在某个地方。我试过你的代码。

我在我的项目中做了以下代码,并带有一些动画,希望对您有所帮助。

CATransition *animation = [CATransition animation];
    [animation setDelegate:self];
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
    [animation setDuration:0.8];
    [animation setType:kCATransitionFade];
    [animation setSubtype:kCATransitionMoveIn];

    //[[self.view layer]addAnimation:animation forKey:@"fview"];

    [self.navigationController.view.layer addAnimation:animation forKey:nil];

    [self.navigationController pushViewController:yourView animated:NO];

在动画类型中,您可以提供任何您想要的动画。

希望对你有所帮助。

【讨论】:

看不到动画:( 我现在正在我的项目中运行此代码,伙计。如果您在其中传递了正确的视图名称,请检查它。在您的代码中,如果您推送视图而不是子视图,它会向您显示动画。 RegistraionViewController *secondController = [[RegistraionViewController alloc] init];CATransition *animation = [CATransition animation];[animation setDelegate:self];[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];[animation setDuration:0.8];[animation setType:kCATransitionFade]; [animation setSubtype:kCATransitionMoveIn]; [self.navigationController.view.layer addAnimation:animation forKey:nil];[self.navigationController pushViewController:secondController animated:NO]; 我试过上面的代码,动画来了,但是在动画之后它加载了相同的文件。 然后检查你是否传递了一个你想要导航的视图。并且不要为不同视图控制器的对象取相同的名称。【参考方案3】:

*

- (IBAction)ClickMe:(id)sender 
    RegistraionViewController *secondController = [[RegistraionViewController alloc] init];
    CATransition *transitionAnimation = [CATransition animation];
    [transitionAnimation setDuration:1];
    [transitionAnimation setType:@"push"];
    [transitionAnimation setSubtype:kCATransitionFromBottom];
    [transitionAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];
    [self.view.layer addAnimation:transitionAnimation forKey:kCATransitionFromBottom];
    [self addChildViewController:secondController];
    [self.view addSubview:secondController.view];

* 将第 1 页上的示例代码替换为上述代码,然后检查。

希望对你有帮助

【讨论】:

以上是关于UIButton点击崩溃应用的主要内容,如果未能解决你的问题,请参考以下文章

uipageviewcontroller内的UIbutton点击崩溃

在 UItableview 单元格中创建的 UIButton 在点击时崩溃

UIButton Clicked for Webview 加载到 ipad 后应用程序崩溃

UIButton 正在使应用程序崩溃

将viewcontroller的视图添加为子视图后UIButton点击事件崩溃

UIButton 在 UITableView 中不起作用(点击时崩溃)?我可以得到一些关于 obj-c 的帮助吗