在AppDelegate中切换ViewController - 没有显示

时间:2011-07-17 13:35:57

标签: objective-c view delegates viewcontroller

坚持这一点,寻求建议。

我做了什么和我的项目: 我为孩子们写了一本书。每个页面都是自定义的ViewController。在每个页面上我都有一个下一页和上一页的按钮。 我使用AppDelegate加载ViewControllers。 我的AppDelgate以ViewController1开始,当我按下ViewController1中的Button / swipe时,它应该加载ViewController2但没有任何反应。

AppDelegate中的我的代码:

self.view1 = [ViewController1 alloc];
[window addSubview:view1.view];
[window makeKeyAndVisible];

- (void)goToNextPage {
    view2 = [ViewController2 alloc];    
    UIView *current = self.window;
    [self.window addSubview:view2.view];

    CATransition *animationT = [CATransition animation];
    [animationT setDuration:0.5];
    [animationT setType:kCATransitionMoveIn];
    [animationT setSubtype:kCATransitionFromRight];
    [animationT setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    [[current layer] addAnimation:animationT forKey:@"SwitchToView1"];

 NSLog(@"works2");
}

这是我的 ViewController1

- (void)addButtonNext { 
     UISwipeGestureRecognizer *swipeGesture = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector       (buttonClicked:)] autorelease];
    swipeGesture.numberOfTouchesRequired = 1;
    swipeGesture.direction = (UISwipeGestureRecognizerDirectionLeft);
    [self.view addGestureRecognizer:swipeGesture];
    NSLog(@"works0");
}


- (void)buttonClicked:(id)sender {
    NSLog(@"works1");
    AppDelegate* next = [AppDelegate alloc];
    [next goToNextPage];
}

我的控制台说works0 works1 works2。 有什么想法吗?

提前致谢

2 个答案:

答案 0 :(得分:2)

不要分配应用程序委托 你需要像这样处理它

AppDelegate *next =(AppDelegate *) [[UIApplication sharedApplication] delegate];
[next goToNextPage];

答案 1 :(得分:1)

您似乎没有初始化第二个视图控制器:

view2 = [ViewController2 alloc];