iOS - 多视图应用程序 - 内存问题

发布于 2024-12-03 18:39:29 字数 1648 浏览 3 评论 0原文

我有一个 MultiView 应用程序,我有一些内存问题,我希望得到一些建议。 我有一个应用程序,它最初加载一个开关控制器,使用户能够在某些视图之间进行更改。在应用程序期间的某个时刻,我想删除 switchview 控制器并向窗口添加另一个子视图。因此,我获得了对共享应用程序的委托的访问权限,并删除了 switchview 控制器并插入了第二个。我不明白如果这是正确的方法,我担心会发生内存泄漏,因为我打印了第二个控制器的保留计数值,它显示 19!!!!!

下面是我的代码的快照。这是正确的方法吗?如何避免这些内存泄漏?

好的,在我的 ApplicationDelegate 中,我有两个视图控制器,我也在

MyAppDelegate.h

@class SwitchViewController;
@class SecondController;

@interface MyAppDelegate : NSObject <UIApplicationDelegate> {

    SwitchViewController *switchViewController;
    SecondController *secondController;  
}


@property (nonatomic, retain) IBOutlet SwitchViewController *switchViewController;
@property (nonatomic, retain) IBOutlet SecondController *secondController;

@property (nonatomic, retain) IBOutlet UIWindow *window;

@end

.m 文件中将它们设置为属性,我添加了

[self.window addSubview:switchViewController.view];
[self.window makeKeyAndVisible];

通知,我正在合成这些控制器并在 dealloc 函数中释放它们

现在这是我的问题!在 SwitchViewController.m 中,我想访问我的应用程序的委托,删除当前的 SwitchViewController 并将我的第二个控制器作为窗口的子视图:

 SwitchViewController.m

 SecondController *secondController2= [[SecondController alloc] init];

 MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];

 [appDelegate.switchViewController.view removeFromSuperview];

  appDelegate.secondController = secondController2;

  [appDelegate.window addSubview:appDelegate.secondController.view];

  [secondController2 release]; 

这是问题。当我打印出 [appDelegate.secondController keepCounter] 时,我得到 19。这是正确的方法吗?我真的有内存泄漏吗?

预先感谢,

安德烈亚斯

I have a MultiView app and I have some memory concerns and I would appreciate some advice..
I have an application which initially loads a switch controller which enables the user to change between some views. On some point during the application I want to remove the switchview controller and add another subview to the window..Therefore, I gained access to the delegate of the shared Application and removed the switchview controller and inserted the second one.. I do not understand if this is the right approach to do it and i am afraid that memory leaks will occur since I print the retainCount value of the second controller and it shows 19!!!!!

Below is snapshots of my code.. Is this the right approach? How do I avoid these memory leaks?

Ok in my ApplicationDelegate I have two view controllers which I also set as properties

MyAppDelegate.h

@class SwitchViewController;
@class SecondController;

@interface MyAppDelegate : NSObject <UIApplicationDelegate> {

    SwitchViewController *switchViewController;
    SecondController *secondController;  
}


@property (nonatomic, retain) IBOutlet SwitchViewController *switchViewController;
@property (nonatomic, retain) IBOutlet SecondController *secondController;

@property (nonatomic, retain) IBOutlet UIWindow *window;

@end

in the .m file I add

[self.window addSubview:switchViewController.view];
[self.window makeKeyAndVisible];

notice that I am synthesizing those controllers and release them in the dealloc function

Now here is my problem! In the SwitchViewController.m I want to gain access to the delegate of my App remove the current SwitchViewController and place my secondController on, as a subview of the window:

 SwitchViewController.m

 SecondController *secondController2= [[SecondController alloc] init];

 MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];

 [appDelegate.switchViewController.view removeFromSuperview];

  appDelegate.secondController = secondController2;

  [appDelegate.window addSubview:appDelegate.secondController.view];

  [secondController2 release]; 

Here is the question. When I print out [appDelegate.secondController retainCounter] I get 19. Is this the right approach. Do I actually have memory leaks?

Thanks in advance,

Andreas

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蝶…霜飞 2024-12-10 18:39:29

你的方法看起来不错,但除了目测之外,还有更好的测试方法。使用 Mac 附带的 Instruments 工具来测试是否存在泄漏。

另外,作为旁注,有一种更好的方法来进行过渡,

[UIView transitionFromView:appDelegate.switchViewController.view 
                    toView:appDelegate.secondController.view
                  duration:1.0 
                   options:UIViewAnimationOptionTransitionNone 
                completion:nil];

希望这会有所帮助。

Your approach looks sound but there are better ways to test than just eyeballing it. Use the Instruments tool that comes with your mac to test if there are leaks.

Also, as a side note, there is a nicer way to do transitions

[UIView transitionFromView:appDelegate.switchViewController.view 
                    toView:appDelegate.secondController.view
                  duration:1.0 
                   options:UIViewAnimationOptionTransitionNone 
                completion:nil];

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文