我有一个viewController,它使用tableview显示查询结果。 通过按一行,我按下一个childView,然后我设置一个navigationBar,右边有两个按钮(上一个/下一个)。 我的问题是: 当我点击上一个或下一个按钮时,如何切换到上一个或下一个“childView”? 我希望在视图切换时也有过渡效果? 有什么帮助吗?
答案 0 :(得分:3)
我有一个视图,其中包含一个营地列表,触摸一个用户访问营地详细信息。我希望允许用户左右滑动以在营地列表中移动,显示每个营地的详细信息。我想要一个显示滑动的视觉动画,并且“后退”按钮总是指向后移动导航堆栈,即返回列表。
包含结果的表的第一个视图是唯一知道“previous”和“next”含义的对象,因此他们将负责实现滑动操作。因为我们将对该视图中的“当前显示的”行/部分进行更改,而视图不是当前视图,您需要添加vars / properties来跟踪它。
因为导航控制器已经可以为视图更改设置动画,所以我让它完成大部分工作。当移动“previous”时,我创建一个包含先前条目细节的新视图,将其插入导航堆栈(在LIST视图和当前DETAIL视图之间),并执行popViewControllerAnimated,它提供视觉效果并卸载详细信息视图只是动画了。
要转到“下一个”阵营详细信息,我创建一个包含下一个条目详细信息的新视图,将其添加到导航堆栈的末尾,它会动画显示,然后我通过删除详细信息视图来清理导航堆栈只是动画了。
所以,简而言之: 详细信息视图将检测滑动手势并通知父级。 父级确定应显示的下一行/上一行。 父级用新的替换当前视图,使替换左/右动画以直观地指示效果。父级还会更新导航堆栈,以确保“后退”按钮始终充当LIST视图的弹出窗口,而不是先前显示的详细视图。
我不能在这里发布我的所有代码,但下面是大多数。一些特别的GOTCHAS需要注意:
如果您尝试在动画制作时删除VC,则操作导航堆栈可能会在运行时导致警告错误。因此,我们等到它被完全替换,然后将其删除,通过注册为导航控制器的委托,并使用“didShowViewController”方法来检测何时可以安全地进行更改。只有在列表中向前移动时才需要这种复杂性,因为在“后退”逻辑中,导航控制器会在popViewController之后自行清理。
要使用didShowViewController,您必须设置委托。代表不能是可能消失的VC,否则你会崩溃。我只有控制LIST视图将自己设置为委托。
当您操纵用户正在查看详细信息的行/部分时,我还会在隐藏的LIST视图上移动突出显示(并滚动表格),以便当它们最终“返回”它时,显示上次查看的项目。
创建DETAIL视图时,传入父视图,定义方法让父母知道滑动,并在DETAILDidLoad方法的DETAIL视图中注册滑动识别器,
LIST视图中的代码(父级)
-(NSString *) nameOfPreviousCampAndUpdateCurrents;
{
// pseudo code
// targetsection = srcSection
// targetrow = srcRow-1.
// if targetrow < 0
// targetsection = srcSection - 1
// targetrow = last row of targetsection
// if targetSection < 0
// return nil;
//
// return name at targetsection, targetrow
NSInteger targetSection;
NSInteger targetRow;
NSString *results = nil;
targetSection = self.currentDetailViewSection;
targetRow = self.currentDetailViewRow-1;
if (targetRow < 0)
{
targetSection--;
if (targetSection <0)
{
return nil;
}// end if
NSInteger numberOfRowsInSection = [self tableView:self.myTable numberOfRowsInSection:targetSection];
targetRow = numberOfRowsInSection-1;
}// end if
results = [self getCampNameInSection:targetSection atOffset:targetRow];
self.currentDetailViewSection = targetSection;
self.currentDetailViewRow = targetRow;
return results;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
CampDetails *detailViewController = [[[CampDetails alloc] initWithNibName:nil bundle:nil] autorelease];
detailViewController.campName = [self getCampNameInSection:indexPath.section atOffset:indexPath.row];
detailViewController.campID = [self getCampIDForSection:indexPath.section atOffset:indexPath.row];
detailViewController.parent = self;
self.currentDetailViewSection = indexPath.section;
self.currentDetailViewRow = indexPath.row;
// ...
// Pass the selected object to the new view controller.
[[self navigationController] pushViewController:detailViewController animated:YES];
//[detailViewController release];
}
-(void) viewDidLoad;
{
// The ROOT view controller should do this.
if ([[self.navigationController viewControllers] count] == 1)
{
self.navigationController.delegate = self;
}// end if
}
-(void) moveToNextCamp;
{
NSString *nextCamp = [self nameOfNextCampAndUpdateCurrents];
if (nextCamp == nil)
{
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Warning"
message:@"You are already at the last item in the list of camps."
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
return;
}// end if
CampDetails *detailViewController = [[[CampDetails alloc] initWithNibName:nil bundle:nil]autorelease];
detailViewController.campName = nextCamp;
detailViewController.campID = [self getCampIDForSection:self.currentDetailViewSection atOffset:self.currentDetailViewRow];
detailViewController.parent = self;
// do the animation to the right
[self.navigationController pushViewController:detailViewController animated:YES];
// remove the previous controller so that popping the current one takes us "up"
// WHILE THE FOLLOWING CODE DOES WORK, it also results in a runtime warning.
// so instead, we tinker with the controller stack only when it's safe (see below)
// NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
// [viewControllers removeObjectAtIndex:1];
// [self.navigationController setViewControllers:viewControllers animated:NO];
//
// clean up the stack AFTER the child is shown.
self.userJustSwiped = YES;
[self updateTableHighlightAndScrollPosition];
}
-(void) moveToPreviousCamp;
{
NSString *previousCamp = [self nameOfPreviousCampAndUpdateCurrents];
if (previousCamp == nil)
{
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Warning"
message:@"You are already at the first item in the list of camps."
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
return;
}// end if
CampDetails *detailViewController = [[[CampDetails alloc] initWithNibName:nil bundle:nil]autorelease];
detailViewController.campName = previousCamp;
detailViewController.campID = [self getCampIDForSection:self.currentDetailViewSection atOffset:self.currentDetailViewRow];
detailViewController.parent = self;
// add the controller so that popping the current one takes us there
NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
NSInteger lastNavStackEntryIndex = [viewControllers count]-1;
[viewControllers insertObject:detailViewController atIndex:lastNavStackEntryIndex];
[self.navigationController setViewControllers:viewControllers animated:NO];
// do the animation (which also releases the previously current vc)
[self.navigationController popViewControllerAnimated:YES];
[self updateTableHighlightAndScrollPosition];
}
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
// IF we just swiped to a details view, do some clean up
if (self.userJustSwiped)
{
self.userJustSwiped = NO;
// clean up the stack AFTER the child is shown. remove the previous controller so that popping the current one takes us "up"
NSMutableArray *viewControllersArray = [NSMutableArray arrayWithArray:navigationController.viewControllers];
NSInteger lastNavStackEntryIndex = [viewControllersArray count] - 1;
[viewControllersArray removeObjectAtIndex:lastNavStackEntryIndex-1];
[navigationController setViewControllers:viewControllersArray animated:NO];
}// end if
}
-(void) userSwipedLeftOnChild;
{
[self moveToNextCamp];
}
-(void) userSwipedRightOnChild;
{
[self moveToPreviousCamp];
}
DETAILS视图中的代码(子):
-(void) leftSwipe:(UIGestureRecognizer*)recognizer;
{
[self.parent userSwipedLeftOnChild];
}
-(void) rightSwipe:(UIGestureRecognizer*)recognizer;
{
[self.parent userSwipedRightOnChild];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// add swipe recognizers
UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwipe:)];
[leftSwipe setDirection:UISwipeGestureRecognizerDirectionLeft];
[self.view addGestureRecognizer:leftSwipe];
[leftSwipe release];
UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwipe:) ];
[rightSwipe setDirection:UISwipeGestureRecognizerDirectionRight];
[self.view addGestureRecognizer:rightSwipe];
[rightSwipe release];
}
答案 1 :(得分:1)
点击这些按钮时,您可以轻松地使用动画推送和弹出视图控制器。
如果您需要帮助,请告诉我。
答案 2 :(得分:1)
使用自动拥有后退按钮的UINavigationController,然后只需在右侧添加下一个按钮。
或者只是隐藏导航控制器的栏并添加你自己的两个按钮(我认为你当前的方法)。
在导航控制器上按下/弹出viewcontroller:
推进到下一个vc:
[myNavController pushViewController:vc animated:YES];
弹回到最后一个vc:
myNavController popViewControllerAnimated:YES];
弹出回到堆栈上的第3个vc:
[myNavController popToViewController:[myNavController.viewControllers objectAtIndex:2] animated:YES];
注意,如果您想最大限度地减少内存使用量,请使用您自己的导航栏和您自己的按钮,并确保您只使用自己的编码索引/数量来处理虚拟堆栈中的两个视图控制器。