tutorial - ionic side menu example




How to make sliding menu with tabs in ios (2)

I am using ZUUIRevealController Library.

Appdelegate.h file

(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window = window;

     UITabBarController *tabBarController=[[UITabBarController alloc]init];

    FrontViewController *frontViewController = [[FrontViewController alloc] init];
    RearViewController *rearViewController = [[RearViewController alloc] init];
    MapViewController *frontViewController2 = [[MapViewController alloc] init];

    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:frontViewController];
    UINavigationController *navigationController2 = [[UINavigationController alloc] initWithRootViewController:frontViewController2];


    RevealController *revealController = [[RevealController alloc] initWithFrontViewController:navigationController rearViewController:rearViewController];

    RevealController *revealController2 = [[RevealController alloc] initWithFrontViewController:navigationController2 rearViewController:rearViewController];


    [revealController.tabBarItem setTitle:@"Home"];
    [revealController2.tabBarItem setTitle:@"Absent note"];



    revealController.tabBarItem.image=[[UIImage imageNamed:@"home_icon.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    revealController2.tabBarItem.image=[[UIImage imageNamed:@"absent_note_icon.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];





    [tabBarController setViewControllers:[NSArray arrayWithObjects:revealController,revealController2,nil]];



    [self.window setRootViewController:tabBarController];

    [self.window makeKeyAndVisible];
    return YES;
}

FrontViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.title = NSLocalizedString(@"Front View", @"FrontView");

    if ([self.navigationController.parentViewController respondsToSelector:@selector(revealGesture:)] && [self.navigationController.parentViewController respondsToSelector:@selector(revealToggle:)])
    {
        UIPanGestureRecognizer *navigationBarPanGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self.navigationController.parentViewController action:@selector(revealGesture:)];
        [self.navigationController.navigationBar addGestureRecognizer:navigationBarPanGestureRecognizer];
        [self.view addGestureRecognizer:navigationBarPanGestureRecognizer];

        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Slide", @"Slide") style:UIBarButtonItemStylePlain target:self.navigationController.parentViewController action:@selector(revealToggle:)];

        }








}

RearViewController is a Table ViewController

MapViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.title = NSLocalizedString(@"Map View", @"MapView");

    if ([self.navigationController.parentViewController respondsToSelector:@selector(revealGesture:)] && [self.navigationController.parentViewController respondsToSelector:@selector(revealToggle:)])
    {
        UIPanGestureRecognizer *navigationBarPanGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self.navigationController.parentViewController action:@selector(revealGesture:)];
        [self.navigationController.navigationBar addGestureRecognizer:navigationBarPanGestureRecognizer];
        [self.view addGestureRecognizer:navigationBarPanGestureRecognizer];

        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Slide", @"Slide") style:UIBarButtonItemStylePlain target:self.navigationController.parentViewController action:@selector(revealToggle:)];
    }
}

Tabs working Properly, but the sliding menu not working properly.

First I am click the Home tab and Slide menu to view the slide Viewcontroller.Next I clicked the Second tab and sliding menu to view the slide Viewcontroller. Again I've clicked the Home tab and sliding menu to view the Slide Viewcontroller. It cannot displayed only display a black screen.


I finally found a solutino to my problem first I added a static variable to View2 and called it inView2 as following in View2.h

@interface
{}

+ ( BOOL ) isInQuizViewController;
+ ( void )setInQuizViewController:(BOOL)inQuizVal;
//...
@end

in View2.m

@implementation
   static BOOL inView2 = NO;

+(BOOL)isInView2
{
    return inView2;
}
+ ( void )setInView2:(BOOL)Val
{
    inView2 = val;
}

these two methods are for setting and getting the value of inView2 which tells me whether or not the user is currently in View2

in View1.h create an IBAction associated with the button that will transmit from View1 to View2 and connect it to your storyboard

- (IBAction)GoToView2:(id)sender;

go to View1.m and import

#import "View2.h"

and implement your IBAction method to set the InView2 to YES

- (IBAction)GoToView2:(id)sender {
[View2 setInView2:YES];
}

then in the delegate.m

#import "View2.h"
@implementation AppDelegate

UITabBarController * _tabBarController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    _tabBarController = (UITabBarController *)_window.rootViewController;
    _tabBarController.delegate = self;

    // Override point for customization after application launch.
    return YES;
}

