2013-12-20 32 views
0

我在應用程序女巫中包含側面菜單。didSelectRowAtIndexPath不能在帶有側邊菜單的ios6中工作

當我在側邊菜單中選擇它的顯示UItableview選項時,此表位於FirstViewController中,當我在firstViewController中選擇特定行時,它不導航到SecondView控制器。

對於側菜單我正在從GitHub準備模板,包含

1)JWSlideMenuController 
2)JWNavigationController 
3)JWSlideMenuViewController 

在這裏,我附上代碼JWNavigationCOntroller.h和JWNavigationCOntroller.m文件 也FirstViewController.h和FirstViewController.m文件

**JWNavigationController.h** 





// JWNavigationController.h 
    // JWSlideMenu 
    // 
    // Created by Jeremie Weldin on 11/22/11. 
    // Copyright (c) 2011 Jeremie Weldin. All rights reserved. 
    // 

    #import <UIKit/UIKit.h> 
    @class JWSlideMenuController; 

    @interface JWNavigationController : UIViewController <UINavigationBarDelegate> 

    @property (nonatomic, retain) UINavigationBar *navigationBar; 
    @property (nonatomic, retain) UIView *contentView; 
    @property (nonatomic, retain) JWSlideMenuController *slideMenuController; 
    @property (nonatomic, retain, readonly) UIViewController *rootViewController; 

    - (id)initWithRootViewController:(UIViewController *)rootViewController; 
    - (void)pushViewController:(UIViewController *)controller; 

    - (UIViewController *)popViewController; 

    @end 

JWNavigationController.m

// JWNavigationController.m 
// JWSlideMenu 
// 
// Created by Jeremie Weldin on 11/22/11. 
// Copyright (c) 2011 Jeremie Weldin. All rights reserved. 
// 

#import "JWNavigationController.h" 
#import "JWSlideMenuViewController.h" 

@interface JWNavigationController(Private) 

-(UIViewController*)removeTopViewController; 

@end 

@implementation JWNavigationController 

@synthesize navigationBar; 
@synthesize contentView; 
@synthesize slideMenuController; 
@synthesize rootViewController=_rootViewController; 


#pragma mark - View lifecycle 

- (id)init 
{ 
    self = [super init]; 
    if (self) { 

     CGRect masterRect = [[UIScreen mainScreen] bounds]; 
     CGRect contentFrame = CGRectMake(0.0, 44.0, masterRect.size.width, masterRect.size.height - 44.0); 
     CGRect navBarFrame = CGRectMake(0.0, 0.0, masterRect.size.width, 44.0); 

     self.view = [[[UIView alloc] initWithFrame:masterRect] autorelease]; 
     self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth; 
     self.view.backgroundColor = [UIColor whiteColor]; 

     self.contentView = [[[UIView alloc] initWithFrame:contentFrame] autorelease]; 
     self.contentView.backgroundColor = [UIColor whiteColor]; 
     self.contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth; 
     [self.view addSubview:self.contentView]; 

     self.navigationBar = [[[UINavigationBar alloc] initWithFrame:navBarFrame] autorelease]; 

     self.navigationBar.autoresizingMask = UIViewAutoresizingFlexibleWidth; 
     self.navigationBar.delegate = self; 
     [self.view insertSubview:self.navigationBar aboveSubview:self.contentView]; 
     self.navigationBar.backgroundColor=[UIColor whiteColor]; 



    } 
    return self; 
} 


- (id)initWithRootViewController:(JWSlideMenuViewController *)rootViewController 
{ 
    self = [self init]; 
    if(self) { 
     _rootViewController = rootViewController; 
     UIBarButtonItem *menuButton = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"menu_icon_20x20.png"] style:UIBarButtonItemStyleBordered target:self.slideMenuController action:@selector(toggleMenu)] autorelease]; 
     rootViewController.navigationItem.leftBarButtonItem = menuButton; 
     [self addChildViewController:rootViewController]; 
     [self.contentView addSubview:rootViewController.view]; 
     [self.navigationBar pushNavigationItem:rootViewController.navigationItem animated:YES]; 
     //rootViewController.navigationController = self; 
    } 
    return self; 
} 

