SWRevealViewController - RightViewController - ios

I'm using SWRevealViewController for implementing two side navigation views in my application. I followed the story board method and successfully implemented the rear view and front view. I tried setting right view exactly like the rear view via storyboard, but couldn't do it.
I have set the reveal view controller segue to "sw_right" but it looks like it is not being recognized by - (void)prepareForSegue:(SWRevealViewControllerSegue *)segue sender:(id)sender which is called twice for "sw_rear" and "sw_front"
What Am I missing?
- (void)prepareForSegue:(SWRevealViewControllerSegue *)segue sender:(id)sender
{
// $ using a custom segue we can get access to the storyboard-loaded rear/front view controllers
// the trick is to define segues of type SWRevealViewControllerSegue on the storyboard
// connecting the SWRevealViewController to the desired front/rear controllers,
// and setting the identifiers to "sw_rear" and "sw_front"
// $ these segues are invoked manually in the loadView method if a storyboard
// was used to instantiate the SWRevealViewController
// $ none of this would be necessary if Apple exposed "relationship" segues for container view controllers.
NSString *identifier = segue.identifier;
if ( [segue isKindOfClass:[SWRevealViewControllerSegue class]] && sender == nil )
{
if ( [identifier isEqualToString:SWSegueRearIdentifier] )
{
segue.performBlock = ^(SWRevealViewControllerSegue* rvc_segue, UIViewController* svc, UIViewController* dvc)
{
[self _setRearViewController:dvc animated:NO];
};
}
else if ( [identifier isEqualToString:SWSegueFrontIdentifier] )
{
segue.performBlock = ^(SWRevealViewControllerSegue* rvc_segue, UIViewController* svc, UIViewController* dvc)
{
[self _setFrontViewController:dvc animated:NO];
};
}
//This is never executed even after setting the identifier
else if ( [identifier isEqualToString:SWSegueRightIdentifier] )
{
segue.performBlock = ^(SWRevealViewControllerSegue* rvc_segue, UIViewController* svc, UIViewController* dvc)
{
[self _setRightViewController:dvc animated:NO];
};
}
}
}

