我遵循一个代码,当用户在屏幕上左右滑动时切换选项卡。问题是,当我切换到其他选项卡时,选项卡栏就消失了。
我试着推一个navigationController,但是它抛出了一个错误,表示它不受支持。
以下是代码:
- (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];
}发布于 2015-11-04 06:13:14
你这样做是不对的。要通过code切换Tab,您不必推一个新的ViewController,它将取代现有的ViewController。
要做到这一点,您可以使用以下方法之一:
self.tabBarController.selectedIndex = i;或
[self.tabBarController setSelectedIndex:i];* i是您想要显示的ViewController的index。
https://stackoverflow.com/questions/33514625
复制相似问题