问题描述
tabBarController = [[UITabBarController alloc] init]; //Create a tab bar
view1 = [[View1 alloc] init]; //Create the first view
UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:view1];
navigationController1.navigationBar.tintColor =[UIColor blackColor];
view2 = [[View2 alloc] init]; //create the second view
UINavigationController *navigationController2 = [[UINavigationController alloc] initWithRootViewController:view2];
navigationController2.navigationBar.tintColor =[UIColor blackColor];
tabBarController.viewControllers = [NSArray arrayWithObjects:navigationController1, navigationController2,nil];
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
上面的代码来自我的appDelegate类(applicationDidFinishLaunching). View1和View2是UIViewController.这就是我最初设计应用程序的方式,该应用程序有两个选项卡(view1和view2).下面的代码实现了当用户向左或向右滑动时在View1中发生的情况.
The code above are from my appDelegate class (applicationDidFinishLaunching). View1 and View2 are UIViewController. This is how I originally design my apps, that the apps has two tabs, (view1 and view2). The code below, implement what happen in View1, when the user swipe left or right.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"I am in touches began methods");
UITouch *touch = [touches anyObject];
gestureStartPoint = [touch locationInView:self.view];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint currentPosition = [touch locationInView:self.view];
CGFloat deltaX = fabsf(gestureStartPoint.x - currentPosition.x);
CGFloat deltaY = fabsf(gestureStartPoint.y - currentPosition.y);
if (deltaX >= kMinimumGestureLength && deltaY <= kMaximumVariance) {
//Set Animation Properties
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration: 1];
if(gestureStartPoint.x > currentPosition.x){ //I am trying to move right
ViewControllerTemplate *newView = [[ViewControllerTemplate alloc] initWithNibName:@"ViewControllerTemplate" bundle:nil];
//Transition spin right
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.navigationController.view cache:NO];
//Push OnTo NavigationController
[self.navigationController pushViewController:newView animated:YES];
}
else { //I am trying to move left
if([viewDelegate getCurrentViewID] > 0){ //At view 0, should not pop anymore view
//Transition Spin left
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.navigationController.view cache:NO];
[self.navigationController popViewControllerAnimated:YES];
}
}
[UIView commitAnimations];
}
}
差不多,它只是说当用户向右滑动时,会创建一个新视图(ViewControllerTemplate)和pushViewController,而当用户向左滑动时,则会创建popViewController.但是,由于我改变主意了,所以不想实现标签栏.因此,我将appDelegate的applicationDidFinishLaunching中的代码更改为以下代码,并且View1.m中touchesMoved中的pushViewController停止工作.我确信它到达了那里,但是当它将newView的pushViewController时,什么也没有发生.我觉得当我取出UINavigationController时,我的视图不再正确地推入堆栈了.知道为什么,以及如何解决
Pretty much, it just say that when the user swipe right, it create a new view (ViewControllerTemplate) and pushViewController, and when the user swipe left, popViewController. However, since I change my mind and dont want to implement tab bar. So I change my code in applicationDidFinishLaunching in appDelegate to these below and the pushViewController in touchesMoved in View1.m stop work. I am sure that it get to there, but when it pushViewController of the newView, nothing happen. I feel like when I took out the UINavigationController, my view are not push onto the stack properly anymore. Any idea why, and how to fix it
view1 = [[View1 alloc] init];
[window addSubview:View1.view];
推荐答案
如果有人遇到相同的问题,这是解决方案将我的applicationDidFinishLaunching()替换为该
If anyone ever run into the same problem, here is the solutionReplace what I have for applicationDidFinishLaunching() up there to this
view1 = [[View1 alloc] initWithNibName:@"View1" bundle:nil]; //Create the first view
UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:view1];
navigationController1.navigationBar.tintColor =[UIColor blackColor];
view1 = navigationController1;
[window addSubview:view1.view];
[window makeKeyAndVisible];
这篇关于pushViewController有问题!!帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!