我正在尝试像警报视图一样创建自定义登录视图弹出窗口。我使用以下函数模拟alertview弹出窗口。我的loginViewController.m
中的viewDidload中可以找到此函数-(void)initialDelayEnded {
self.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.001, 0.001);
self.view.alpha = 1.0;
[UIView animateWithDuration:1.0/1.5 animations:^{
self.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.1, 1.1);
}completion:^(BOOL complete){
[UIView animateWithDuration:1.0/2 animations:^{
self.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.9, 0.9);
}completion:^(BOOL complete){
[UIView animateWithDuration:1.0/2 animations:^{
self.view.transform = CGAffineTransformIdentity;
}];
}];
}];
}
- (void)viewDidLoad
{
[self initialDelayEnded];
[super viewDidLoad];
}
我正在通过以下方式在我的firstViewController中调用loginViewController。
LoginViewController *login = [[LoginViewController alloc]initWithNibName:@"LoginViewController" bundle:NULL];
[self presentViewController:login animated:YES completion:NULL];
但它因以下错误而崩溃。
'UIViewControllerHierarchyInconsistency', reason: 'A view can only be associated with at most one view controller at a time! View <UIView: 0x8674bf0; frame = (0 20; 320 460); autoresize = W+H; layer = <CALayer: 0x8670620>> is associated with <LoginViewController: 0x868a7d0>. Clear this association before associating this view with <LoginViewController: 0x8451e70>.
有人能帮助我吗?
提前致谢!
答案 0 :(得分:1)
下面:
[self presentViewController:login animated:YES completion:NULL];
您正在通过Self呈现viewController
,我猜这本身就是viewcontroller
。
相反你应该使用:
[self presentModalViewController:login animated:YES];
如果你想展示你的viewcontroller
,而不是将它推到导航堆栈上。
在哪个类中使用此代码。
答案 1 :(得分:0)
你检查过LoginViewController xib吗?您是否有可能使用多个视图控制器映射相同的视图?
答案 2 :(得分:0)
除非您有特殊原因,否请在本地实施中尽早致电[super viewDidLoad];
。含义:在[self initialDelayEnded];
[super viewDidLoad];
请确保名为LoginViewController
的.xib文件在File's Owner
中只有一个Placeholders
,Objects
面板中没有ViewController对象。并确保File's Owner
的自定义类为LoginViewController
。您能否上传.xib的屏幕截图,专门显示Document Outline
?可以更容易地找出可能出错的地方
答案 3 :(得分:0)
错误消息暗示有两个'LogInViewController'实例。你在使用故事板吗?如果是这样,您可以将视图控制器添加到主故事板文件(而不是单独的笔尖),为其提供标记/标识符(但不要将其连接到其他VC)。然后,您可以获取故事板创建的实例并显示该实例。像这样:
//Get the stroyboard
UIStoryBoard *mainStoryBoard = [UIStoryBoard storyboardWithName:<STORYBOARD_NAME> bundle:nil];
//Get the VC
LogInViewController *login = [mainStoryBoard instantiateViewControllerWithIdentifier:<VIEWCONTROLLER_TAG>];
//Present
[self presentModalViewController:login animated:YES];
此外,您不应该尝试在viewDidLoad:
中呈现其他视图控制器,它将无法正常工作。移动代码以将新控制器显示为viewDidAppear:
答案 4 :(得分:0)
我通过以下方式解决了我的应用登录问题&gt;
在警告框出现之前加载LoginSubView控制器。
AFLoginViewController *LoginSubView = [[AFLoginViewController alloc] init];
[_window addSubview:LoginSubView.view];
[_window makeKeyAndVisible];
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"This is an example alert!" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Login", nil];
alert.alertViewStyle = UIAlertViewStyleSecureTextInput;
[alert show];
如果登录成功,我在其他任何行之前删除了LoginSubView
for (UIView *subView in _window.subviews)
{
if (subView.tag == 99)
{
[subView removeFromSuperview];
}
}
也可以在applicationDidEnterBackground上添加相同的子视图。下次回到前台时避免敏感的屏幕视图闪烁