here is the sample project , this is working fine I worked out for your self, the download link is
https://www.sendspace.com/file/0l2ndd
after downloaded the project u want to delete the project , use this link
https://www.sendspace.com/delete/0l2ndd/1b1bd537ad852b2fdea9b9a0cce3390f
here u were need the right swipe on the front view controller , add the UIBarButtonItem in the particular view Controller
#property (strong, nonatomic) IBOutlet UIBarButtonItem *rightIcon; //this is for left bar button Item
#property (nonatomic) IBOutlet UIBarButtonItem* revealButtonItem; //this is for right bar button Item
and add the some functions is View DidLoad
- (void)viewDidLoad
{
[super viewDidLoad];
//action for left Swipe
[self.revealButtonItem setTarget: self.revealViewController];
[self.revealButtonItem setAction: #selector( revealToggle: )];
[self.navigationController.navigationBar addGestureRecognizer: self.revealViewController.panGestureRecognizer];
//action for Right Swipe
[self.rightIcon setTarget: self.revealViewController];
[self.rightIcon setAction: #selector( rightRevealToggle: )];
[self.navigationController.navigationBar addGestureRecognizer: self.revealViewController.panGestureRecognizer];
}
Swift
override func viewDidLoad() {
super.viewDidLoad()
//action for left Swipe
self.revealButtonItem.target = self.revealViewController
self.revealButtonItem.action = "revealToggle:"
self.navigationController.navigationBar.addGestureRecognizer(self.revealViewController.panGestureRecognizer)
//action for Right Swipe
self.rightIcon.target = self.revealViewController
self.rightIcon.action = "rightRevealToggle:"
self.navigationController.navigationBar.addGestureRecognizer(self.revealViewController.panGestureRecognizer)
}

I figured out what the problem was. In loadStoryboardControllers method [self performSegueWithIdentifier:SWSegueRightIdentifier sender:nil]; was commented out. If we implement right view controller this has to be uncommented.

You requested that I look at this thread.
I do not have experience with storyboards.
So, I am pasting below the code I have in my projects that allow for the Right Hand Side.
Take a look at the section I commented. Hopefully, this will set you on the right path.
Good luck
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window = window;
FrontViewController *frontViewController = [[FrontViewController 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;
// THIS SECTION RIGHT BELOW IS PROBABLY WHAT YOU WANT TO LOOK AT
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;
}

There is a try-catch condition between line on 435 to 444 on SWRevealViewController.m. Just rearrange the statement as following
[self performSegueWithIdentifier:SWSegueRightIdentifier sender:nil];
[self performSegueWithIdentifier:SWSegueFrontIdentifier sender:nil];
[self performSegueWithIdentifier:SWSegueRearIdentifier sender:nil];
Now my right-menu/rightViewController is working even though i am not using the leftmenu/rearViewController.

you can also do what shad suggested. But if you are getting a flag with the right you can just replace
- (void)loadStoryboardControllers
if ( self.storyboard && _rearViewController == nil )
{
//Try each segue separately so it doesn't break prematurely if either Rear or Right views are not used.
#try
{
[self performSegueWithIdentifier:SWSegueRearIdentifier sender:nil];
}
#catch(NSException *exception) {}
#try
{
[self performSegueWithIdentifier:SWSegueFrontIdentifier sender:nil];
}
#catch(NSException *exception) {}
#try
{
[self performSegueWithIdentifier:SWSegueRightIdentifier sender:nil];
}
#catch(NSException *exception) {}
}
}
with
- (void)loadStoryboardControllers
{
//[self performSegueWithIdentifier:SWSegueRightIdentifier sender:nil];
[self performSegueWithIdentifier:SWSegueFrontIdentifier sender:nil];
[self performSegueWithIdentifier:SWSegueRearIdentifier sender:nil];
}
as you can see i commented out
//[self performSegueWithIdentifier:SWSegueRightIdentifier sender:nil];
becasue thats the one giving you an issue. If its the front or rear you can do the same

Related

How to return to view controller in main.storyboard from a xib without using navigation controller or navigation bar?

I am making an app with different view controllers. My home screen is in main.storyboard. Rest of the view controllers have their own xib, .h & .m files. I am trying this for navigation from home screen.
-(IBAction)btnSignUpTapped:(id)sender
{
SignUpWithEmailViewController * login = [[SignUpWithEmailViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc]
initWithRootViewController:login];
[self presentViewController:nav animated:YES completion:NULL];
}
This code works fine for navigating to different viewcontroller (in this case SignUpwithEmailViewController). On SignUpWithEmailViewController I have a back button which is supposed to bring me back to home screen. This is what I got so far:
-(IBAction)btnBackTapped:(id)sender
{
ViewController * homeScreen = [[ViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc]
initWithRootViewController:homeScreen];
[self presentViewController:nav animated:YES completion:NULL];
}
But as result of this code, screen turns black and nothing happens. How do I solve this problem? For hiding the nav bar I am using
-(void) viewWillAppear:(BOOL)animated
{
[[self navigationController] setNavigationBarHidden:YES animated:NO];
}
Yes, you have to insert you called viewController to NavigationController,
but if you wont to use modal flow - to close presentedViewController just call:
[self dismissViewControllerAnimated:YES completion:^{
}];
EDITED:
may be it will be more helpful, it just example, put it to you main view controller:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
UIViewController *c = [[UIViewController alloc] init];
c.view.backgroundColor = [UIColor redColor];
[self presentViewController:c animated:YES completion:^{
[self dismissViewControllerAnimated:YES completion:^{
}];
}];
}

Call method before SWRevealViewController is shown

I am using the SWRevealViewController plugins, it works great and I can navigate fine around the pages. My issue is that when the menu is being shown you can still interact with objects on the view that called the menu. I want to disable buttons from my view whilst the menu is shown. I am assigning the sidebar button using this code.
_sidebarButton.tintColor = [UIColor colorWithWhite:0.1f alpha:0.9f];
_sidebarButton.target = self.revealViewController;
_sidebarButton.tag=10;
_sidebarButton.action = #selector(revealToggle:);
[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
I cannot work out how to call a function in my own class before the revealToggle action is fired. Any suggestions?
In MenuViewController (which is rear viewcontroller) write following:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.revealViewController.frontViewController.view setUserInteractionEnabled:NO];
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[self.revealViewController.frontViewController.view setUserInteractionEnabled:YES];
}
then you can get each subview from this viewcontroller.
for example if frontviewcontroller is UINavigationController, you can do like this:
UINavigationController *navController = (UINavigationController *)self.revealViewController.frontViewController;
UIViewController *rootViewController = [navController.viewControllers firstObject];
NSArray *allViews = [NSArray arrayWithArray:(NSArray *)[rootViewController.view subviews]];

iOS ShareKit Cancel button is not working with Xcode 4.2

