我認爲這將是最好使用自定義的容器控制的方法來做到這一點。在下面的示例代碼中,我創建了一個登錄控制器,它的大小是一個表單(540 x 600),並將其從右側滑入ViewController的主視圖,以便它垂直居中,右邊。
在ViewController.m,我有這樣的:
-(IBAction)showLogin:(id)sender {
LoginController *login = [self.storyboard instantiateViewControllerWithIdentifier:@"Login"];
login.view.frame = CGRectMake(768, 202, 540, 600);//centered vertically and offscreen to the right
[self addChildViewController:login];
[self.view addSubview:login.view];
[UIView animateWithDuration:.5 animations:^{
login.view.frame = CGRectMake(228, 202, 540, 600);
} completion:^(BOOL finished) {
[login didMoveToParentViewController:self];
}];
}
,並刪除了看法,我有這樣的LoginController.m:
編輯
-(IBAction)goBackToMain:(id)sender {
[UIView animateWithDuration:.5 animations:^{
self.view.frame = CGRectMake(768, 202, 540, 600);
} completion:^(BOOL finished) {
[self.view removeFromSuperview];
[self removeFromParentViewController];
}];
}
後:
如果你想在視圖控制器按鈕刪除登錄控制器,你可以做這樣的:
-(IBAction)goBackToMain:(id)sender {
LoginController *login = self.childViewControllers.lastObject;
[UIView animateWithDuration:.5 animations:^{
login.view.frame = CGRectMake(768, 202, 540, 600);
} completion:^(BOOL finished) {
[login.view removeFromSuperview];
[login removeFromParentViewController];
}];
}
嘗試使用'initWithNibName:@ 「MyNibName」]'你'LoginViewController'初始化。它可能創造UIViewController'的'一個新實例,因爲它未鏈接到你的筆尖文件.. – Jeeter
我把標題和這一觀點與該名LoginViewController控制器的故事板ID,然後將我的代碼更改LoginViewController *的LoginController = [[LoginViewController alloc] initWithNibName:@「LoginViewController」bundle:nil];但是由於未捕獲異常'NSInternalInconsistencyException',原因:'無法在軟件包中加載NIB: – Anna
而不是'nil',請嘗試'[NSBundle mainBundle]',看看是否有效,終止應用程序。 – Jeeter