display a SplitViewController from UIViewController in Objective-C - ios

I am developing a ViewController (login app) with a single button, when I press this button I want to appear my UISplitView like this:
- (IBAction)loadSplitViewController:(id)sender {
[self showSplitViewController];
}
and the code developed for the creation of my splitViewController is this:
-(void)showSplitViewController{
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPad" bundle: nil];
LeftViewController *leftViewController = [mainStoryboard instantiateViewControllerWithIdentifier:#"LeftViewController"];
RightViewController *rightViewController = [mainStoryboard instantiateViewControllerWithIdentifier:#"RightViewController"];
UINavigationController *leftNavController = [[UINavigationController alloc] initWithRootViewController:leftViewController];
UINavigationController *rightNavController = [[UINavigationController alloc] initWithRootViewController:rightViewController];
UISplitViewController *splitViewController = [[UISplitViewController alloc] init];
splitViewController.viewControllers = [NSArray arrayWithObjects:leftNavController, rightNavController, nil];
leftViewController.delegate = rightViewController;
splitViewController.delegate = rightViewController;
[self presentViewController:splitViewController animated:YES completion:^{}];
}
the thing is... if I use for display my splitViewController this line:
[self presentViewController:splitViewController animated:YES completion:^{}];
throws me an error
I also tried with
[self.view addSubview:splitViewController.view];
but this way my splitViewController never rotates, and delegates doesn't work as well... and I don't want my splitViewController to be a subview of my viewController, I want it to appear more like an independient modalView
any help I'll appreciate
thanks in advance

Split view controllers really should be the root view controller of the window (in fact Apple says it has to be, though there seem to be some examples where this isn't true). Instead of presenting it, you could just switch the window's root view controller to be the split view controller.
self.view.window.rootViewController = splitViewController;

Related

present a new ViewController but maintain the view stack

I'm trying to update an old IOS (objective-c) app to handle shortcutitems. When the user starts the app from a shortcut I would like to go the viewcontroller they've chosen, but would like them to be able to move back through the view hierarchy, to the original launch screen.
the current code in appdelegate didFinishLaunchingWithOptions looks like this:
UINavigationController *controller = (UINavigationController*)[storyboard instantiateInitialViewController];
self.window.rootViewController = controller;
return YES
And trying this:
UINavigationController *controller = (UINavigationController*)[storyboard instantiateInitialViewController];
self.window.rootViewController = controller;
...
if (fromShortCut) {
InfoViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"info"];
// init vc
vc.info = .....
[controller.navigationController pushViewController:vc animated:NO];
// or [controller pushViewController:vc animated:NO];
}
return YES
Shows the correct View, but there's no Back/Back-arrow or swipe gesture to allow the user to move back to the main screen.
Is this possible?
Many Thanks.
Here you go for your solution
if (fromShortCut) {
InfoViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"info"];
UIViewController *rootController = [UIApplication sharedApplication].keyWindow.rootViewController;
// init vc
vc.info = .....
[rootController.navigationController.navigationBar setHidden:NO];
[rootController.navigationController pushViewController:vc animated:NO];
// or [controller pushViewController:vc animated:NO];
}
else{
UINavigationController *controller = (UINavigationController*)[storyboard instantiateInitialViewController];
self.window.rootViewController = controller;
}
Yes. This is possible.
UINavigationController has a setViewControllers:animated: method that lets you do exactly that.
Pay attention to the documentation here:
viewControllers
The view controllers to place in the stack. The
front-to-back order of the controllers in this array represents the
new bottom-to-top order of the controllers in the navigation stack.
Thus, the last item added to the array becomes the top item of the
navigation stack.
You should create an array of UIViewController objects and the order should like this.. [firstVC, secondVC, ..., topMostVC]
EDIT:
The code should look something like this
UINavigationController *controller = (UINavigationController*)[storyboard instantiateInitialViewController];
self.window.rootViewController = controller;
if (fromShortCut) {
NSMutableArray *viewControllers = [[NSMutableArray alloc] init];
//create other viewonctollerObjeccts
FirstViewController *firstVC = storyboard instantiateViewControllerWithIdentifier:#"first"];
SecondViewController *secondVC = storyboard instantiateViewControllerWithIdentifier:#"second"];
ThirdViewController *thirdVC = storyboard instantiateViewControllerWithIdentifier:#"third"];
InfoViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"info"];
// init vc
vc.info = .....
[viewControllers insertObject:vc atIndex:0];
//finally set the array to nav stack
[controller setViewControllers:viewControllers animated:NO];
}
What this does is, it sets InfoViewController at the top of the navigation stack, and backs out to ThirdViewController which backs out to SecondViewController and so on.

