【问题标题】:switching ViewController in AppDelegate - doesn´t show up在 AppDelegate 中切换 ViewController - 不显示
【发布时间】:2011-07-17 13:35:57
【问题描述】:

坚持这个,寻求建议。

我做了什么和我的项目: 我为孩子们做一本书。每个页面都是一个自定义的 ViewController。在每个页面上,我都有一个用于下一页和上一页的按钮。 我使用 AppDelegate 加载 ViewControllers。 我的 AppDelgate 从 ViewController1 开始,当我在 ViewController1 中按下按钮/滑动时,它应该加载 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。 有什么想法吗?

提前致谢

【问题讨论】:

  • 我认为您应该首先查看 Apple 的 Objective-C 指南中的分配/初始化章节。看来,您还没有很好地理解这些非常基本的概念。你永远不会在没有相关 init 方法的情况下调用 alloc。
  • 与 farajnew 的解决方案一起工作。谢谢你的时间。我现在就读。

标签: objective-c view delegates viewcontroller


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    view2 = [ViewController2 alloc];
    

    【讨论】:

    • @tomk,完全正确,但是在更深入地阅读了代码之后,我意识到和许多其他人,现在我相信他的问题可能在任何地方..
    猜你喜欢
    • 2016-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多