iPhone,了解视图控制器,presentModalViewController [英] iphone, understanding the view controller, presentModalViewController

查看:57
本文介绍了iPhone,了解视图控制器,presentModalViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



感谢您的所有帮助,像往常一样,我正在寻找更多信息...


Thanks for all your help, as usual, i am looking for some more information...

关于视图控制器,我是尝试开发具有多个视图的应用程序。

Regarding view controllers, i am trying to develop an application with multiple views.

使用presentModalViewController从View A加载View Controller-并从那里加载View B-也可以,但是我有些na的问题...

View Controller loads from View A using presentModalViewController - and loads View B from there - also works fine, but i have some nagging questions...

视图A中有一个表,可深入到视图B,我可以移回视图A,使用us dismissModalViewController,但一旦返回视图A我无法访问视图A中的表。

View A has a table in it, which drills down to View B, and i can move back to view A usig dismissModalViewController, but once back in View A i can't access the table in View A.

按下视图B时,视图A中的控件/变量会发生什么?调用View B时调用了dealloc吗?如果我希望在视图B被关闭并且视图A重新显示时访问控件/变量,我该怎么办?

What happens to controls/variables in View A when View B is pushed? is the dealloc called when View B is called? If i wish to access controls/variables when View B is dismissed and View A comes back into view, what do i need to do?

是否有一些文章可以教人在这?
非常感谢您的帮助

Is there some article that can educate on this? ANy help is highly appreciated

要重申


  • View Controller从View x初始化页面

  • View X加载-使用presentModalViewController
  • 在视图A中释放带有表的视图A
  • 表dealloc

  • 视图B可以很好地加载,可以正常工作-关闭视图B控制器将带回到视图A

  • 此时,我想重新加载视图A中的表格一旦回到屏幕上

  • View Controller initiates a page from View x
  • View X loads - View A with a table into the view using presentModalViewController
  • table is released in View A's dealloc
  • View B loads fine, works fine - dismissing the view B controllers takes back to View A
  • At this point, i would like to reload the table in View A once it comes back onto the screen

我敢肯定,还有很多其他同类成员情况

I am pretty sure there are many other fellow members who are in this kind of situation

这是我的代码,点点滴滴

Here is my code, in bits and pieces

startPage-(这是应用程序的主页)和开始按钮,其中包含

startPage - (This is the application's home page) with a Start Button, contains

 View Controller -> View

单击开始按钮时,正在加载名为ViewA(View Controller-> View)的viewcontroller,并使用以下代码

On clicking the start button, am loading a viewcontroller called ViewA(View Controller -> View), with the following code

UIViewController *viewA= [[viewA alloc] initWithNibName:@"viewA" bundle:nil];
viewA.modalTransitionStyle=UIModalTransitionStyleCoverVertical;
[naviControl presentModalViewController:viewA animated:YES];    
[viewA release];   

在ViewA中选择表行时,将调用另一个viewcontroller ViewB(View Controller-> View),方法是以下代码

On picking a table row in ViewA, another viewcontroller ViewB(View Controller -> View) is called with the follwoing code

UIViewController *viewB= [[viewB alloc] initWithNibName:@"viewB" bundle:nil];
viewB=UIModalTransitionStyleCoverVertical;
[self viewB animated:YES];

关闭ViewB并用代码显示VIewA

ViewB is dismissed and VIewA is shown with code

[self dismissModalViewControllerAnimated:YES];

此时,对于某些NSLogs,关闭ViewB时,仅在ViewA上调用viewWillAppear,而不是viewDidLoad,如果我尝试在ViewA中重新加载表中的数据,则该应用程序将崩溃,并显示EXC_BAD_ACCESS

At this point, with some NSLogs, when ViewB is dismissed, only viewWillAppear is called on ViewA, not viewDidLoad and if i try to reload data on the table in ViewA, the app crashses with EXC_BAD_ACCESS

感谢
Veeru

Thanks Veeru

推荐答案

首先,请更正以下语句:

First of all, please correct following statement:

UIViewController *viewA= [[viewA alloc] initWithNibName:@"viewA" bundle:nil];

UIViewController *viewA= [[UIViewController alloc] initWithNibName:@"viewA" bundle:nil];

其次,您需要了解内存管理和视图控制器的流程,例如

and secondly you will need to understand the memory management and flow of view controllers, e.g.


  1. 在分配视图控制器时,会在内存中启动保留计数为1的实例,

  2. 当您将此视图控制器推入或显示到屏幕上时,其保留计数变为2,因为现在另一个实例(导航控制器或视图控制器正在使用您的实例),

  3. 然后释放它这会使视图控制器实例的保留计数再次变为1,即减少1(必须注意,当保留计数变为0时,只有它将从内存中释放出来)。

  4. 然后,您在此当前视图控制器之上调用了另一个视图控制器,这使其在后台运行,并且由于您尚未明确保留实例,因此导航控制器将其释放或添加了它的视图控制器,因此其保留计数变为0,因此从内存中释放出来。

  5. 因此,当您尝试重新加载tableview时,它会为您提供EXC_BAD_ACCESS,因为您尝试使用已释放的实例。

  1. when you allocate a view controller then an instance with retain count 1 is initiated in the memory,
  2. when you push or present this view controller on to the screen then its retain count becomes 2 as now another instance either navigation controller or view controller is using your instance,
  3. then you release it this makes retain count of your view controller's instance again to 1, i.e. reduces by 1, (you must be aware that when the retain count becomes 0 then only it will be freed from memory).
  4. then you have called another view controller on top of this current view controller which makes it to go in background and as you haven't retained the instance explicitly it is getting released by your nav controller or view controller in which you added, so its retain count becomes 0, hence freed from memory.
  5. so when you try to reload tableview it is giving you EXC_BAD_ACCESS as you are trying to use an instance which is already freed.

解决方案:


  1. 如果尝试在应用程序中使用多个导航屏幕,则应仅使用UINavigationController的pushViewController方法,该方法将保留viewcontroller的实例,直到您明确释放它们为止。

  1. If you are trying to use multiple navigation screens in your application, you should only use pushViewController method of UINavigationController which keeps the instances of viewcontroller until you explicitly release them.

还请小心使用本地实例或对象实例及其保留计数,跟踪分配和释放,即保留计数,除非您需要,否则不应为0。

Also be careful of using local instances or object instances and their retain count, keep track of alloc and release i.e. retain count, it should not be 0 unless you want them.

H欧普这将有助于...

Hope this will help...

这篇关于iPhone,了解视图控制器,presentModalViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