我创建了ViewController A(a.k.a vcA)和vcB嵌入导航视图控制器,将故事板中的子视图控制器映射为classes.m
当我尝试构建视图控制器A-> B-> A的重定向,或者从vcB创建vcA时,使用:
A :
PairViewController * sliderVC = [self.storyboard instantiateViewControllerWithIdentifier:@"PairViewController"];
sliderVC.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentViewController:sliderVC animated:NO completion:nil];
sliderVC.view.backgroundColor = [UIColor clearColor];
B : ViewController *destinationController = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
[destinationController setBle:_ble];
[destinationController setLeDevice:selectedDevice];
destinationController.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentViewController:destinationController animated:NO completion: nil] ;
新A创建时没有出现任何导航栏。
当我使用
时[self.navigationController pushViewController:destinationController animated:nil];
xCode表示异常;
Cannot call pushNavigationItem:animated: directly on a UINavigationBar managed by a controller.'
请您告诉我从vcB创建vcA的方法,我的自定义导航栏保持完整,不丢失?
在A中,我尝试在此方法中构建导航栏,但如果从vcB创建vcA,导航栏将变为nil
-(void)viewWillLayoutSubviews{
navigationBar= [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
}
- (void)viewWillAppear:(BOOL)animated {
[self.navigationController setNavigationBarHidden:NO];
navigationBar = self.navigationController.navigationBar;
[navigationBar setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], NSForegroundColorAttributeName,
[UIFont fontWithName:@"TitilliumText22L-Medium" size:22.0], NSFontAttributeName,
nil] ];
UINavigationItem *navigationItem = [[UINavigationItem alloc] initWithTitle:@"FPV Control"];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 20)];
[button setImage:[UIImage imageNamed:@"menu_back.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
// UIBarButtonItem *buttonItemA = [[UIBarButtonItem alloc] initWithCustomView:button];
UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self action:@selector(buttonClicked:)];
[leftBarButtonItem setCustomView:button];
navigationItem.leftBarButtonItem = leftBarButtonItem;
UIButton *buttonA = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 70, 20)];
[buttonA setImage:[UIImage imageNamed:@"rc_logo.png"] forState:UIControlStateNormal];
UIBarButtonItem *buttonItemB = [[UIBarButtonItem alloc] initWithCustomView:buttonA];
navigationItem.rightBarButtonItem = buttonItemB;
[navigationBar pushNavigationItem:navigationItem animated:NO];
NSLog(@" navigaytion item : %@" , navigationItem);
NSLog(@" navigaytion bar : %@" , navigationBar);
答案 0 :(得分:1)
所以使用NavigationController呈现你的ViewController,如下所示。
MyViewController *myViewController = [[MyViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController *navigationController =
[[UINavigationController alloc] initWithRootViewController:myViewController];
//now present this navigation controller modally
[self presentViewController:navigationController
animated:NO
completion:nil];