Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.
Change viewcontroller on tabbarcontroller
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.
0 •
Replies
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? 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.
Duncan C
WareTo
Animated GIF created with Face Dancer, available for free in the app store.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeBut I stil get this error:
I put in ' Why is that?
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeYou 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.)
Duncan C
WareTo
Animated GIF created with Face Dancer, available for free in the app store.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeThen in your code when you want to present your new view controller just call, In your PrepareForSegue Method, check the segue identifier and do any setup needed
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeDuncan C
WareTo
Animated GIF created with Face Dancer, available for free in the app store.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeI 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.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeI then check this variable in my ViewDidAppear method.
This should work, I have not yet implemented it in my code.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome