我想我只是坚持设计的逻辑。需要考虑的重要因素......我正在尝试以编程方式执行所有操作。在我的AppDelegate中,我将标签栏控制器指定为根。我正在努力的是如何使用它自己的导航栏和项目在标签栏控制器顶部显示登录。
首先,我尝试将LoginViewController作为根目录。要导航到标签栏控制器我使用[self.navigationController setViewControllers]方法。这导致了问题,因为我猜它正在将登录视图转换为标签栏视图,因此当尝试注销并返回到根视图时,登录视图全部搞砸了。
现在我正在尝试将标签栏控制器设置为根视图,并使用我的LoginViewController添加容器视图。问题是1)标签栏项目1的导航栏仍然显示,2)标签栏本身正在显示。所以我想我要问几件事......我怎样才能让容器视图占据整个屏幕,并带有自己的导航控制器...什么是实现注销按钮返回根目录的最佳方法查看控制器?
LogInView.m
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
UIBarButtonItem *signup = [[UIBarButtonItem alloc]initWithTitle:@"Sign Up" style:UIBarButtonItemStyleBordered target:self action:@selector(showSignUp:)];
self.navigationItem.rightBarButtonItem = signup;
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Log In" style:UIBarButtonItemStyleBordered target:nil action:nil];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(50, 150, 100, 30);
[button setTitle:@"Login Done" forState:UIControlStateNormal];
[button addTarget:self action:@selector(push:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)push:(id)sender{
[[NSUserDefaults standardUserDefaults]setObject:@"something" forKey:@"something"];
[self willMoveToParentViewController:nil];
[self.view removeFromSuperview];
[self removeFromParentViewController];
}
- (void)showSignUp:(id)sender{
SignUpViewController *signup = [[SignUpViewController alloc]init];
[self.navigationController pushViewController:signup animated:YES];
}
TabBarView1.m
- (void)viewWillAppear:(BOOL)animated{
if ([[NSUserDefaults standardUserDefaults]valueForKey:@"something"] == nil) {
LogInViewController *login = [[LogInViewController alloc]init];
[self addChildViewController:login];
[self willMoveToParentViewController:self];
[self.view addSubview:login.view];
[login didMoveToParentViewController:self];
}
}
到目前为止很有用......除了我没有看到带有按钮的LoginView导航栏的部分。第二个有问题的方法是退出:
TabView4.m
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.title = @"Me";
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Logout" style:UIBarButtonItemStyleBordered target:self action:@selector(logout:)];
}
- (void)logout:(id)sender{
[[NSUserDefaults standardUserDefaults]removeObjectForKey:@"something"];
[[NSUserDefaults standardUserDefaults]synchronize];
self.tabBarController.selectedIndex = 0;
//that takes me back to the first tab and the login screen comes up...
//is that the best way to do this right now?
}
如果有人有任何建议(我知道有更好的方法可以做到这一点),请告诉我。但请记住,我正在尝试以编程方式完成此工作,而不使用storyboards / segues / xibs。请帮忙!
正确答案:按下按钮上的模态
- (void) modal:(id)sender{
LogInViewController *login = [[LogInViewController alloc]init];
UINavigationController *loginNav = [[UINavigationController alloc]initWithRootViewController:login];
loginNav.modalPresentationStyle = UIModalPresentationFullScreen;
loginNav.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:loginNav animated:YES completion:nil];
}
这只是点击按钮时第一个标签的尝试。它出现全屏但没有导航栏......