iOS: SWRevealViewController revealToggle is been call but doesn't do nothing - ios

I'm trying to implement SWRevealViewController on this scenario:
From my main viewController:
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:YES];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
SWRevealViewController *SWR = [storyboard instantiateViewControllerWithIdentifier:#"SWRevealViewController"];
[self presentViewController:SWR animated:YES completion:nil];
}
From my green view controller:
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:YES];
self.reveal = [[SWRevealViewController alloc] init];
self.reveal.delegate = self;
self.menu.target = self;
self.menu.action = #selector(revealToggleAction:);
[self.view addGestureRecognizer:self.reveal.panGestureRecognizer];
NSLog(#"viewDidLoad");
}
-(void)revealToggleAction:(id)sender
{
[self.reveal revealToggle:self];
}
The revealToggle action is been call but doesn't do anything. It doesn't load the rear view controller. Any of you knows this happening or what I'm doing wrong?

Assuming that you are showing the side bar menu from right side.
First Embed your first view controller to navigation controller, then in your first view controller viewdidload() or viewDidAppear method add below mentioned code i.e.
//this is your side menu view controller.
UIViewController *sideMenuController =
[self.storyboard instantiateViewControllerWithIdentifier:#"YourSideMenuIdentifier"];
//this is the navigation controller embed to your green view controller.
UINavigationController *nc1 =
(UINavigationController *)[self.storyboard instantiateViewControllerWithIdentifier:#"YourNavigationControllerIdentifier"];
//This is your reveal view Controller.
SWRevealViewController *revealViewController =
[[SWRevealViewController alloc]initWithRightViewController:sideMenuController frontViewController:nc1];
[self.navigationController pushViewController:revealViewController animated:YES];
[self.navigationController setNavigationBarHidden:YES];
This will navigate to Controller i.e Green View Controller.
then in your green view controller viewdidload() or viewDidAppear method add below mentioned code i.e.
//GreenViewController.h file
#interface GreenViewController : UIViewController
#property (weak, nonatomic) IBOutlet UIBarButtonItem *sideBarItem;
#end
//Your GreenViewController.m file
_sideBarItem.target = self.revealViewController;
_sideBarItem.action = #selector(rightRevealToggle:);
SWRevealViewController *revealController = [self revealViewController];
[self.view addGestureRecognizer:revealController.panGestureRecognizer];
Note: For more details, check below mentioned link
http://www.appcoda.com/ios-programming-sidebar-navigation-menu/

You need set SWRevealViewController is the initial view controller.

You are creating a new instance of SWRevealViewController.
I think if you set the self.reveal to self.revealViewController it will work.

Related

How to present a view controller programmatically

I have a button inside my nib file. How can I programmatically present another view controller when the user taps on this button?
As I understood , you want to push new viewController on Button click . Please try this
- (IBAction)buttonClicked {
MyViewController *myViewController = [[MyViewController alloc]init];
[self presentViewController: myViewController animated:YES completion:nil];
}
First you need to create an action for your button, normally via Ctrl+drag. On that action implementation you should alloc the destination ViewController. The code would be something like this:
- (IBAction)buttonTapped {
MyViewController *modalViewController = [MyViewController new];
[self presentViewController:modalViewController animated:YES completion:nil];
}
Since you said that your file is a nib, I'm assuming that you aren't on a storyboard with both ViewControllers on it and the ViewController to be presented is code based. If this isn't the case, please elaborate your question.
As requested: Loading a VC from a storyboard
- (IBAction)buttonTapped {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MyStoryboard" bundle:nil];
RSSViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:#"RSSVC"];
// OR If is initial
// RSSViewController *viewController = [storyboard instantiateInitialViewController];
viewController.feedURL = #"...";
[self presentViewController:viewController animated:YES completion:nil];
}
If you need, ViewController identifier should be set in the Identity Inspector in the Storyboard.
Refer the below screenshots
First : control+drag the button
Second: Once you release the mouse,it shows you the box
Third:Click Connection Dropdown box.It shows you 3 option
Fourth:Choose or Select Action from the drop down.
Five : Finally give action name
now in ViewContoller.h the action method is
- (IBAction)actionGoNextPage:(id)sender;
Then in ViewContoller.m
#import "SecondViewController.h"
- (IBAction)actionGoNextPage:(id)sender
{
SecondViewController *secondVC = [[SecondViewController alloc]initWithNibName:#"SecondViewController" bundle:nil];
[self presentViewController:secondVC animated:YES completion:nil];
}

how to add New UIViewCOntroller Over UiView

I have added subview in a UIViewController and I want a new UIViewController Should be loaded on click of button.
Challenge is I am not able to use button in new UIViewController.
Code which I have write:
DetailsOfDayViewController *aViewController =[[DetailsOfDayViewController alloc] initWithNibName:#"DetailsOfDayViewController" bundle:nil];
aViewController.dateParsing = dateparsing;
[self addSubview:aViewController.view];
This will fix your issue. This is happening because your view controller is being deallocated when you don't have any strong pointers on it.
DetailsOfDayViewController *aViewController =[[DetailsOfDayViewController alloc] initWithNibName:#"DetailsOfDayViewController" bundle:nil];
aViewController.dateParsing = dateparsing;
[self addChildViewController:aViewController];
[self addSubview:aViewController.view];
Another fix will be connecting this view controller to a property:
#property(nonatomic,strong) DetailsOfDayViewController *aViewController;
And change your code like this:
self.aViewController = [[DetailsOfDayViewController alloc] initWithNibName:#"DetailsOfDayViewController" bundle:nil];
self.aViewController.dateParsing = dateparsing;
[self addSubview:self.aViewController.view];
For showing another controller you can following options
1. Add SubView
newViewControllerObj.view.frame = oldViewControllerObj(self).view.frame
[oldViewControllerObj(self).view.newViewControllerObj.view];
2. Present
[oldViewControllerObj(self) presentViewController: newViewControllerObj animated:YES completion:nil];
3. Push using Navigation (For this you already have to set navigation controller as root controller)
[oldViewControllerObj(self).navigationController pushViewController:newViewControllerObj animated:YES];

iOS: How to switch from one UIViewController to another, including in SWRevealViewController

I have an example of SWRevealViewController from github.
And now I'm tryng to understand, how switch my independent UIViewController to another, which is including in SWRevealViewController.
I read somewhere that this should work:
- (IBAction)action:(UIButton *)sender {
SWRevealViewController *swcontroller = self.revealViewController;
if (swcontroller) {
self.revealViewController.frontViewController = [[UIViewController alloc] init];
}
}
and I create a modal segue from my UIButton to SWRevealViewController.
But it does not work.
Can anybody give an example?
As per my information you need to manually push or pop your view controller in SWRevealViewController.As shown below try this..
- (IBAction)btnActionSalesQuotation:(id)sender{
SWRevealViewController *revealview = self.revealViewController;
SalesQuotationsViewController *Gosalequo=[[SalesQuotationsViewController alloc]initWithNibName:#"SalesQuotationsViewController" bundle:nil];
[revealview pushFrontViewController:Gosalequo animated:YES];
}
That code helped me:
- (IBAction)act:(UIButton *)sender {
SWRevealViewController *sw = [self.storyboard instantiateViewControllerWithIdentifier:#"SWRevealViewController"];
UINavigationController *nav = [self.storyboard instantiateViewControllerWithIdentifier:#"NavigationController"];
[sw setRearViewController:nav];
sw.frontViewController = nav;
[self presentViewController:sw animated:NO completion:nil];
}
I forgot to add setRearViewController

FirstViewController is not working when Second ViewController is removed from its superview

I'm using a UIStoryBoard to allow the FirstViewController to add the view of second ViewController as a subview. On removing the sub view using the following method
FirstViewController.m
- (IBAction) btnMoveTo:(id)sender
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"Second"];
vc.view.backgroundColor = [UIColor clearColor];
self.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentModalViewController:vc animated:NO];
}
SecondViewController.m
-(void)viewDidLoad{
self.view.opaque = YES;
self.view.backgroundColor = [UIColor clearColor];
}
- (IBAction) withDraw:(id)sender{
[self.view removeWithZoomOutAnimation:2 option:nil];
}
When I access the withDraw Function, the view of the SecondViewController is removed and firstViewController is back. However when I use the button to access the - (IBAction) btnMoveTo:(id)sender function. It doesn't work. Nothing really happens. Any suggestions or help would be greatly appreciated.
You are confusing views with view controllers. View controllers are objects that control views, so for the first line you wrote,
I'm using a UIStoryBoard to allow the FirstViewController to add the view of second ViewController as a subview. On removing the sub view using the following method
you cant add a view controller as a subview of another view controller. You present a view controller that manages its own views, and you can add UIViews as subviews of a UIViewController. A UIViewController manages UIViews.
The problem you are having with the code you posted, is that when you trigger the withDraw: function, you are removing the view of the view controller. Not the view controller itself. You need to dismiss the SecondViewController to gain access of the FirstViewController again.

push a view controller from a UIView ios

I have a navigation based application.On click of a button on the navigation bar in the first screen , I am able to push another view controller as follows :
-(void) buttonClicked:(id)sender
{
UIViewController* mv = [[SecondViewController alloc] init];
[[self navigationController] pushViewController:mv animated:YES];
}
Now i have a UIView(separate .h and .m files) as part of the first screen. On click of a button in the UIView, i want to push the SecondViewController.
I have tried the following :
UIViewController* mv = [[SecondViewController alloc] init];
UIViewController * home=[[FirstViewController alloc]init];
[[home navigationController] pushViewController:mv animated:YES];
It doesnt work!! Kindly help
UIViewController* mv = [[SecondViewController alloc] init];
UIViewController * home=[[FirstViewController alloc]init];
[[home navigationController] pushViewController:mv animated:YES];
The problem here is that home isn't part of the navigation stack, so [home navigationController] is surely nil. I'm not quite clear on what you're trying to do here, but just creating a view controller doesn't mean that it's actually part of the view controller graph.
Why would it work? Randomly creating view controllers whose view is not even visible, is not the solution. You can either keep a reference to the VC in the view like this:
#imlementation ViewController
- (id) init
{
// ...
aView = [[CustomView alloc] init];
aView.viewController = self;
// ...
}
#end
#interface CustomView
#property (assign) ViewController *viewController;
#end
Or you can search the responder chain at runtime:
UIResponder *next = [view nextResponder];
while (next)
{
if ([next isKindOfClass:[ViewController class]])
{
break;
}
next = [next nextResponder];
}
And now "next" will contain the view controller (or nil if it can't be found).
Try using the same navigationController to push view, this keeps the same stack of ViewControllers.
UIViewController* mv = [[SecondViewController alloc] init];
[[self navigationController] pushViewController:mv animated:YES];
[mv release];
I see your problem now! You need to #import your FirstViewController, then #class it. Then do your push.
So:
//.h
#import "FirstViewContoller.h"
#class FirstViewController;
#interface...
//.m
-(void)return {
FirstViewController *firstview = [[FirstViewController alloc]init(withnibname:)];
[firstView.navigationController pushViewController: firstView.navigationController.topViewController animated: TRUE];
}
If I am not wrong, your UIView though is in separate files, is still added to the screen from a UIViewController class.
Simply, post a notification from UIView to your FirstViewController class where you have access to the navigation controller. Then push the SecondViewController from there.
You Can use this. It Works very well for me:-
Firstly Create Object of AppDelegate in UIView Class and initialize it. Then create Navigationcontroller object in Appdelegate.h :-
#property(strong,nonatomic) UINavigationController *navControl;
In your UIView Class implement this code where you want to push :-
ViewController *objview = [[ViewController alloc]init]; [appDelegate.navControl pushViewController:objview animated:YES];

Resources