Having problem with pushViewController!! Help

2019-06-14 09:46发布

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];

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];
}
}

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];

1条回答
混吃等死
2楼-- · 2019-06-14 10:13

If anyone ever run into the same problem, here is the solution Replace 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];
查看更多
登录 后发表回答