ARC似乎正在释放我的NSViewController的视图 [英] ARC seems to be releasing the view of my NSViewController

查看:58
本文介绍了ARC似乎正在释放我的NSViewController的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解决一个更大的问题,并且我在指出ARC显然为时过早将视图发布到我的NSViewController的事实.我认为:)因此,我创建了一个简单的应用程序来重建情况.

I am trying to solve a larger problem and I am tipping on the fact that ARC apparently is releasing the view to my NSViewController too early. I think :) So I created a simple app to reconstruct the situation.

我有一个简单的ARC Cocoa应用程序.在 MainMenu.xib 的窗口中,我将 Custom View 连接到 @property(强)IBOutlet NSView * theView; AppDelegate.h

I have a simple ARC Cocoa application. In the Window of the MainMenu.xib I hook up a Custom View to a @property (strong) IBOutlet NSView *theView; which is declared in the AppDelegate.h

AppDelegate.m 中,我合成了该属性,然后调用以下内容:

In the AppDelegate.m I synthesize the property and then call the following:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    TestViewController *tvc =  [[TestViewController alloc] initWithNibName:@"TestViewController" bundle:nil];
    [_theView addSubview:[tvc view]];
}  

TestViewController 会显示在自定义视图中-没问题.它包含一个NSButton.它被连接到名为-(IBAction)btnPressed:(id)sender 的方法和一个NSTextView,该方法也被连接为 IBOutlet .

The TestViewControllergets displayed in the Custom View - no problem. It contains one NSButton. It is hooked up to a method called -(IBAction)btnPressed:(id)sender and one NSTextView which is also hooked up as an IBOutlet.

TestViewController.h 中,我声明:

@property (nonatomic, strong) IBOutlet NSTextField *textField;
@property (nonatomic, strong) NSString *theString;

-(IBAction)btnPressed:(id)sender;

然后在 TestViewController.m 中执行

@synthesize theString = _theString;
@synthesize textField = _textField;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Initialization code here.
        _theString = @"Hello World";
    }

    return self;
}

-(IBAction)btnPressed:(id)sender
{
    [_textField setStringValue:_theString];
}

当我运行应用程序并按按钮时,它崩溃了.如果我检查是否有僵尸,则会收到以下消息:

When I run the app and press the button it crashes. If I check it for zombies I receive the following:

#   Address Category    Event Type  RefCt   Timestamp   Size    Responsible Library Responsible Caller
0   0x7f97a3047560  TestViewController  Malloc  1   00:00.652.631   128 TestARC -[AppDelegate applicationDidFinishLaunching:]
1   0x7f97a3047560  TestViewController  Retain  2   00:00.653.088   0   TestARC -[TestViewController initWithNibName:bundle:]
2   0x7f97a3047560  TestViewController  Release 1   00:00.653.089   0   TestARC -[TestViewController initWithNibName:bundle:]
3   0x7f97a3047560  TestViewController  Retain  2   00:00.653.912   0   AppKit  -[NSNib instantiateNibWithOwner:topLevelObjects:]
4   0x7f97a3047560  TestViewController  Release 1   00:00.658.831   0   AppKit  -[NSNib instantiateNibWithOwner:topLevelObjects:]
5   0x7f97a3047560  TestViewController  Release 0   00:00.662.377   0   Foundation  -[NSNotificationCenter postNotificationName:object:userInfo:]
6   0x7f97a3047560  TestViewController  Zombie  -1  00:01.951.377   0   AppKit  -[NSApplication sendAction:to:from:]

我做错了什么?谢谢

推荐答案

添加属性以保存视图控制器.您的控制器当前没有任何东西可以让它保持生命,直到分配它的方法结束为止.

Add a property to hold the view controller. Your controller currently has nothing to keep it alive past the end of the method that allocates it.

添加:

@property (strong) TestViewController *tvc;

修改:

self.tvc =  [[TestViewController alloc] initWithNibName:@"TestViewController" bundle:nil];

(我很好奇...如果只需要包含视图的视图,创建视图控制器的目的是什么?)

(I'm curious...what do you see as the point of creating a view controller if all you want is the view it contains?)

关于一般方法,似乎这是更正确的行为,应该使用容器视图控制器来实现.该机制允许多个视图控制器以有组织的方式共享屏幕.

Concerning the general approach, it seems that this is more properly behavior that should be implemented using a container view controller. That mechanism allows multiple view controllers to share the screen in an organized way.

这篇关于ARC似乎正在释放我的NSViewController的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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