SWRevealViewController - manually switch to another view? - ios

I have a sidebar menu, made with SWRevealViewController.
One of menu elements in sidebar is "sign-in". It opens sign-in view (via custom SWRevealViewController segue).
I want to programmatically switch to another view after user successfully signs in. How do I do that?
UPDATE, my code:
ViewController *vc = [[ViewController alloc] init]; // this is main front ViewController
SWRevealViewController *revealController = self.revealViewController;
[revealController setFrontViewController:vc animated:YES];
After this I get just empty screen. What am I doing wrong? I feel that this is wrong:
ViewController *vc = [[ViewController alloc] init];
so what is correct way to access already loaded view controller? (it loads at app start by SWRevealViewController as front controller)

Try this:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
SecondViewController *rootViewController = [storyboard instantiateViewControllerWithIdentifier:#"SecondViewController"];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
[navController setViewControllers: #[rootViewController] animated: YES];
[self.revealViewController setFrontViewController:navController];
[self.revealViewController setFrontViewPosition: FrontViewPositionLeft animated: YES];
work for me :-)

Use
- (void)setFrontViewController:(UIViewController *)frontViewController animated:(BOOL)animated; method
SWRevealController has it's own property you can access from any UIViewController:
SWRevealViewController *revealController = self.revealViewController;
You can check example projects at it's GitHub repository:
https://github.com/John-Lluch/SWRevealViewController

Here is a Swift version of the setFrontViewController solution:
if let secondViewController = self.storyboard?.instantiateViewControllerWithIdentifier("SecondViewControllerStoryBoardID") as? SecondViewController {
let navController = UINavigationController(rootViewController: secondViewController)
navController.setViewControllers([secondViewController], animated:true)
self.revealViewController().setFrontViewController(navController, animated: true)
}

Try this:
ViewController *vc = [[ViewController alloc] init]; // this is main front ViewController
UINavigationController* navController = (UINavigationController*)self.revealViewController.frontViewController;
[navController setViewControllers: #[vc] animated: YES];
// [navController pushViewController:vc animated:YES]; // use this if you want to stack the views
[self.revealViewController setFrontViewPosition: FrontViewPositionLeft animated: YES];

There is a new API - (void)pushFrontViewController:(UIViewController *)frontViewController animated:(BOOL)animated;
As far as I know, this is the most elegant way that animates correctly to switch between view controllers manually.
// get the view controller you want to push to properly.
// in this case, I get it from Main.storyboard
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController * vc = [sb instantiateViewControllerWithIdentifier:#"SignInViewController"];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:vc];
[navController setViewControllers: #[vc] animated:NO];
[self.revealViewController pushFrontViewController:navController animated:YES];
If you are in the target vc that doesn't import SWRevealViewController and want to push back:
// get your vc properly
[self.navigationController setViewControllers:#[vc] animated:YES];
Please notice that [self.navigationController showViewController:vc sender:self]; has the same result, but don't use this since this creates a new view controller on the navigation stack, which is basically meaningless while using SWRevealViewController.

This one works for me perfectly
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
ViewController *rootViewController = [storyboard instantiateViewControllerWithIdentifier:#"ViewController"];
UINavigationController *navController = [storyboard instantiateViewControllerWithIdentifier:#"NavController"];
[navController setViewControllers: #[rootViewController] animated: YES];
[self.revealViewController setFrontViewController:navController];
[self.revealViewController setFrontViewPosition: FrontViewPositionLeft animated: YES];

#Evana has a good answer, however for my case which passing parameter to the destViewController, I need to improve it as below. By following her example exactly, I noticed the DestViewController viewDidLoad was called twice. First with 0 on the 'selectedId', only the second call did the 'selectedId' is receiving my tag value.
Thus, in order for a cleaner navigation, the DestViewController must be obtained from the navController, so that there is no redundant call.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UINavigationController *navController = [storyboard instantiateViewControllerWithIdentifier:#"DestNavController"];
DestViewController *destViewController = (DestViewController*)[[navController viewControllers] objectAtIndex:0];
destViewController.selectedId = ((UIButton*)sender).tag;
[self.revealViewController setFrontViewController:navController];

Related

How to passing data when using presentViewController with UITabbarController

I am a newbie in ios. I tried working with UITabbarController and I had some problem with it when I tried passing data from a ViewController (this ViewController is not an item in Tabbar) to an other ViewController that is item in Tabbar. In this case i wanted when I click button Login,"Welcome to shop" will appear and the username in Login form will pass into label in "Welcome to shop".
I use code below to make "welcom to shop" controller appearing:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UITabBarController *tabbarController= [storyboard instantiateViewControllerWithIdentifier: #"tabbarID"];
[self presentViewController: tabbarController animated: YES completion: nil];
So any idea for me to passing username (in LoginController) into label (WelcomeToShopController) ?
My problem resolved after i researched :). Because i used tabbar and navigationController so i should instance tabbar -> NavigationController -> ViewController. In my case, this code below will work perfectly.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UITabBarController *tabbarController= [storyboard instantiateViewControllerWithIdentifier: #"tabbarID"];
UINavigationController *nav = (UINavigationController *)[tabbarController.viewControllers firstObject];
HistoryOrderingController *historyOderingVC = (HistoryOrderingController *)[[nav viewControllers] firstObject];
historyOderingVC.user = self.user;
[self presentViewController: tabbarController animated: YES completion: nil];

Move to navigation controller from swrevealcontroller?

I have used SWRevealController in my app.On click of item in UITableView I want to go to a navigation controller so that I can switch between multiple view controllers.But as of now I am not able to redirect to navigation controller.I have used below code but it just replace the controller.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
MoreHelpController *rootViewController = [storyboard instantiateViewControllerWithIdentifier:#"HelpController"];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
[navController setViewControllers: #[rootViewController] animated: YES];
[self.revealViewController setFrontViewController:navController];
[self.revealViewController setFrontViewPosition: FrontViewPositionLeft animated: YES];
Please tell how can I directly go to navigation controller.It is very confusing here.
You just cannot move to navigation controller. Navigation Controller takes you to another view controller from one view controller.
If you want to move to another view controller on selection of table view row, then use didSelectRowAtIndexPath method of tableview and inside that use this:
yourViewController *obj = [self.storyboard instantiateViewControllerWithIdentifier:#"yourViewControllerIdentifier"];
[self.navigationController pushViewController:obj animated:YES];

Pushing view controller from AppDelegate

I am just beginning to learn how to implement Push notifications with iOS and Parse.
At the moment, I can send push notifications from the Parse dashboard to the app,the notification is received, as I can see in the Xcode console, but now my issue is that I am not able to open a view controller from the AppDelegate.
This is my code:
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))handler {
// Create empty photo object
NSString *photoId = [userInfo objectForKey:#"p"];
NSLog(#"P=%#",photoId);
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
Aceptado *targetViewController = (Aceptado*)[mainStoryboard instantiateViewControllerWithIdentifier:#"servicio_aceptado"];
UINavigationController *controlador = [[UINavigationController alloc]init];
[controlador pushViewController:targetViewController animated:YES];
}
As I said, the console show the log NSLog(#"P=%#",photoId) , the notification is received, but the view controller is not shown, any warning or error is shown.
Any help is welcome.
I have solved my problem like this:
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main"
bundle: nil];
Aceptado *controller = (Aceptado*)[mainStoryboard
instantiateViewControllerWithIdentifier: #"servicio_aceptado"];
controller.id_not =_id_not;
controller.idemployee = _idemployee;
controller.fullname = _fullname;
controller.phone = _phone;
controller.photo = _photo;
controller.dni = _dni;
controller.licenseplate = _licenseplate;
controller.model = _model;
controller.color = _color;
controller.latitude = _latitude;
controller.longitude = _longitude;
self.window.rootViewController = controller;
Now I can pass values to the target view controller from the AppDelegate file, keeping the SWRevealViewController structure.
Thank to all of you for your proposals.
try this code.
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
Aceptado *targetViewController = (Aceptado*)[mainStoryboard instantiateViewControllerWithIdentifier:#"servicio_aceptado"];
UINavigationController *navVc=(UINavigationController *) self.window.rootViewController;
[navVc pushViewController: targetViewController animated:YES];
may help you.
Try this once.
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
Slider1ViewController *viewcontroller = [storyBoard instantiateViewControllerWithIdentifier:#"Slider1ViewController"];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewcontroller];
[navController setViewControllers: #[viewcontroller] animated: YES];
[self.revealViewController setFrontViewController:navController];
[self.revealViewController setFrontViewPosition: FrontViewPositionLeft animated: YES];
You should not create new NavigationController, but use one existing. Usually it is rootViewController property of your key window.
Try this:
UINavigationController *controlador = self.window.rootViewController;
If you want to display your targetViewController in SWRevealViewController you may change your code as this:
SWRevealViewController *controlador = (SWRevealViewController)self.window.rootViewController;
[controloador pushFrontViewController:targetViewController animated:YES];
If you need display targetViewController over SWRevealViewController, use this:
WRevealViewController *controlador = (SWRevealViewController)self.window.rootViewController;
[controloador presentViewController:targetViewController
animated:YES
completion:nil];
If you need just replace SWRevealViewController with targetViewController use this:
self.window.rootviewController=targetViewController;
But don't forget save your SWRevealViewController in some another property.
Try this:
SomePageViewController *someVC = [self.storyboard instantiateViewControllerWithIdentifier:#"SomeVC"];
self.navController = [[UINavigationController alloc] initWithRootViewController:someVC];
self.window.rootViewController = self.navController;
[self.window makeKeyAndVisible];
If you want to push a view controller into navigation stack from appDelegate you need to cast your rootView controller as UINavigation Controller

performSegueWithIdentifier without storyboard idendentifier

I've a UITableViewController with dynamics rows, for every row, I need to open a new viewcontroller.
I've tried to use performSegueWithIdentifier, but it doesn't works because I don't have storyboard identifier.
How to open a new viewcontroller dynamically?
I've tried with:
newUiViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"controller"];
[self.navigationController pushViewController:controller animated:YES];
but it doesn't work.
You should give a controller identifier in your storyboard, check this:
Class is your "newUiViewController" and Storyboard ID "menu" is your "controller"
then you can do:
newUiViewController *controller = (newUiViewController *)[self.storyboard instantiateViewControllerWithIdentifier:#"controller"];
[self.navigationController pushViewController:controller animated:YES];
Ok, I've found the solution.
Like navcontroller I'm using "SWRevealViewController", so the correct code is:
UINavigationController* navController = (UINavigationController*)self.revealViewController.frontViewController;
[navController setViewControllers: #[list] animated: YES];
[self.revealViewController setFrontViewController:navController];
[self.revealViewController setFrontViewPosition: FrontViewPositionLeft animated: YES];
I think that is useful for other

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