问题描述
我有一个包含 3 个视图控制器的标签栏控制器,在其中一个视图控制器中,我有一个按钮来显示另一个不在标签栏控制器中的视图控制器。按钮中的功能运行,但另一个视图控制器没有显示。更具体地说,标签栏中的 3 个视图控制器是 ProfilePage、EditProfile 页面和 AllUsersProfile 页面。在我的 ProfilePage 中,我有一个退出按钮可返回登录屏幕,该按钮不是 tabBar 的一部分。
这是我的代码:
- (void)scene:(UIScene *)scene willConnectToSession:(UIScenesession *)session options:(UISceneConnectionoptions *)connectionoptions {
// last login
Nsstring * username = [[NSUserDefaults standardUserDefaults] objectForKey:@"SessionKey"];
Users* lastUser =[LocalStorageController loadCustomObjectWithKey:username];
ProfilePage *lastUserProfile = [[ProfilePage alloc] initWithUser:lastUser];
EditProfile *lastUserEdit = [[EditProfile alloc]initWithUser:lastUser];
AllUsersProfile *lastUserViewAll = [[AllUsersProfile alloc]init];
UITabBarController *tabBarController = [[UITabBarController alloc] init];
NSArray *viewControllers = [NSArray arrayWithObjects:lastUserProfile,lastUserEdit,lastUserViewAll,nil];
[tabBarController setViewControllers:viewControllers animated:NO];
[tabBarController release];
self.window.rootViewController = tabBarController;
lastUserEdit.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Edit Profile" image:nil selectedImage:nil];
lastUserProfile.tabBarItem =[[UITabBarItem alloc] initWithTitle:@"Home" image:nil selectedImage:nil];
lastUserViewAll.tabBarItem =[[UITabBarItem alloc] initWithTitle:@"View Others" image:nil selectedImage:nil];
这里是注销代码:
- (void) logout{
[self dismissViewControllerAnimated:YES completion: nil]; //this does not show the LoginScreen view controller
}
解决方法
请检查当前代码以在 TabBar 控制器中显示新控制器。
// 你有三个 UIViewControllers
- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
ViewControllerTab1 * tabVC1 = [[ViewControllerTab1 alloc] init];
ViewControllerTab2 * tabVC2 = [[ViewControllerTab2 alloc] init];
ViewControllerTab3 * tabVC3 = [[ViewControllerTab3 alloc] init];
UITabBarController *tabBarController = [[UITabBarController alloc] init];
NSArray * viewControllers = [NSArray arrayWithObjects:tabVC1,tabVC2,tabVC3,nil];
[tabBarController setViewControllers:viewControllers animated:NO];
self.window.rootViewController = tabBarController;
tabVC1.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Edit Profile" image:nil selectedImage:nil];
tabVC2.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Home" image:nil selectedImage:nil];
tabVC3.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"View Others" image:nil selectedImage:nil];
}
在 ViewControllerTab1 中,根据您的要求显示您想要的 NewViewController
-(void)presentAnotherVC{
NewViewController * vc = [[NewViewController alloc]init];
[self presentViewController:vc animated:YES completion:nil];
}
在 NewViewController 中,关闭 NewViewController 以返回到选定的 TabBar 控制器
-(void)dsmissThisVC{
[self dismissViewControllerAnimated:YES completion:nil];
}