我正在为 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/