How to push a new viewController with Transparent Background?

I wanted to Push New View Controller with Transparent Background on top of one View Controller which is already shown. I know How to PRESENT but I wanted to PUSH new View Controller.
UIViewController *controller = [[UIViewController alloc] init];
self.definesPresentationContext = YES;
controller.modalPresentationStyle = UIModalPresentationOverCurrentContext;
[self presentViewController:controller animated:YES completion:^{}];
You need to give memory to your ViewController and set to RootController of UINavigationController. After that, you can push it from your current controller...
UIViewController *yourViewController = [[UIViewController alloc] init];
UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:yourViewController];
[self presentViewController:navigationController1 animated:YES completion:nil];

How do I return back to RootView Controller from my last view controller?

I have 5 view controller and want to come back from the last view to 1st view controller
Call a function in which you have to change the app rootviewcontroller. set it as a navigationController
yourViewcontoller *viewController = [[yourViewContoller alloc] init];
UINavigationController *navController=[[UINavigationController alloc]initWithRootViewController:viewController];
self.window.rootViewController = navController;
If you're using storyboards, give the UINavigationController a Storyboard ID. You can then initiate it and present through the code:
UINavigationController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"<Storyboard ID>"];
[self presentViewController:vc animated:YES completion:nil];
Use this method on Animation splash screen
[self performSelector:#selector(setHidden:) withObject:nil afterDelay:2.0];
-(void) setHidden:(id)sender
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboare" bundle:nil];
YourVC* vController = (YourVC*)[Storyboard instantiateViewControllerWithIdentifier:#"storyboardId"];
UINavigationController *navcotoller=[[UINavigationController alloc]initWithRootViewController:vController];
self.window.rootViewController =navcotoller;
}
If you are using UINavigationController you could and should simply call
[self.navigationController popToRootViewControllerAnimated:YES];

Unable to present a view controller from AppDelegate

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.

iOS- navigate From AppDelegate to certain item in slidemenu

I use swrevealviewcontroller to add slide menu to my app ,
Let consider we have menu like this one in pic
I need navigate from My appDelegate to any item in menu (ex:Map View Controller )
my tries :
in my appDelegate.m
UIStoryboard *storyboard =[UIStoryboard storyboardWithName:#"Main" bundle:nil];
InforView *school_view = [storyboard instantiateViewControllerWithIdentifier:#"info_view"];
[self.window makeKeyAndVisible];
[self.window.rootViewController presentViewController:school_view animated:YES completion:NULL];
When its move to InforView Controller it crash in viewdidload
- (void)viewDidLoad
{
[super viewDidLoad];
_nav_bar.target = self.revealViewController;
_nav_bar.action = #selector(revealToggle:);
// its crash here
[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
}
my storyboard
navigation controller --> home view --> reveal View controller
reveal View controller has two views --> slide menu
navigation controller -- > Front view
my slide menu has some items as appear in pic
I need navigate from my appDelegate to one of this items
Finally I find the answer
UIStoryboard * storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
// this any item in list you want navigate to
Home_tableView *home = (Home_tableView *) [storyboard instantiateViewControllerWithIdentifier:#"home_view"];
SlideMenu *slidemenu = (SlideMenu *)[storyboard instantiateViewControllerWithIdentifier:#"Menu_view"];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:home];
UINavigationController *smVC = [[UINavigationController alloc]initWithRootViewController:slidemenu];
// define rear and frontviewcontroller
SWRevealViewController *revealController = [[SWRevealViewController alloc]initWithRearViewController:smVC frontViewController:nav];
// make it as root
self.window.rootViewController = revealController;
In the SidebarDemo project, you can use the following code to show MapViewController on initial loading.
SWRevealViewController *revealViewController = (SWRevealViewController*)self.window.rootViewController;
UIStoryboard *storyboard =[UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
MapViewController *mapVC = [storyboard instantiateViewControllerWithIdentifier:#"MapID"];
[self.window makeKeyAndVisible];
UINavigationController* navController = (UINavigationController*)revealViewController.frontViewController;
[navController setViewControllers: #[mapVC] animated: NO ];
[revealViewController setFrontViewPosition: FrontViewPositionLeft animated: YES];
After makeKeyAndVisible, the frontViewController and realViewController of the revealViewController are loaded. And you can set rootViewController of frontViewController, which is a navigationController.

Resources