In my xcode project, I want a UIButton to open a right slide-out menu using SWRevealViewController but it isn't working. Any help?
He's the code in my view didLoad for the UIButton:
SWRevealViewController *revealViewController = self.revealViewController;
if ( revealViewController )
{
[sidebarButton addTarget:revealViewController action:#selector(rightRevealToggle:) forControlEvents:UIControlEventTouchUpInside];
[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
}
And here is the IBAction for the button:
- (IBAction)reveal:(id)sender {
SWRevealViewController *revealViewController = self.revealViewController;
[self revealViewController];
}
I am using storyboard in xcode.
Ok, so I fixed the issue. In my storyboard file instead of "sw_rear" segue identifier I put "sw_right" and that was it.
Related
I have done some demo project for the side menu.in that i have 3 vc. i am using right side menu to show the side menu view. Here, from vc1 to vc 2 push. And in vc2 i have one button to open right side menu. But here, when i click first time its not working. After second click its not working.
Sam if i go back and come again. First click its not working. After second click its working fine. Any idea why its happening ?.
My demo project : here
I added uibutton action like below.
// from v1 to v3 vc
- (IBAction)tap:(id)sender {
NSString * storyboardName = #"Main";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
VC3 * vc = [storyboard instantiateViewControllerWithIdentifier:#"VC3"];
[self.navigationController pushViewController:vc animated:YES];
}
to show menu :
- (IBAction)DASD:(id)sender {
[self.btn addTarget:self.revealViewController action:#selector(rightRevealToggle:) forControlEvents:UIControlEventTouchUpInside];
}
Make you button target action in ViewDidLoad. Don't create an ibAction to trigger the side menu.
- (void)viewDidLoad {
[super viewDidLoad];
SWRevealViewController *revealViewController = self.revealViewController;
if ( revealViewController )
{
[self.yourButton addTarget:self.revealViewController action:#selector(rightRevealToggle:) forControlEvents:UIControlEventTouchUpInside];
}
}
i have checked your code. you have to do just one change.
[self.btn addTarget:self.revealViewController action:#selector(rightRevealToggle:) forControlEvents:UIControlEventTouchUpInside];
remove above code from the - (IBAction)DASD:(id)sender and add in viewDidLoad Method of VC3.m file.
so your code of VC3.m file is looks like below way :-
- (void)viewDidLoad {
[super viewDidLoad];
[self.btn addTarget:self.revealViewController action:#selector(rightRevealToggle:) forControlEvents:UIControlEventTouchUpInside];
}
- (IBAction)DASD:(id)sender {}
I am trying to use the SWRevealViewController without a storyboard and with a UIButton. My project has two UIViewControllers with nib's. The main one is called ViewController and the other is called MenuViewController.
In my ViewController I have the following code:
- (void)viewDidLoad {
[super viewDidLoad];
MenuViewController *menuView;
SWRevealViewController *revealViewController = [[SWRevealViewController alloc] initWithRearViewController:menuView frontViewController:self];
if ( revealViewController )
{
[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
}
}
- (IBAction)toggleMenu:(id)sender {
NSLog(#"Toggling menu");
SWRevealViewController *revealController = self.revealViewController;
[revealController rightRevealToggleAnimated:YES];
}
The toggle menu button prints "Toggling Menu" as expected but doesn't reveal the menu controller. I know this isn't the conventional way of using the SWRevealViewController but does someone have any ideas how to get this to work ?
Add below line in viewDidLoad()
self.revealViewController.delegate = self;
Here are two methods I use to set up the two (left and right) view controllers for SWRevealViewController. Just call them in viewDidLoad.
-(void) initSlideMenu
{
SWRevealViewController * menuViewController = (SWRevealViewController *)self.revealViewController;
if (menuViewController)
{
[_menuButton setTarget:self.revealViewController];
[_menuButton setAction:#selector(revealToggle:)];
[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
}
}
-(void) initRightMenu
{
SWRevealViewController * rightViewController = (SWRevealViewController *)self.revealViewController;
if (rightViewController)
{
rightViewController.rightViewRevealOverdraw =0.0f;
[_notificationButton setTarget:self.revealViewController];
[_notificationButton setAction:#selector(rightRevealToggle:)];
[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
}
}
You can try this one with some modifications. It helped in my case.
- (IBAction)toggleMenu:(id)sender{
[self.btn addTarget:self.revealViewController action:#selector(revealToggle:) forControlEvents:UIControlEventTouchUpInside];
}
in my application create sliding menu using SWRevealViewController
when i click on uiviewcontroller sliding menu not close automatically if view controller contain tableview on other click event
how can i solved this problem
my code is :
SWRevealViewController *revealController = [self revealViewController];
[self.view addGestureRecognizer:revealController.panGestureRecognizer];
Write this code on your green view viewdidload method to navigate to another view.
revealController=[[SWRevealViewController alloc]init];
revealController = [self revealViewController];
[self.view addGestureRecognizer:revealController.panGestureRecognizer];
revealController.delegate=self;
[revealController panGestureRecognizer];
[revealController tapGestureRecognizer];
I solve it using this :-
SWRevealViewController *revealController = [self revealViewController];
[revealController panGestureRecognizer];
[revealController tapGestureRecognizer];
[self.menueBtn addTarget:revealController action:#selector(revealToggle:) forControlEvents:UIControlEventTouchUpInside];
selector (revealToggle:)call SWRevealViewController class
I have a problem.
I write code in button touch event. Run project and click button. Then i get a black screen.
- (IBAction)ff:(id)sender {
SecondViewController *viewController = [[SecondViewController alloc] init];
[self presentViewController:viewController animated:YES completion:nil];
}
1) Add button in XIB or Storyboard.
2) Create a button in your viewController.h file
__weak IBOutlet UIButton *yourButton;
3) In the same file, add selector of the button:
-(IBAction)yourButton_pressed:(id)sender;
4) Go to XIB or Storyboard, open Connection Inspector, Drag and drop from yourButton to the button added in XIB or Storyboard.
5) Same way, Drag and drop from yourButton_pressed to the button added in XIB or Storyboard. It will ask you for the action type like TouchUpInside, TouchUpOutside. Select TouchUpInside for now.
6) Present modal view controller:
YourViewController *viewController = [[YourViewController alloc]initWithNibName:#"YourView" bundle:nil];
viewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:viewController animated:YES];
[viewController release];
7) Enjoy and Upvote :D
Just select the Button from the MainStoryboard and hold CTRL and drag it to the view controller and select present modally.
And remove the IBAction from the implementation and remove the sent events from the MainStoryboard for that button.
If you want to go to SecondViewController, you can always use prepareForSegue method.
But first make a connection, see screenshots here: http://imgur.com/a/CJFwy
Then add this code to your viewController:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"ShowSecondVC"]) {
SecondViewController *secondVC = [segue destinationViewController];
}
}
I'm trying to transition our app to use SWRevealViewController to give us a side-bar on each side of the application. But despite following the code in one of the example apps, I'm getting an error where the ViewController for the front view doesn't work properly. viewDidLoad gets called, but everything remains black.
Interestingly, if in my viewDidLoad, I set the background colour to red of the view, this is reflected. But stuff in Interface builder from the original story board is not.
The code I use in the AppDelegate is:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window = window;
MainViewController *frontViewController = [[MainViewController alloc] init];
RearViewController *rearViewController = [[RearViewController alloc] init];
UINavigationController *frontNavigationController = [[UINavigationController alloc] initWithRootViewController:frontViewController];
UINavigationController *rearNavigationController = [[UINavigationController alloc] initWithRootViewController:rearViewController];
SWRevealViewController *revealController = [[SWRevealViewController alloc] initWithRearViewController:rearNavigationController frontViewController:frontNavigationController];
revealController.delegate = self;
RightViewController *rightViewController = rightViewController = [[RightViewController alloc] init];
rightViewController.view.backgroundColor = [UIColor greenColor];
revealController.rightViewController = rightViewController;
//revealController.bounceBackOnOverdraw=NO;
//revealController.stableDragOnOverdraw=YES;
self.viewController = revealController;
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
#pragma mark - SWRevealViewDelegate
- (id <UIViewControllerAnimatedTransitioning>)revealController:(SWRevealViewController *)revealController animationControllerForOperation:(SWRevealControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC
{
if ( operation != SWRevealControllerOperationReplaceRightController )
return nil;
if ( [toVC isKindOfClass:[RightViewController class]] )
{
if ( [(RightViewController*)toVC wantsCustomAnimation] )
{
id<UIViewControllerAnimatedTransitioning> animationController = [[CustomAnimationController alloc] init];
return animationController;
}
}
return nil;
}
This is the Main.Storyboard which is just for the MainViewController:
When the app loads, the view is just black. But I can drag from both the left and right edge and view the side bars as would be expected. So it's only the FrontView that is coming up black. I stuck an NSLog() into the ViewDidLoad, which appears in the console, as does one in -(void)loadView{}, which shows the View is loading.
If I put into the viewDidLoad a [self.view setBackgroundColor:[UIColor redColor]] this takes effect, meaning it is linked to the view, but it's just the view inside the storyboard is not appearing. Which is weird since the Storyboard also contains a NavigationController, which does appear (I think - unless that navigation controller is coming from somewhere else - which I'm pretty sure it isn't).
Any thoughts on what might be causing this?
I had the same thing going on. I fixed it while looking on the Example code.
In your Storyboard add a UIViewController
Select it and in Identity Inspector just use 'SWRevealViewController' as class
Add a UITableViewController to your story board
Now select your previously added ViewControler and right-click-draw a line to your TableViewController
Select 'reveal view controller set controller'
Click on the newly added Segue and in Attribute Inspector change the identifier to 'sw_rear'
Add any custom ViewController (for example 'MyViewController') to the story board
Select it, then go to the Menu->Editor->Embed In->Navigation Controller
Now a new Navigation Controller should appear
Again right-click-draw a line from the first ViewController to your new NavigationController
Again choose 'reveal view controller set controller'
Now set the identifier of this new Segue to 'sw_front'
Now you have a Basic Setup and when running your app, you should see your custom ViewController. Now the Button for the Menu has to be added.
In your ViewControllers .m add following:
#interface MyViewController ()
#property (nonatomic) IBOutlet UIBarButtonItem* revealButtonItem;
#end
- (void)viewDidLoad {
[super viewDidLoad];
[self customSetup];
}
- (void)customSetup
{
SWRevealViewController *revealViewController = self.revealViewController;
if ( revealViewController )
{
[self.revealButtonItem setTarget: self.revealViewController];
[self.revealButtonItem setAction: #selector( revealToggle: )];
[self.navigationController.navigationBar addGestureRecognizer: self.revealViewController.panGestureRecognizer];
}
}
Now again switch to the story board. Add a UIBarButtonItem to MyViewController. Again right-click-draw a line from MyViewController to this new item in the NavigationBar. Choose 'revealButtonItem'.
Thats it! Repeat the last steps for every ViewController you want to add. You only have to connect them with right-click-drawing from the TableView to your newly added NavigationController of each of your added ViewControllers. To push the ViewControllers just select 'reveal view controller push controller'.
Hope that helps a bit!