gpt4 book ai didi

ios - UIbutton 崩溃应用程序,除了 (lldb) 之外没有任何错误

转载 作者:行者123 更新时间:2023-12-03 16:51:16 26 4
gpt4 key购买 nike

我正在处理 View A (createExerciseViewController),它在单击 UIButton 后添加 View B (createRoutinePopupViewController)。

这部分工作正常,并且 View 添加得很好。

然后在 View B (createRoutinePopupViewController) 中我有另一个 UIButton。当我单击此 UIButton 时,应用程序崩溃,我得到的所有错误是 (lldb) 并且 NSLog 未执行。

但有时且仅有时在几次崩溃后一切都会正常执行......

我对 iOS 开发世界还很陌生,我不知道我可能做错了什么。

所有 UIButton 方法都是

有人知道为什么会发生这种情况吗?

我认为问题可能在于我如何插入 subview 并处理整个 subview ?

A ---- 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]];
}

这是UIViewControllerB----createRoutinePopupViewController.m

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

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

enter image description here

最佳答案

您不应该创建 View Controller 只是为了将其 View 添加到另一个 View Controller 的 View 中。您需要告诉系统您正在将 View 从一个 Controller 移动到另一个 Controller ,以便它可以完成其内务处理。如果您不这样做,一个 View Controller 最终会拥有另一个 View Controller 呈现的 View ,因此事件和触摸等会变得困惑。这可能就是导致崩溃的原因。

iOS 现在提供了一种“容器 View Controller ”机制来管理这种情况,您可以通过该机制告诉系统您正在将 View 从一个 Controller 移动到另一个 Controller 。

来自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.

实际上,它比听起来更简单。在 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];
}

关于ios - UIbutton 崩溃应用程序,除了 (lldb) 之外没有任何错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16288663/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com