0

我的答案视图控制器(显示答案的视图控制器)上有一个后退按钮我的主要视图是显示所有要回答的问题。为什么会这样?这是一个非常简短的事情,但绝对引人注目!

 UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 48)];
navBar.delegate = self;
UINavigationItem *backItem = [[UINavigationItem alloc] initWithTitle:@"Back"];
[navBar pushNavigationItem:backItem animated:NO];

UINavigationItem *topItem = [[UINavigationItem alloc] initWithTitle:@"Question"];
[navBar pushNavigationItem:topItem animated:NO];
topItem.leftBarButtonItem = nil;

[self.view addSubview:navBar];


- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item
{

   ViewController *controller = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
   controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
   [self presentViewController:controller animated:YES completion:nil];
   return true;
}

- (void)navigationBar:(UINavigationBar *)navigationBar didPopItem:(UINavigationItem *)item
{

   ViewController *controller = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
   controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
   [self presentViewController:controller animated:YES completion:nil];
}

带有后退按钮的视图图片 正在发生的问题的图片

4

1 回答 1

2

您正在创建自己的UINavigationBar实例UINavigationItem,您可能不应该这样做。您描述的情况正是 aUINavigationController的用途。使用 aUINavigationController时,它会创建您在屏幕上显示的UINavigationBar每个UIViewController(推入导航控制器)都有自己的UINavigationItem(标题取自title视图控制器的标题)。

你得到一个名为“Back”的空“视图”的原因是你正在创建它:

UINavigationItem *backItem = [[UINavigationItem alloc] initWithTitle:@"Back"];
[navBar pushNavigationItem:backItem animated:NO];

放弃所有这些,创建一个UINavigationController并将您的问题视图控制器设为根视图控制器,然后将导航控制器添加到屏幕上。然后当一个问题被回答时,推送答案视图控制器:

[self.navigationController pushViewController:answerViewController animated:YES];
于 2013-05-13T22:45:44.690 回答