#pragma mark - UINavigationBarDelegate 


- (void)navigationBar:(UINavigationBar *)navigationBar didPopItem:(UINavigationItem *)item 
{ 
    UIViewController *controller = [self.childViewControllers lastObject]; 

    if (item==controller.navigationItem) //Will now called only if a back button pop happens, not in manual pops 
    { 
     [self removeTopViewController]; 
    } 
} 

- (void)navigationBar:(UINavigationBar *)navigationBar didPushItem:(UINavigationItem *)item 
{ 

} 

#pragma mark - Stack Interaction 

- (void)pushViewController:(JWSlideMenuViewController *)controller 
{ 
    [self addChildViewController:controller]; 
    [self.navigationBar pushNavigationItem:controller.navigationItem animated:YES]; 
    controller.navigationController = self; 

    controller.view.frame = self.contentView.bounds; 

    if([self.childViewControllers count] == 1) 
    { 
     [self.contentView addSubview:controller.view]; 
    } 
    else 
    { 
     UIViewController *previousController = [self.childViewControllers objectAtIndex:[self.childViewControllers count]-2]; 
     [self transitionFromViewController:previousController toViewController:controller duration:0.5 options:UIViewAnimationOptionTransitionNone animations:NULL completion:NULL]; 
    } 
} 


- (UIViewController *)popViewController 
{ 
    //Can use this to pop manually rather than back button alone 
    UIViewController *controller = [self.childViewControllers lastObject]; 
    UIViewController *previousController = nil; 
    if([self.childViewControllers count] > 1) 
    { 
     previousController = [self.childViewControllers objectAtIndex:[self.childViewControllers count]-2]; 
     previousController.view.frame = self.contentView.bounds; 
    } 

    [self transitionFromViewController:controller toViewController:previousController duration:0.3 options:UIViewAnimationOptionTransitionNone animations:NULL completion:NULL]; 
    [controller removeFromParentViewController]; 

    if(self.navigationBar.items.count > self.childViewControllers.count) 
     [self.navigationBar popNavigationItemAnimated:YES]; 

    return controller; 
} 

- (void)viewDidUnload 
{ 
    _rootViewController = nil; 
    self.navigationBar = nil; 
    self.contentView = nil; 

    self.slideMenuController = nil; 

    [super viewDidUnload]; 
} 

- (void)dealloc { 
    [_rootViewController release]; 
    [navigationBar release]; 
    [contentView release]; 
    [super dealloc]; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return YES; 
} 

-(UIViewController*)removeTopViewController 
{ 
    UIViewController *controller = [self.childViewControllers lastObject]; 

    UIViewController *previousController = nil; 
    if([self.childViewControllers count] > 1) 
    { 
     previousController = [self.childViewControllers objectAtIndex:[self.childViewControllers count]-2]; 
     previousController.view.frame = self.contentView.bounds; 


    } 


    [self transitionFromViewController:controller toViewController:previousController duration:0.3 options:UIViewAnimationOptionTransitionNone animations:NULL completion:NULL]; 
    [controller removeFromParentViewController]; 

    return controller; 
} 

@end 

這裏是代碼FirstViewController.h

// FirstViewController.h 

// Created by mobile on 12/18/13. 
// Copyright (c) 2013 Jeremie Weldin. All rights reserved. 
// 

#import "JWSlideMenuViewController.h" 

@interface FirstViewController : JWSlideMenuViewController<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate> 

@property (strong,nonatomic) NSMutableArray *array; 

@property (retain, nonatomic) IBOutlet UITableView *myTableView; 
@end 

Here is code for FirstViewController.m 
// FirstViewController.m 
// 
// 
// Created by mobile on 12/18/13. 
// Copyright (c) 2013 Jeremie Weldin. All rights reserved. 
// 
#import "FirstViewController.h" 
#import "SecondViewController.h" 
#import "JWNavigationController.h" 

