【发布时间】: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