0

I have created ViewController A (a.k.a vcA) and vcB embed in Navigation View Controller ,mapping child view controller in storyboard as classes.m

When I try to construct this redirect of view controller A->B->A , or to create vcA from vcB , by using :

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] ;

The new A is created without any navigation bar appeared.

When I use

[self.navigationController pushViewController:destinationController animated:nil]; 

The xCode said for the exception ;

Cannot call pushNavigationItem:animated: directly on a UINavigationBar managed by a controller.'

Would you please tell me the way to create vcA from vcB , with my custom navigation bar keeping intact , not missing ?

In A I try to construct the navigation bar in this method but navigation bar turns nil if creating vcA from vcB

-(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);
Jeff Bootsholz
  • 2,971
  • 15
  • 70
  • 141

1 Answers1

1

So present your ViewController with NavigationController as below.

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];
DilumN
  • 2,889
  • 6
  • 30
  • 44
  • I have an empty UINavigationController set on Storyboard (without mapping to the implementation class ). It turns to showing this error Cannot call pushNavigationItem:animated: directly on a UINavigationBar managed by a controller.' – Jeff Bootsholz Jan 04 '16 at 05:19
  • Then map your storyboard UINavigationController to here or create it pragmatically as above – DilumN Jan 04 '16 at 05:28
  • Do you mean mapping my UINavigationController to my ViewControllerA or create the new one mapping? – Jeff Bootsholz Jan 04 '16 at 05:30
  • It saids I cannot push push a navigation controller to another navigation controller. What should I do then ? http://stackoverflow.com/questions/33044758/push-uinavigationcontroller-programmatically-ios9 – Jeff Bootsholz Jan 04 '16 at 05:32
  • From bViewController when presenting aViewController [self.navigationController presentViewController:navigationController animated:NO completion:nil]; – DilumN Jan 04 '16 at 05:36
  • I have just set as above and set Storyboard id in navigation controller , it turns 'Pushing a navigation controller is not supported' Could you please tell me What is the next step ? – Jeff Bootsholz Jan 04 '16 at 05:41
  • I have to set the storyboard id in embedding navigation controller in xcode storybaord but it turns to [UINavigationController setCheckSumByte:]: unrecognized selector sent to instance ... – Jeff Bootsholz Jan 04 '16 at 07:33
  • HOw to pass data among view controllers afterwards ? – Jeff Bootsholz Jan 04 '16 at 07:59
  • And I am trying to use same navigation bar for all child view controllers – Jeff Bootsholz Jan 04 '16 at 08:05
  • Go through this. If you can't understand then ask http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers – DilumN Jan 04 '16 at 08:16