侧面菜单和按钮单击更改视图控制器

时间:2016-09-06 18:44:55

标签: ios objective-c

我的侧边菜单有问题。我不想使用任何框架并尝试通过自己的方式做一些事情,就像我使用本教程作为指南sliding out navigation panel一样。在教程中显示如何构建由表视图和单元格组成的侧面菜单,但我使用按钮代替单元格。这是我的问题:侧面菜单很好地滑动并且返回一切正确,但我不知道如何进行导航。我创建了一系列按钮,在一个IBAction中添加一个单独的事件标签。我有一系列的视图控制器,我想在按下按钮时替换它我创建了一个名为" currenViewController "的公共viewController。并在 viewDidLoad

- (void)viewDidLoad {
    [super viewDidLoad];
    _currentViewController = [[UIViewController alloc] init];
    _currentViewController = _messagesViewController;
    NSLog(@"TOP log %@", _currentViewController);
    currentXibName = @"MessagesViewController";

    [self setupView];
}

" TOP LOG"说currentViewController是null但是当我运行app时,我看到我的messageViewController,一切正常。这是我的setupView方法

- (void)setupView {
    self.currentViewController = [[UIViewController alloc] initWithNibName:currentXibName bundle:nil];
    self.currentViewController.view.tag = CENTER_TAG;
    _currentViewController.view.frame = CGRectMake(70, 0, self.view.frame.size.width, self.view.frame.size.height);
    self.headerView.delegate = self;
    [self.view addSubview:self.currentViewController.view];
    [self addChildViewController:_currentViewController];
    [_currentViewController didMoveToParentViewController:self];
    [self setupGestures];

}

但是当我尝试通过按钮更改我的视图控制器时,没有任何反应。

-(IBAction)menuButtonClickSelector:(UIButton *)sender {
    if (sender.tag == 101) {
        NSLog(@"homepressed");
        _currentViewController = _homeViewController;
        currentXibName = @"HomeViewController";
        [self changeViewController:_currentViewController xibName:currentXibName];
    } else if (sender.tag == 102) {
        NSLog(@"messagepra");
        _currentViewController = _messagesViewController;
        currentXibName = @"MessagesViewController";
        [self changeViewController:_currentViewController xibName:currentXibName];
    } else {
        NSLog(@"not found tag");
    } 
}

这是我的changeViewController方法

-(void)changeViewController:(UIViewController *)vc xibName:(NSString *)xibName {
    vc = _currentViewController;
    xibName = currentXibName;
    vc = [[UIViewController alloc] initWithNibName:xibName bundle:nil];
    [self.navigationController pushViewController:vc animated:YES];
    NSLog(@"view controller now %@  xib name now %@", vc, xibName);
}

可能我应该说我的ViewControllers xib包含我从不同的xib添加的视图,例如标题和背景。 我将不胜感激任何帮助!!!

0 个答案:

没有答案