2015-11-04 102 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你想顯示。