[英]UIbutton crashing app with no error other than (lldb)
我正在研究视图A(createExerciseViewController),该视图在单击UIButton之后添加了视图B(createRoutinePopupViewController)。
这部分工作正常,并且添加了视图。
然后在视图B内部(createRoutinePopupViewController),我有另一个UIButton。 当我单击此UIButton时,应用程序崩溃,我得到的所有错误均为(lldb)
并且未执行NSLog。
但是然后有时并且只有有时几次崩溃之后,一切都会正常执行...
我对iOS开发人员世界还很陌生,我不知道自己可能做错了什么。
所有的UIButton方法都很strong
有谁知道为什么会这样?
我认为问题可能出在我如何插入子视图和处理整个子视图中?
一个---- 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]];
}
这是UIViewController
B ---- createRoutinePopupViewController.m
#import "createRoutinePopupViewController.h"
#import "createExerciseViewController.h"
#import "User.h"
#import "Routine.h"
- (IBAction)createRoutine:(UIButton *)sender {
NSLog(@"Please dont crash");
}
您不应该只是为了将视图控制器添加到其他视图控制器的视图而随意创建视图控制器。 您需要告诉系统您正在将视图从一个控制器移动到另一个控制器,以便它可以做家务。 如果您不这样做,那么一个视图控制器最终将拥有另一个视图控制器正在呈现的视图,因此事件和触摸等会变得混乱。 这可能是导致崩溃的原因。
iOS现在提供了一种“容器视图控制器”机制来管理这种情况,即您告诉系统您正在将视图从一个控制器移动到另一个控制器。
从苹果的文档 :
实现容器的目标是能够将另一个视图控制器的视图(和关联的视图层次结构)添加为容器的视图层次结构中的子树。 子级仍然负责其自己的视图层次结构,除了容器决定将其放置在屏幕上的位置之外。 添加子视图时,需要确保事件继续分配给两个视图控制器。 您可以通过将新的视图控制器显式关联为容器的子级来实现。
实际上,它比听起来简单。 在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];
}
尝试这个:
CreateRoutinePopupViewController *popUp = [[storyboard instantiateViewControllerWithIdentifier:@"createRoutinePopupView"];
[self presentModalViewController:popUp Animated:YES];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.