2013-05-02 65 views
2

在多个UIViewControllers一起工作的应用程序中,需要有关UIViewController的帮助

firstViewController添加到根。直到现在,它的罚款现在我想去secondViewController我不想使用UINavigationControllerUITabBarController。我已阅读View Controller Programming Guide,但未指定UINavigationController, UITabBarController and story board

当用户想从secondViewController移动到firstViewController如何secondViewController将被销毁?

Apple Doc还没有指定如何释放或销毁UIViewController?它只能告诉UIViewController内的生命周期。

+0

@MartinR我想邀请您回答这个问题。 – 2013-05-02 08:42:57

+0

如上所述替换窗口的根视图控制器。在这里:http://stackoverflow.com/questions/16334041/re-assigning-rootviewcontroller-after-successful-login可能是一个选项。 – 2013-05-02 09:14:21

+0

你应该使用UIViewController类的iOS 6的功能“childViewControllers”,这样说的:实现一个容器视图控制器@ http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIViewController_Class/Reference/Reference。 html – 2013-05-02 09:27:52

回答

2

如果您担心的UIViewController如何释放或然后销毁这里有一个场景你: -

这里是FirstViewController一个键击方法呈现(使用pushViewController,presentModalViewController SecondViewController等)

在FirstViewController.m文件

- (IBAction)btnTapped { 

    SecondViewController * secondView = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil]; 
    NSLog(@"Before Present Retain Count:%d",[secondView retainCount]); 
    [self presentModalViewController:secondView animated:YES]; 
    NSLog(@"After Present Retain Count:%d",[secondView retainCount]); 
    [secondView release]; //not releasing here is memory leak(Use build and analyze) 
} 

现在在SecondViewController中。运行代码M档

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    NSLog(@"View Load Retain Count %d",[self retainCount]); 
    } 

- (void)dealloc { 
    [super dealloc]; 
    NSLog(@"View Dealloc Retain Count %d",[self retainCount]); 
} 

后:

前推保留计数:1
查看负载保留计数3
推保留计数后:4
查看dealloc的保留计数1

如果你是alloca并初始化一个ViewController,你是它的生命周期的所有者,你必须在push或modalPresent之后发布它。 在上述输出时分配初始化保留SecondViewController的计数是一个,,,,令人惊讶但在逻辑上它的保留计数仍然是一个即使它已被释放(见dealloc方法),所以要求在FirstViewController释放到彻底摧毁它。

+0

超棒:)。 ..非常感谢 – 2013-05-02 10:39:43

+0

请你也可以回答这个问题http://stackoverflow.com/questions/16335537/what-are-all-the-possible-ways-to-jump-from-one-view-controller-to -otherother-and-b – 2013-05-02 10:41:09

+0

谢谢**“快乐帮忙”** – 2013-05-02 11:07:00

1

提出一个新的视图控制器做它像一个模态视图控制器其他方式(注意自我是firstViewController):

[self presentModalViewController:secondViewController animated:YES]; 

那么,当你婉回来的firstViewController并摧毁secondViewController ,您必须关闭视图控制器(来自secondViewController):

[self dismissModalViewControllerAnimated:YES]; 

希望有所帮助。

+0

抱歉,我也忘记提及模型视图演示文稿,我也不想使用它。在没有导航或标签栏控制器或模型视图演示的游戏中。他们如何从一个视图控制器跳到另一个 – 2013-05-02 09:25:16

+0

你可以有动画属性为NO然后 – Firdous 2013-05-02 09:34:14

+0

@HéctorDarioArroyoGóm我想请你回答这个问题http://stackoverflow.com/questions/17657719/please-clear-some-confusions-regarding-uiviewcontroller – 2013-07-16 05:25:42

1

您可以使用UINavigationController移动到secondViewController,并通过将UINavigationController属性'navigationBarHidden'设置为YES返回。这将隐藏导航栏。视图控制器的释放和销毁将由此照顾。

0

然后,你可以采取其他的策略,不是最好的建立你的视图控制器层次结构,但它也可以工作。您可以覆盖secondViewContrller的观点在firstViewController的,使secondViewController成为从firstViewController孩子:

//... 
[self addChildViewController:secondViewController]; 
[self.view addSubview:secondViewContrller.view]; 
//... 

而且当你要删除的视图控制器,你必须删除视图,并索要视图控制器从他的父母删除:

//... 
[self.view removeFromSuperview]; 
[self removeFromParentViewController]; 
//... 

但随后你将不得不自己(把和自己删除视图和视图控制器)来控制视图层次结构。