【问题标题】:How to push/pop controller by swipe in iPhone SDK?如何通过在 iPhone SDK 中滑动来推送/弹出控制器?
【发布时间】:2011-09-07 09:46:24
【问题描述】:
目前我正在开发一个 iPhone 应用程序,其中通过滑动手势识别器在两个控件之间导航。
应用结构是这样的
为此,我首先加载日历屏幕并在它的视图上加载,我使用 PushViewController 加载主屏幕。在主屏幕上,如果用户向左滑动,则日历屏幕使用 PopViewController 显示,或者如果用户向右滑动,则列表视图屏幕由 PushViewController 显示。这工作正常。
现在的问题是,在我的列表视图屏幕上,我正在使用 UITableViewController,其中手势识别器无法正常工作,有时它无法滑动。
如何在这些屏幕之间完美滑动,我想像滚动视图滚动一样滑动。
请给我一些解决方案。
【问题讨论】:
标签:
iphone
uiscrollview
uigesturerecognizer
swipe
pushviewcontroller
【解决方案1】:
如果您的应用在 iOS 3.2 或更高版本上,您可以使用 UISwipeGestureRecognizer 执行此操作,如下所示。
//Add Table
UITableView *table = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, 320, 460)];
table.dataSource=self;
table.delegate=self;
[table reloadData];
[self.view addSubview:table];
[table release];
//Add Gesture Recognizer
swipeLeftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom)];
[table addGestureRecognizer:swipeLeftRecognizer];
[swipeLeftRecognizer release];
在你的控制器中的某个地方;
-(void)handleSwipeFrom {
if (swipeLeftRecognizer.direction == UISwipeGestureRecognizerDirectionRight) {
// Your code here to go back to the main view
}
}
这只会对向右滑动做出反应,并让您的表格以正常方式运行。希望这可以帮助! :)
【解决方案2】:
经过长时间的谷歌搜索,我找到了解决方案。我正在使用宽度为 3 个屏幕的滚动视图,并在它的第一个屏幕框架上添加日历 xib 视图,在它的第二个屏幕框架上添加主 xib 视图,然后在它的第三个屏幕框架上添加列表 xib 视图,我创建了一个 UINavigationController *navigationHome;
id parentController;
在主屏幕上用于从滚动视图管理主屏幕导航并为其余控制器采用相同的技术,这对我来说很好。