ios - 单击按钮删除 View Controller

标签 ios uiviewcontroller

我正在为 iOS 应用程序开发一个库,该库将根据用户输入的凭据对用户进行身份验证。

我正在做的是,从 View Controller 说 TestViewController 我在 TestViewController 之上设置一个新的 View Controller LoginScreenViewController ,当用户使用以下代码点击按钮。

LoginScreenViewController * LoginScreen = [[LoginScreenViewController alloc] initWithNibName:nil bundle:nil];

[self.view addSubview:LoginScreen.view];

LoginScreenViewController包含一个Cancel按钮,我想删除该LoginScreenViewController按钮。为此,我使用以下代码

[self.view removeFromSuperview];

问题是当我执行此代码时,我的应用程序崩溃了。

由于我的 LoginScreenViewController 是由库定义的,因此我希望它独立于调用者的实现。

编辑:
LoginScreenViewController.m 中,我使用以下代码将事件处理程序添加到取消按钮。

[button addTarget:self action:@selector(CancelButtonHandler:) forControlEvents:UIControlEventTouchUpInside];

当我点击取消按钮时,应用程序崩溃了。即使 CancelButtonHandler 不包含任何代码行。

最佳答案

您正在尝试删除错误的 View 。要删除 LoginScreen View ,请执行以下操作:

[LoginScreen.view removeFromSuperview];

关于ios - 单击按钮删除 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19223606/

相关文章:

ios - UIView hitTest 阻止点击 subview

ios - iOS中从中央服务器查询大量数据的建议

ios - 更改 Core Data 中的验证规则时是否需要轻量级迁移

ios - Controller 中的 UIView 触摸事件

ios - 当特定 ViewController 类被插入另一个选项卡中的堆栈时,如何从堆栈中弹出 View Controller ?

ios - 有没有办法在 Storyboard中将自定义 UIView 设置为 'design',并在不添加到任何特定 Controller 的情况下在多个 ViewController 中使用?

ios - 制作一些可拖动的 UIView

ios - WKWebView子类在dealloc上崩溃

ios - 如何在将当前 View 缩放到 0.5% 之前弹出父 View Controller 以在后台绘制

ios - 将 UIViewController 添加为 subview 后取消初始化