@interface FirstViewController() 

@end 

@implementation FirstViewController 

@synthesize array,myTableView; 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 


- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 


    array=[[NSMutableArray alloc]initWithObjects:@"One",@"Two",@"Three",@"Four",@"Five",@"Six",@"Seven",@"Eight",@"Nine",@"Ten" ,nil]; 
} 



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [array count]; 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *[email protected]"cell"; 

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellidentifier]; 
    if(cell==nil) 
    { 
     cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellidentifier]; 

     cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator; 

    } 
    cell.textLabel.text=[array objectAtIndex:indexPath.row]; 

    return cell; 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 





    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
      UIViewController *controller = [[[SecondViewContRoller alloc] init] autorelease]; 
      [self.navigationController pushViewController:controller]; 
    } 

- (void)viewDidUnload 
{ 
    [self setMyTableView:nil]; 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 
- (void)dealloc { 
    [super dealloc]; } @end 

**I am also Try This Code but not working** 
     SecondViewController *second=[[SecondViewContRoller alloc]initWithNibName:@" SecondViewContRoller" bundle:nil]; 
       [self.navigationController pushViewController:controller]; 

JWSlideMenuController.h包含下面的代碼


// JWSlideMenuController.h 
// JWSlideMenu 
// 
// Created by Jeremie Weldin on 11/14/11. 
// Copyright (c) 2011 Jeremie Weldin. All rights reserved. 
// 

#import <UIKit/UIKit.h> 
@class JWNavigationController; 
@class JWSlideMenuViewController; 

@interface JWSlideMenuController : UIViewController <UITableViewDataSource, UITableViewDelegate> 

@property (retain, nonatomic) UITableView *menuTableView; 
@property (retain, nonatomic) UIView *menuView; 
@property (retain, nonatomic) UIToolbar *contentToolbar; 
@property (retain, nonatomic) UIView *contentView; 
@property (retain, nonatomic) UIColor *menuLabelColor; 

-(IBAction)toggleMenu; 
-(JWNavigationController *)addViewController:(JWSlideMenuViewController *)controller withTitle:(NSString *)title andImage:(UIImage *)image; 


@end 

JWSlideMenuController.m包含下面的代碼didSelectRowAtIndexPath方法


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    if([contentView.subviews count] == 1){ 
     [[contentView.subviews objectAtIndex:0] removeFromSuperview]; 
    } 


    UIViewController* controller = (UIViewController*)[self.childViewControllers objectAtIndex:indexPath.row]; 
    controller.view.frame = self.contentView.bounds; 
    [contentView addSubview:controller.view]; 
    [self toggleMenu]; 





} 
+0

'didSelectRowAtIndexPath'委託被調用嗎? –

+0

當你點擊單元格「didSelectRowAtIndexPath」正在調用? –

+0

不要在筆尖名稱中放置任何空間。 'SecondViewController * second = [[SecondViewContRoller alloc] initWithNibName:@「SecondViewContRoller」bundle:nil]; [self.navigationController pushViewController:controller];' –

回答

2

試試這個。

從github下載zib用於MFSideMenu類,並將此代碼用於表視圖的select方法。它在ios6中工作得很好,ios7也是......

yourViewController *dealsVC = [[yourViewController alloc] initWithNibName:@"yourViewController" bundle:nil]; 
     //[self.navigationController pushViewController:dealsVC animated:YES]; 


     UINavigationController *navigationController = self.menuContainerViewController.centerViewController; 
     NSArray *controllers = [NSArray arrayWithObject:dealsVC]; 
     navigationController.viewControllers = controllers; 
     [self.menuContainerViewController setMenuState:MFSideMenuStateClosed]; 
+0

https:// github。com/mikefrederick/MFSideMenu/blob/master/MFSideMenu/ –

+0

這裏是左側和右側菜單的完整教程。 –

+0

如果你有這個,否則我們將繼續在這個明天 –