I added a new UIViewController to get a log in issue fixed and now the button in the NavigationBar on the next scene are inactive. Here is the code I used to load the new StoryBoard:
-(void)newSotrybooard{
UIStoryboard *settingsStoryboard = [UIStoryboard storyboardWithName:#"LogedIn" bundle:nil];
UIViewController *initialSettingsVC = [settingsStoryboard instantiateInitialViewController];
initialSettingsVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:initialSettingsVC animated:YES completion:NULL];
}
I added the new storyboard after I couldn't get the NavigationBar to show up and push without and error on the main storyboard. This is the only code that has changed. The Navigation Controller does not have a class associated with it and neither does the TableView to follow. I can also not scroll the Table view. Thank you for your help!
Update:
I updated the code above and
The error I receive is:
Warning: Attempt to present <UINavigationController: 0x109fa0ce0> on <PrivateViewController: 0x109f6ef80> whose view is not in the window hierarchy!
Based on the information you have provided, I surmise that the following could be helpful -
Yes, you have used instantiateViewControllerWithIdentifier.
But, remember it instantiates and returns the view controller with
the specified identifier. You're missing that.
Provide the same name to your new Storyboard in your code and
File Inspector (like you named it LogedIn above) and it
should be set in the Main Interface (Target >> General >> Main
Interface).
See rootViewController? The rootViewController
for the window needs to be assigned (either via Storyboard or
programmatically). Please check. I have added a UINavigationController in my example below -
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"StoryboardName" bundle:nil];
ViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"StoryboardName"];
vc.title = #"Is this title visible?";
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
self.window.rootViewController = nav;
return YES;
}
For the ViewController.m
-(void) goToSettings {
UIStoryboard *settingsStoryboard = [UIStoryboard storyboardWithName:#"SettingsStoryboard" bundle:nil];
UIViewController *initialSettingsVC = [settingsStoryboard instantiateInitialViewController];
initialSettingsVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:initialSettingsVC animated:YES completion:NULL];
}
Related
I have created storyboard which I want to open on top of another view or in childView so that when I close or destroy this view of storyboard the earlier view on which the storyboard is opened remains the same.
When I run the following code:-
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main_iPad" bundle:nil];
ViewController *detailViewController = [storyboard instantiateViewControllerWithIdentifier:#"neolick"];
[[UIApplication sharedApplication].keyWindow setRootViewController:detailViewController];
First, the storyboard opens but I don't know whether it opens on top of previous view or it destroys the previous view & then open.
Second, the functions needed on that storyboard runs automatically which is as I want but how these things are working.
If anyone can help me understand the above code and its working.
NOTE: I cannot call the earlier view again in same state because of some reason.
Thanks in Advance!!
Here you are setting root view controller
it will not keep your back screen as it is what you want
If you want to keep current screen and show other screen on that
you have two approaches
1) Present ViewController
2) Push View Controller
1) Present ViewController
for this you can present your screen on top of other screen which is visible
for example
- (IBAction)btnNextTapped:(id)sender {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main_iPad" bundle:nil];
ViewController *detailViewController = [storyboard instantiateViewControllerWithIdentifier:#"neolick"];
[self presentViewController:detailViewController animated:true completion:nil]
}
2) Push View Controller
For that you need NavigationController and need to push your ViewController from current visible screen
i.e
[self.navigationController pushViewController:vc animated:true];
EDIT
as per discussion you need to find current top view controller then you should present it
Add this method below your method
- (UIViewController*) topMostController
{
UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
while (topController.presentedViewController) {
topController = topController.presentedViewController;
}
return topController;
}
And Replace this method with code
- (void)goToNewPage:(CDVInvokedUrlCommand*)command
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main_iPad" bundle:nil];
ViewController *detailViewController = [storyboard instantiateViewControllerWithIdentifier:#"neolick"];
[[self topMostController] presentViewController:detailViewController animated:true completion:nil];
}
I need to present a view controller from app delegate.
When a phone notification comes in, I am able to decide which one of 3 view controllers (named ForumViewController, BlogViewController & NewsViewController) should be presented by analyzing the 'userInfo' in the method 'didReceiveRemoteNotification'.
But when i try to present the appropriate view controller using storyboards or the code below:
self.viewController = [[MembersViewController alloc] initWithNibName:#"MembersViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
Then, the app gives the error 'Warning: Attempt to present whose view is not in the window hierarchy!'. Also it gets stuck on a particular view controller.
Please keep in mind that the view controllers that I am trying to present are not part of the flow when the app starts (the flow is LogoViewController -> SplashViewController -> HomeViewController).
The HomeViewController & MembersViewController are essentially the main menu pages for public & private viewing. Here I have to display something to the viewer.
choice-1
using push
UINavigationController *navController = (UINavigationController *)self.window.rootViewController;
MembersViewController *vc = [navController.storyboard instantiateViewControllerWithIdentifier:#"MembersViewController"];
[navController pushViewController:vc animated:YES];
using present
MembersViewController *root = (MembersViewController *)self.window.rootViewController;
UIViewController *vc = [root.storyboard instantiateViewControllerWithIdentifier:#"MembersViewController"];
[root presentViewController:vc animated:YES completion:NULL];
upadted
UIStoryboard *mainstoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
MembersViewController* pvc = [mainstoryboard instantiateViewControllerWithIdentifier:#"MembersViewController"];
[self.window.rootViewController presentViewController:pvc animated:YES completion:NULL];
Loading a view controller from the storyboard:
[self performSelector: #selector(ShowModalViewController) withObject: nil afterDelay: 0];
-(void)ShowModalViewController{
NSString * storyboardName = #"MainStoryboard";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:#"IDENTIFIER_OF_YOUR_VIEWCONTROLLER"];
[self.window.rootViewController presentViewController:vc animated:YES completion:nil];
}
Identifier of your view controller is either equal to the class name of your view controller, or a Storyboard ID that you can assign in the identity inspector of your storyboard.
We have an error with our iOS game. The gameover screen (containing the score) doesn't come up after losing the game, as it just hangs up. Can anyone suggest what could be the problem and how can we fix this?
We are currently testing and we are not sure if this is the case because of the code or the test settings? But so far the Xcode doesn't show any errors.
Thanks for your help.
Open a view from a normal class:
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:storyBoardName bundle:nil];
UIViewController *viewController = [storyBoard instantiateViewControllerWithIdentifier:StoryboardID];
[self presentViewController:viewController animated:NO completion:nil];
Edit these strings to:
storyBoardName = The name of your storyboard.
storyBoardID = The StoryBoardID of your ViewController.
Open a view from the AppDelegate
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:storyBoardName bundle:nil];
UIViewController *viewController = [storyBoard instantiateViewControllerWithIdentifier:storyBoardID];
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];
Edit these strings to:
storyBoardName = The name of your storyboard.
storyBoardID = The StoryBoardID of your ViewController.
Other information
How to get the storyBoardName?
If your StoryBoard is called Main.storyboard Main is the title.
If your StoryBoard is called MyName.storyboard MyName is the title
How to set a StoryBoardID?
Select your ViewController. Your View must now be highlighted blue. Go to the identity inspector and under identity you'll find StoryBoardID. Set it to whatever you like.
If it still doesn't work when you use this code you have put in the wrong variables, or there's an unknown error.
I recently work on a tutorial for my app. I created the tutorial with this Tutorial:
http://www.appcoda.com/uipageviewcontroller-tutorial-intro/
I created a "button" which brings the user back to the rootViewController, in this case the TabBarController. Problem is: with this tutorial I made a extra storyboard for the tutorial. So how can I go back to the original rootViewController(TabBarController) with the button?
Code:
- (IBAction)start:(id)sender {
UIViewController* backToRootViewController = [[UIViewController alloc] initWithNibName:#"TabBarController" bundle:[NSBundle mainBundle]];
[self.view addSubview:backToRootViewController.view];
}
This does not work, too
- (IBAction)start:(id)sender {
[self.navigationController popToRootViewControllerAnimated:YES];
}
EDIT
To open the tutorial at the first start:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
BOOL isAccepted = [standardUserDefaults boolForKey:#"iHaveAcceptedTheTerms"];
if (!isAccepted) {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[APPViewController alloc] initWithNibName:#"APPViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
}
APPViewController is the Tutorial
EDIT
After the help of johnnelm9r the current code is this:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"main" bundle: nil];
IntroViewController *introViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"IntroViewController"];
BOOL isAccepted = [standardUserDefaults boolForKey:#"iHaveAcceptedTheTerms"];
if (!isAccepted) {
[self.window.rootViewController presentViewController:introViewController animated:NO completion:nil];
}
But now, sadly, the app crashed and the error is:
Application tried to present a nil modal view controller on target <UITabBarController: 0x175409d0>.'
and also a warning: Incompatible pointer type assigning to 'ViewController' from 'UIViewController' in AppDelegate
*****UPDATE****
I uploaded a sample project to demonstrate what I mean. You can find it here: github link
Good luck!
**** EDIT
After talking with you more I'd suggest trying to use something similar to this code in your viewDidAppear method of whatever view controller runs the first tab of your tabbarcontroller (obviously you want your own class name where I have IntroViewController):
BOOL didShowIntro = [[NSUserDefaults standardUserDefaults] boolForKey:#"showIntro"];
IntroViewController *introViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"IntroViewController"];
if (didShowIntro)
{
[self presentViewController:introViewController animated:NO completion:nil];
}
and then just pop the presented controller from your button in the tutorial like so:
[self dismissViewControllerAnimated:NO completion:nil];
Just remember to set your user defaults to no when you press the button in your tutorial view controller. And make sure you are going from tabbarcontroller to navigationcontroller as a relationship segue and then to the view controller you want to show after the tutorial as a root. Just to be clear: there should be no connection to the tutorial view controller on your storyboard.
Just to be extra clear: your storyboard should have the navbarcontroller as your initial view controller connected to a navigationcontroller connected to a view controller and another view controller not connected to anything. :) And make sure you delete the code in the delegate.
To go to rootViewController
[self.navigationController popToRootViewControllerAnimated:YES];
I have generated a new Tabbed Application using storyboards.
So far I have
TabBarController
-> FirstViewController
-> SecondViewController
-> ModalViewController
I'm trying to open the modal view before showing the tabBarController. I added the following code on the AppDelegate.m
showModalView is called from application:didFinishLaunchingWithOptions:;
- (void)showModalView
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
GSLoginViewController *loginView = [storyboard instantiateViewControllerWithIdentifier:#"loginView"];
[loginView setModalPresentationStyle:UIModalPresentationFullScreen];
[self.window.rootViewController presentViewController:loginView animated:YES completion:NULL];
}
And here the output I have:
Warning: Attempt to present <ModalViewController: 0x93670d0> on
<UITabBarController: 0x935d170> whose view is not in the window hierarchy!
You are getting this because your Appdelegate don't know that tabbarcontroller is your root view.You should try something like this.
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
and add your code accordingly.The thing is that you should let the app delegate know tabbarcontroller is the rootviewcontroller.