2015-11-04 98 views
2

我跟着一个代码,当用户在屏幕上向左/向右滑动时切换选项卡。问题是,当我切换到其他选项卡时,选项卡栏消失。Objective-C切换标签使用滑动

我试着推导航控制器,但它会抛出一个错误,说它不被支持。

下面是代码:

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRight:)]; 
    swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft; 
    [self.view addGestureRecognizer:swipeLeft]; 
    swipeLeft.delegate = self; 

    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRight:)]; 
    swipeRight.direction = UISwipeGestureRecognizerDirectionRight; 
    [self.view addGestureRecognizer:swipeRight]; 
    swipeRight.delegate = self; 
} 

-(void) swipeRight:(UISwipeGestureRecognizer *) recognizer { 
    if (recognizer.direction == UISwipeGestureRecognizerDirectionRight){ 
     //NSLog(@"swipe right"); 
     NSString * storyboardName = @"Main"; 
     UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil]; 
     UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"RecentsController"]; 
     [self presentViewController:vc animated:YES completion:nil]; 

    } 
} 

-(void) swipeLeft:(UISwipeGestureRecognizer *) recognizer { 
    if (recognizer.direction == UISwipeGestureRecognizerDirectionRight) 
     NSLog(@"Swipe left"); 
    NSString * storyboardName = @"Main"; 
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil]; 
    UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"SettingsController"]; 
    [self presentModalViewController:vc animated:YES]; 
} 

回答

2

你正在做它在一个错误的方式。要切换你标签栏通过code你不必推新的ViewController它将取代你现有的。

要做到这一点,你可以使用下面的方法之一:

self.tabBarController.selectedIndex = i; 

[self.tabBarController setSelectedIndex:i]; 

****凡i是的indexViewController你想显示。