如何使用ios中的选项卡制作滑动菜单

时间:2014-05-16 15:08:58

标签: objective-c ios7 tabs slidingmenu

我正在使用ZUUIRevealController库。

Appdelegate.h文件

(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是一个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:)];
    }
}

标签工作正常,但滑动菜单无法正常工作。

首先,我点击Home选项卡和Slide菜单查看幻灯片Viewcontroller.Next我点击了Second选项卡和滑动菜单来查看幻灯片Viewcontroller。我再次点击了Home选项卡和滑动菜单来查看Slide Viewcontroller。它不能显示只显示黑屏。

1 个答案:

答案 0 :(得分:0)

我找到了解决这个问题的方法。 在Appdelegate文件中,我编写了swreavealtoggle调用导航。

- (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;}

然后创建一个viewController来添加Tabbar项目组件。 Myviewcontroller名称是HomeViewController.h 在我包含标签项按钮后单击操作

#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;
}

}

现在它运作良好