The iOS Sharekit working with previous Xcode but will 4.2 it's not working anymore, When I hit the Cancel button it goes to this routine
Inside the SHK.m
- (void)hideCurrentViewControllerAnimated:(BOOL)animated
{
if (isDismissingView)
return;
if (currentView != nil)
{
// Dismiss the modal view
if ([currentView parentViewController] != nil)
{
self.isDismissingView = YES;
[[currentView parentViewController] dismissModalViewControllerAnimated:animated];
}
else
self.currentView = nil;
}
}
I stepped the code and it just hit if (isDissmissingView) and it just return.
So, I manually inserted the code
[[currentView parentViewController] dismissModalViewControllerAnimated:animated];
to the top of the routine but this doesn't do anything.
I also includes some other codes for reference
- (void)showViewController:(UIViewController *)vc
{
if (rootViewController == nil)
{
// Try to find the root view controller programmically
// Find the top window (that is not an alert view or other window)
UIWindow *topWindow = [[UIApplication sharedApplication] keyWindow];
if (topWindow.windowLevel != UIWindowLevelNormal)
{
NSArray *windows = [[UIApplication sharedApplication] windows];
for(topWindow in windows)
{
if (topWindow.windowLevel == UIWindowLevelNormal)
break;
}
}
UIView *rootView = [[topWindow subviews] objectAtIndex:0];
id nextResponder = [rootView nextResponder];
if ([nextResponder isKindOfClass:[UIViewController class]])
self.rootViewController = nextResponder;
else
NSAssert(NO, #"ShareKit: Could not find a root view controller. You can assign one manually by calling [[SHK currentHelper] setRootViewController:YOURROOTVIEWCONTROLLER].");
}
// Find the top most view controller being displayed (so we can add the modal view to it and not one that is hidden)
UIViewController *topViewController = [self getTopViewController];
if (topViewController == nil)
NSAssert(NO, #"ShareKit: There is no view controller to display from");
// If a view is already being shown, hide it, and then try again
if (currentView != nil)
{
self.pendingView = vc;
[[currentView parentViewController] dismissModalViewControllerAnimated:YES];
return;
}
// Wrap the view in a nav controller if not already
if (![vc respondsToSelector:#selector(pushViewController:animated:)])
{
UINavigationController *nav = [[[UINavigationController alloc] initWithRootViewController:vc] autorelease];
if ([nav respondsToSelector:#selector(modalPresentationStyle)])
nav.modalPresentationStyle = [SHK modalPresentationStyle];
if ([nav respondsToSelector:#selector(modalTransitionStyle)])
nav.modalTransitionStyle = [SHK modalTransitionStyle];
nav.navigationBar.barStyle = nav.toolbar.barStyle = [SHK barStyle];
[topViewController presentModalViewController:nav animated:YES];
self.currentView = nav;
}
// Show the nav controller
else
{
if ([vc respondsToSelector:#selector(modalPresentationStyle)])
vc.modalPresentationStyle = [SHK modalPresentationStyle];
if ([vc respondsToSelector:#selector(modalTransitionStyle)])
vc.modalTransitionStyle = [SHK modalTransitionStyle];
[topViewController presentModalViewController:vc animated:YES];
[(UINavigationController *)vc navigationBar].barStyle =
[(UINavigationController *)vc toolbar].barStyle = [SHK barStyle];
self.currentView = vc;
}
self.pendingView = nil;
}
(void)hideCurrentViewController
{
[self hideCurrentViewControllerAnimated:YES];
}
This is a known error, which showed up on ios5. This has been fixed a long time ago in ShareKit 2.0. If you decide to upgrade, make sure to follow new install wiki very carefully and literally, since many things have changed comparing to the original sharekit
.

UINavigationController, how to hide tabbar in second level viewController then show tabbar in third level viewController

here is a piece of my code, but in this way, when I push the third level view controller, the tabbar won't show.
//at first level
SecondLevelViewController *_2vc = [[SecondLevelViewController alloc]initWithNibName:#"SecondLevelViewController" bundle:nil];
_2vc.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:_2vc animated:YES];
//at second level
ThirdLevelViewController *_3vc = [[ThirdLevelViewController alloc]initWithNibName:#"ThirdLevelViewController" bundle:nil];
_3vc.hidesBottomBarWhenPushed = NO;
[self.navigationController pushViewController:_3vc animated:YES];
// Load the view
AddViewController *aController = [[AddViewController alloc] init];
// Set the view title
aController.title = #"Add View";
// hide tabbar
aController.hidesBottomBarWhenPushed = YES;
// add it to stack.
[[self navigationController] pushViewController:aController animated:YES];
-(void)viewWillAppear: (BOOL)animated
{
[super viewWillAppear:animated];
[self.tabBarController.tabBar setHidden:YES];
}
-(void)viewWillDisappear: (BOOL)animated
{
[super viewWillDisappear:animated];
[self.tabBarController.tabBar setHidden:NO];
}
Instead of setting the values of hidesBottomBarWhenPushed when you initialise the view controllers, you should instead handle the hiding mechanism in the -(void)viewWillAppear:(BOOL)animated in the view controllers instead.
An example of this implementation would be:
In SecondLevelViewController.m
-(void)viewWillAppear:(BOOL)animated
{
[_bottomBar setHidden:YES];
}
In ThirdLevelViewController.m
-(void)viewWillAppear:(BOOL)animated
{
[_bottomBar setHidden:NO];
}

Add close button to UIModalPresentationPageSheet corner

Is there a way to add a button at the corner of an UIModalPresentationPageSheet? I mean, I want to put that Apple-like (x) button at the corner of a Page Sheet, but adding it to the parent view makes it appear behind the Page Sheet (and also impossible to tap) and adding it to the Page Sheet will make part of it hidden, since it's out of the view area.
Is there a solution?
Thanks.
Here's a solution that I use. It's not quite what you describe, which would be neat too, but would be tricky since you'd want the button to be partially out of the view's bounds (as you say, it would have to be a child of the view-controller-view's superview).
My solution is to put a close button in the left-button area of a navigation bar. I do this automagically via a UIViewController class extension. To use it, just call [currentViewController presentAutoModalViewController: modalViewController animated: YES];
#implementation UIViewController (Modal)
- (void) presentAutoModalViewController: (UIViewController *) modalViewController withDismissAction: (SEL) onDismiss animated:(BOOL)animated
{
UINavigationController* nc = nil;
if ( NO == [ modalViewController isKindOfClass: [UINavigationController class]] )
{
nc = [[[UINavigationController alloc] initWithRootViewController: modalViewController] autorelease];
[nc setToolbarHidden:YES animated: NO];
nc.modalPresentationStyle = modalViewController.modalPresentationStyle;
modalViewController.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop
target:self
action:onDismiss] autorelease];
}
else
{
nc = (UINavigationController*) modalViewController;
UIViewController* rootViewController = [nc.viewControllers objectAtIndex: 0];
rootViewController.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop
target:self
action:onDismiss] autorelease];
}
[nc setNavigationBarHidden: NO];
nc.navigationBar.barStyle = UIBarStyleBlack;
nc.toolbar.barStyle = self.navigationController.navigationBar.barStyle;
[self presentModalViewController: nc animated: animated ];
}
- (void) presentAutoModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated
{
[self presentAutoModalViewController:modalViewController withDismissAction: #selector(autoModalViewControllerDismiss:) animated: animated];
}
- (void) autoModalViewControllerDismiss: (id)sender
{
[self dismissModalViewControllerAnimated:YES];
}
- (BOOL) isAutoModalViewController
{
return ( self.navigationController != nil && self.navigationController.parentViewController != nil && self.navigationController.parentViewController.modalViewController == self.navigationController );
}
#end
EDIT: Actually, the first thing I would suggest you do is use a different kind of close button. You can, for example, add a toolbar at the top with a Done button.
If you still want the Apple-style floating X, then try setting properties to make sure it doesn't get hidden or clipped. I would say the better way to do this would be to create a UIButton with a foreground image which is the styled button image, and a background image which fades from the background colour/patterns of the page sheet to a transparent background around the button. It effectively gives you a "floating close button" without having to go outside the bounds of the page sheet.
I found #TomSwift's suggestion very helpful. Here is a version that uses the non-deprecated iOS7 methods and ARC.
#implementation UIViewController (Modal)
- (void)presentAutoModalViewController: (UIViewController *) modalViewController withDismissAction:(SEL) onDismiss animated:(BOOL)animated
{
UINavigationController* nc = nil;
if ( NO == [ modalViewController isKindOfClass: [UINavigationController class]] )
{
nc = [[UINavigationController alloc] initWithRootViewController: modalViewController];
[nc setToolbarHidden:YES animated: NO];
nc.modalPresentationStyle = modalViewController.modalPresentationStyle;
modalViewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop
target:self
action:onDismiss];
}
else
{
nc = (UINavigationController*) modalViewController;
UIViewController* rootViewController = [nc.viewControllers objectAtIndex: 0];
rootViewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop
target:self
action:onDismiss];
}
[nc setNavigationBarHidden: NO];
nc.navigationBar.barStyle = UIBarStyleBlack;
nc.toolbar.barStyle = self.navigationController.navigationBar.barStyle;
[self presentViewController:nc animated:animated completion:nil];
}
- (void)presentAutoModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated
{
[self presentAutoModalViewController:modalViewController withDismissAction: #selector(autoModalViewControllerDismiss:) animated: animated];
}
- (void)autoModalViewControllerDismiss: (id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (BOOL)isAutoModalViewController
{
return ( self.navigationController != nil && self.navigationController.parentViewController != nil && self.navigationController.parentViewController.presentedViewController == self.navigationController );
}
#end
And I call it like...
MyController *vc = [[MyController alloc] init];
vc.modalPresentationStyle = UIModalPresentationFormSheet;
vc.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentAutoModalViewController:info animated:YES];

Resources