简体   繁体   English

UIbutton崩溃的应用程序,除了(lldb)以外没有其他错误

[英]UIbutton crashing app with no error other than (lldb)

I am working on view A (createExerciseViewController) that adds view B (createRoutinePopupViewController) after clicking a UIButton. 我正在研究视图A(createExerciseViewController),该视图在单击UIButton之后添加了视图B(createRoutinePopupViewController)。

This part works fine and the view is added fine. 这部分工作正常,并且添加了视图。

Then inside view B (createRoutinePopupViewController) I have another UIButton. 然后在视图B内部(createRoutinePopupViewController),我有另一个UIButton。 When I click this UIButton then the app crashes and all i get as an error is (lldb) and the NSLog is not executed. 当我单击此UIButton时,应用程序崩溃,我得到的所有错误均为(lldb)并且未执行NSLog。

but then sometimes and only sometimes it all gets executed fine after several crashes... 但是然后有时并且只有有时几次崩溃之后,一切都会正常执行...

I am quite new to the iOS dev world and I have no idea what I could be doing wrong. 我对iOS开发人员世界还很陌生,我不知道自己可能做错了什么。

All UIButton method are strong 所有的UIButton方法都很strong

Does anyone know why this could be happening? 有谁知道为什么会这样?

I think the issue could be in how i am inserting the subview and handling the whole subview?? 我认为问题可能出在我如何插入子视图和处理整个子视图中?

A ---- createExerciseViewController.m 一个---- createExerciseViewController.m

#import "createExerciseViewController.h"
#import "createExercisePopupViewController.h"
#import "createRoutinePopupViewController.h"

// ....more code

- (IBAction)createRoutine:(id)sender {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];
    [self.view addSubview:[[storyboard instantiateViewControllerWithIdentifier:@"createRoutinePopupView"] view]];
}

this is UIViewController B ---- createRoutinePopupViewController.m 这是UIViewController B ---- createRoutinePopupViewController.m

#import "createRoutinePopupViewController.h"
#import "createExerciseViewController.h"
#import "User.h"
#import "Routine.h"

- (IBAction)createRoutine:(UIButton *)sender {
    NSLog(@"Please dont crash");
  }

在此处输入图片说明

You shouldn't be creating view controllers just to add their views to another view controller's view willy-nilly. 您不应该只是为了将视图控制器添加到其他视图控制器的视图而随意创建视图控制器。 You need to tell the system that you're moving views from one controller to another, so that it can do its housekeeping. 您需要告诉系统您正在将视图从一个控制器移动到另一个控制器,以便它可以做家务。 If you don't do this, one view controller ends up owning a view that's being presented by another view controller, so events and touches etc get confused. 如果您不这样做,那么一个视图控制器最终将拥有另一个视图控制器正在呈现的视图,因此事件和触摸等会变得混乱。 This may be what's causing the crash. 可能是导致崩溃的原因。

iOS now provides a 'container view controller' mechanism to manage this situation, whereby you tell the system that you're moving a view from one controller to another. iOS现在提供了一种“容器视图控制器”机制来管理这种情况,即您告诉系统您正在将视图从一个控制器移动到另一个控制器。

From Apple's doc : 苹果的文档

The goal of implementing a container is to be able to add another view controller's view (and associated view hierarchy) as a subtree in your container's view hierarchy. 实现容器的目标是能够将另一个视图控制器的视图(和关联的视图层次结构)添加为容器的视图层次结构中的子树。 The child remains responsible for its own view hierarchy, save for where the container decides to place it onscreen. 子级仍然负责其自己的视图层次结构,除了容器决定将其放置在屏幕上的位置之外。 When you add the child's view, you need to ensure that events continue to be distributed to both view controllers. 添加子视图时,需要确保事件继续分配给两个视图控制器。 You do this by explicitly associating the new view controller as a child of the container. 您可以通过将新的视图控制器显式关联为容器的子级来实现。

In practice, it's simpler than it sounds. 实际上,它比听起来简单。 Try something like this in createExerciseViewController.m: 在createExerciseViewController.m中尝试以下操作:

    - (IBAction)createRoutine:(id)sender {
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];

        CreateRoutinePopupViewController* popupController = [storyboard instantiateViewControllerWithIdentifier:@"createRoutinePopupView"];

        //Tell the operating system the CreateRoutine view controller
        //is becoming a child:
        [self addChildViewController:popupController];

        //add the target frame to self's view:
        [self.view addSubview:popupController.view];

        //Tell the operating system the view controller has moved:
        [popupController didMoveToParentViewController:self];
    }

Try this: 尝试这个:

    CreateRoutinePopupViewController *popUp = [[storyboard instantiateViewControllerWithIdentifier:@"createRoutinePopupView"];
   [self presentModalViewController:popUp Animated:YES];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM