Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Change viewcontroller on tabbarcontroller

Trisect DevelopmentTrisect Development DenmarkPosts: 285Registered Users @ @
I have an App with a TabBarController and on one of my tabs I want to change the viewcontroller to show another view.
I do itlike this.

NewViewController *newViewController;
[self presentViewController:newViewController animated:YES completion:nil];
When I call this code I get this error:
Error=Application tried to present a nil modal view controller on target .
I can easily change the view if I put a button on my TabBarView and make a segue in storyboard, but I want to change view in code.
Can anybody help me with my problem. Some code example would be nice.

Replies

  • Duncan CDuncan C Posts: 9,029Tutorial Authors, Registered Users @ @ @ @ @ @ @

    I have an App with a TabBarController and on one of my tabs I want to change the viewcontroller to show another view.
    I do itlike this.


    NewViewController *newViewController;
    [self presentViewController:newViewController animated:YES completion:nil];
    When I call this code I get this error:
    Error=Application tried to present a nil modal view controller on target .
    I can easily change the view if I put a button on my TabBarView and make a segue in storyboard, but I want to change view in code.
    Can anybody help me with my problem. Some code example would be nice.
    Look at your code. You've posted 2 lines. Let's look at them one at a time:
    NewViewController *newViewController;
    What does that code do? Well, it defines an instance variable of type NewViewController. Does it create an instance of a NewViewController object?

    If you add a debug statement after that line, what value is stored in your variable?
    [self presentViewController:newViewController animated:YES completion:nil];
    Ok in your next line you try to present newViewController as a modal. That will only work if newViewController actually contains an instance of a NewViewController, and THAT will only be true if you execute code that creates a NewViewController. You didn't, so it doesn't.

    You need an alloc/init call, or if you are using storyboards, you need to load your view controller from the storyboard. Take a look at the instance methods in UIStoryBoard and see if any of them will create a new view controller for you.
    Regards,
    Duncan C
    WareTo

    widehead.gif
    Animated GIF created with Face Dancer, available for free in the app store.

    I'm available for one-on-one help at CodeMentor
  • Trisect DevelopmentTrisect Development DenmarkPosts: 285Registered Users @ @
    edited February 2013
    Okay I useally do it like this.

    NewViewController *newViewController = [[NewViewController alloc] init];
    [self presentViewController:newViewController animated:YES completion:nil];
    I don't know why I did not see that.
    But I stil get this error:
    I put in '

    2013-02-21 16:00:46.509 M.I.L[586:c07] Warning: Attempt to present '<'NewViewController: 0xa3a64f0'>' on '<'UITabBarController: 0xa572c00'>' whose view is not in the window hierarchy!
    Why is that?


    Post edited by Trisect Development on
  • Duncan CDuncan C Posts: 9,029Tutorial Authors, Registered Users @ @ @ @ @ @ @

    Okay I useally do it like this.


    NewViewController *newViewController = [[NewViewController alloc] init];
    [self presentViewController:newViewController animated:YES completion:nil];
    I don't know why I did not see that.
    But I stil get this error:
    I put in '

    2013-02-21 16:00:46.509 M.I.L[586:c07] Warning: Attempt to present '<'NewViewController: 0xa3a64f0'>' on '<'UITabBarController: 0xa572c00'>' whose view is not in the window hierarchy!
    Why is that?



    You almost never use init to create a view controller. Instead, you should use either initWithNibName:bundle: (to create the view controller from an XIB file) or the storyboard method instantiateViewControllerWithIdentifier if you are loading a view controller from a storyboard file, like it sounds like you are doing.

    However, the error message sounds to me like the tab bar controller from which you are trying to present the new view controller is not on-screen.

    You need to give more background. How is the tab bar controller being created? Where is the code you are posting being executed? What invokes it? (post the whole method, as well as describing how that method is being invoked.)

    Regards,
    Duncan C
    WareTo

    widehead.gif
    Animated GIF created with Face Dancer, available for free in the app store.

    I'm available for one-on-one help at CodeMentor
  • GHuebnerGHuebner Posts: 665Registered Users @ @ @
    If you are using storyboards and your view controller is in your storyboard that you want to present modally, then create a modal segue from your View to the ViewController. Not from a button but from the view itself. Name the segue something. i.e. 'segueToModal'

    Then in your code when you want to present your new view controller just call,

    [self performSegueWithIdentifier:@"segueToModal" sender:self];
    In your PrepareForSegue Method, check the segue identifier and do any setup needed

    if ([segue.identifier isEqualToString:@"segueToModal"])
    {
    NewVewController *destination = (NewViewController* )segue.destinationViewController;
    // Perform any setup necessary on destination
    }
  • Duncan CDuncan C Posts: 9,029Tutorial Authors, Registered Users @ @ @ @ @ @ @
    GHuebner said:

    If you are using storyboards and your view controller is in your storyboard that you want to present modally, then create a modal segue from your View to the ViewController. Not from a button but from the view itself. Name the segue something. i.e. 'segueToModal'

    Then in your code when you want to present your new view controller just call,


    [self performSegueWithIdentifier:@"segueToModal" sender:self];
    In your PrepareForSegue Method, check the segue identifier and do any setup needed

    if ([segue.identifier isEqualToString:@"segueToModal"])
    {
    NewVewController *destination = (NewViewController* )segue.destinationViewController;
    // Perform any setup necessary on destination
    }
    What he said. :)
    Regards,
    Duncan C
    WareTo

    widehead.gif
    Animated GIF created with Face Dancer, available for free in the app store.

    I'm available for one-on-one help at CodeMentor
  • Trisect DevelopmentTrisect Development DenmarkPosts: 285Registered Users @ @
    Thanks, thats working.
    I did have [self performSegueWithIdentifier:@"segueToModal" sender:self]; in my ViewDidLoad method but that gave me an error.
    I then moved it to ViewDidAppear and its working now.


  • GHuebnerGHuebner Posts: 665Registered Users @ @ @
    Now once you dismiss your modal view, wont it be called again because you will be executing ViewDidAppear when you return? That does not seem right.
  • Trisect DevelopmentTrisect Development DenmarkPosts: 285Registered Users @ @
    I just put in an variable my app delegate that gets set in the viewcontroller I open.
    I then check this variable in my ViewDidAppear method.
    This should work, I have not yet implemented it in my code.
  • GHuebnerGHuebner Posts: 665Registered Users @ @ @
    As long as the variable gets set after the modal is displayed and you dont want it displayed again. Since you didnt post code, I was just indicating that the logic you had would continually present the modal controller.
Sign In or Register to comment.