I defined a global _tabBarController and set it to _window.rootViewController and set its delegate to this delegate and remember to import "View2.h"

going to the ShouldSelectViewController method (note that this method is called before the transition to the selected tab ViewController, hence it is perfect for decision making of whether or not the selected tab viewController should be displayed to the user). so in ShouldSelectViewController method I did the following

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {



    if(tabBarController.selectedIndex == 2)
    {
        if ([View2 isInView2]) {


        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"alert"
                                                            message:@"are you sure you want to exit?"
                                                           delegate:self
                                                  cancelButtonTitle:@"Cancel"
                                                  otherButtonTitles:@"Yes",nil];
        [alertView show];

        return NO;
        }
    }
    return YES;

}


-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{

    if(buttonIndex == 1)
    {//ok button pressed




//        NSInteger destinationTabIdx = 2;
//        UIView * fromView = tabBarController.selectedViewController.view;
//        UIView * toView = [[[[[tabBarController.viewControllers objectAtIndex:destinationTabIdx] navigationController] viewControllers] objectAtIndex:0] view];

        UINavigationController *nav = (UINavigationController *)_tabBarController.selectedViewController;

        NSLog(@"vc title: %@",nav.title);
//        [UIView transitionFromView:fromView toView:toView duration:0.8
//                           options: UIViewAnimationOptionTransitionNone
//                        completion:^(BOOL finished) {
//                            if (finished) {
//                                tabBarController.selectedIndex = destinationTabIdx;
//                            }
//                        }];
        [nav popViewControllerAnimated:YES];
        [QuizViewController setInQuizViewController:NO];
        NSLog(@"app delegate transistion done");


    }

}

I found the solution for that problem. in Appdelegate file i have written the swreavealtoggle call for navigation.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window = window;

HomeViewController *frontViewController = [[HomeViewController alloc] init];
SlideViewController *rearViewController = [[SlideViewController 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;
self.viewController = revealController;

[self.window setRootViewController:self.viewController];
[self customizeInterface];
[self.window makeKeyAndVisible];
return YES;}

Then create a viewController to add a Tabbar item components. Myviewcontroller name is HomeViewController.h after i include the tab item button click action

#import <UIKit/UIKit.h>
@interface HomeViewController : UIViewController<UITabBarDelegate>{
UITabBar *mainTabBar;
UIViewController *tab1vc;  // view controller of first tab
UIViewController *tab2vc;  // view controller of second tab
UIViewController *tab3vc;  // view controller of first tab
UIViewController *tab4vc;  // view controller of second tab
}
@property (nonatomic, retain) IBOutlet UITabBar *mainTabBar;
@property (nonatomic, retain) UIViewController *tab1vc;
@property (nonatomic, retain) UIViewController *tab2vc;
@property (nonatomic, retain) UIViewController *tab3vc;
@property (nonatomic, retain) UIViewController *tab4vc;

@end

HomeViewController.m

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
// item is the selected tab bar item
switch (item.tag) {
    case 1:
        if (tab1vc == nil) {
            self.tab1vc =[[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil];

        }
        [self.view insertSubview:tab1vc.view belowSubview:mainTabBar];
         self.title = @"Home";
        label.text = self.title;
        //[tab1vc release];
        NSLog(@"1st");

        break;
    case 2:
        if (tab2vc == nil) {
            self.tab2vc =[[Tab2 alloc] initWithNibName:@"Tab2" bundle:nil];

        }
        [self.view insertSubview:tab2vc.view belowSubview:mainTabBar];
        //[tab2vc release];

        self.title = @"Tab2";
        label.text = self.title;
         NSLog(@"2st");


        break;
    case 3:
        if (tab3vc == nil) {
            self.tab3vc =[[Tab3 alloc] initWithNibName:@"Tab3" bundle:nil];

        }
        [self.view insertSubview:tab3vc.view belowSubview:mainTabBar];
        //[tab2vc release];

        self.title = @"Tab3";
        label.text = self.title;
        NSLog(@"3rd");
        break;

    case 4:
        if (tab4vc == nil) {
            self.tab4vc =[[Tab4 alloc] initWithNibName:@"Tab4" bundle:nil];

        }
        [self.view insertSubview:tab4vc.view belowSubview:mainTabBar];
        //[tab2vc release];

        self.title = @"Tab4";
        label.text = self.title;
        NSLog(@"4th");
        break;

    default:
        break;
}

}

Now its working good