从子视图控制器切换到父视图控制器 - iOS
我有一个主要的动作视图控制器,并在那里有按钮“审查”。评论按钮的功能是:从子视图控制器切换到父视图控制器 - iOS
- (IBAction)btnReview:(id)sender
{
ReviewViewController *vc = [[ReviewViewController alloc]initWithNibName:@"ReviewViewController" bundle:nil];
[self addChildViewController:vc];
[vc didMoveToParentViewController:self];
[self.view addSubview:vc.view];
}
现在在审查页面,我有一个按钮。在该按钮的操作上,我想切换回父视图控制器。应该显示它的视图。我试过下面的代码,它会暂停或崩溃应用程序。
[self didMoveToParentViewController:nil];
[self.view removeFromSuperview];
[self removeFromParentViewController];
我甚至尝试:
[self dismissModalViewControllerAnimated:YES]
我已阅读有关这个问题以及其他的职位,但未能找到满意的答案。请帮忙!
听起来像你要找的可以照顾一个UINavigationController
。只需使用pushViewController:
即可将新视图推送到屏幕上。然后,返回按钮将出现在视图顶部的导航栏中,该按钮可用于返回到“父级”视图控制器。
请参阅documentation。
不,我没有使用UINavigationController,因为我的应用程序是基于Tab的。 – user2334616 2013-04-30 06:14:31
然后请参阅Levi的回答。 – 2013-04-30 06:20:05
您应该使用委派。当您点击ReviewViewController
按钮,你可以调用的方法等:[self.delegate hideReviewController:self];
而且这种方法看起来是这样的:
- (void)hideReviewController:(ReviewViewController *)controller {
[UIView animateWithDuration:0.3
delay:0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
controller.view.alpha = 0;
} completion:^(BOOL finished) {
[self.view removeSubview:controller.view];
// If you want the Review Controller to be deallocated:
[self removeChildViewController:controller];
}];
}
编辑: 在你ReviewDelegate.h文件中加入:
@class ReviewViewController;
@protocol ReviewDelegate <NSObject>
- (void)hideReviewController:(ReviewViewController *)controller;
@end
然后添加该属性:
@property (nonatomic, weak) id <ReviewDelegate> delegate;
在父类:
- (IBAction)btnReview:(id)sender
{
ReviewViewController *vc = [[ReviewViewController alloc]initWithNibName:@"ReviewViewController" bundle:nil];
vc.delegate = self;
[self addChildViewController:vc];
[vc didMoveToParentViewController:self];
[self.view addSubview:vc.view];
}
- (void)hideReviewController:(ReviewViewController *)controller {
[UIView animateWithDuration:0.3
delay:0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
controller.view.alpha = 0;
} completion:^(BOOL finished) {
[self.view removeSubview:controller.view];
// If you want the Review Controller to be deallocated:
[self removeChildViewController:controller];
}];
}
我也建议你阅读delegation
你并不需要让复杂的...只是使用UINavigationController
。
要导航从ParentVC
到ChildVC
:
ChildViewController *ChildVC = [[ChildViewController alloc]initWithNibName:@"ChildViewController" bundle:nil];
[self.navigationController pushViewController:ChildVC animated:YES];
而对于导航从ChildVC
回ParentVC
:
ParentViewController *ParentVC = [[ParentViewController alloc]initWithNibName:@"ParentViewController" bundle:nil];
[self.navigationController popViewControllerAnimated:YES];
从ParentVC导航到ChildVC工作正常,但从ChildVC导航回ParentVC时崩溃。 – user2334616 2013-04-30 06:27:05
你是否实现了'pushViewController:'从ParentVC导航到ChildVC? – 2013-04-30 06:30:27
您是否在您的父VC的界面文件以及ChildVC中实现了'UINavigationControllerDelegate'。 – 2013-04-30 06:36:38
既然你要添加的子视图作为一个子视图到父,你需要手动它从它的超级视图(在你的情况下,父视图)。而不是将其添加为一个子视图,您可以利用presentViewController方法如下:
- (IBAction)btnReview:(id)sender
{
ReviewViewController *vc = [[ReviewViewController alloc] initWithNibName:@"ReviewViewController" bundle:nil];
[self presentViewController:vc
animated:YES
completion:nil];
}
,并在后退按钮子类代码将是:
-(IBAction)backButtonClicked:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
}
我假定这是您正在尝试构建的自定义视图控制器容器,“self”是您正在讨论的主要操作视图控制器。
试试这个:
- (IBAction)btnReview:(id)sender
{
ReviewViewController *vc = [[ReviewViewController alloc]initWithNibName:@"ReviewViewController" bundle:nil];
[self addChildViewController:vc];
[self.view addSubview:vc.view];
[vc didMoveToParentViewController:self];
vc = nil;
}
对于 “返回” 按钮,我假设这是返回,从审查的viewController主要处理措施的viewController按钮,试试这个:
- (IBAction)btnReviewHide:(id)sender
{
[self willMoveToParentViewController:nil];
[self.view removeFromSuperview];
[self removeFromParentViewController];
}
希望这可以帮助。
试试这个
self.view=nil;
[self willMoveToParentViewController:nil];
[self.view removeFromSuperview];
[self removeFromParentViewController];
只写
- (IBAction)backbutton:(id)sender {
[self.navigationController popToRootViewControllerAnimated:YES];
}
曾在我的情况下
不知道的是,这里的问题,但你不应该叫'didMoveToParentViewController:'用一个'nil'参数,'removeFromParentViewController'应该在视图控制器被移除后自动调用它。 – omz 2013-04-30 06:12:50
即使我删除[self didMoveToParentViewController:nil] ;,它仍会暂停应用程序,并在稍后崩溃。 – user2334616 2013-04-30 06:16:27