实例变量在NSViewController中的initWithNib之后和IBAction之前丢失值 [英] Instance variables lose value after initWithNib and before IBAction in NSViewController

查看:195
本文介绍了实例变量在NSViewController中的initWithNib之后和IBAction之前丢失值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个OS X应用程序有一个非常基本的初始化问题。我在我的NSViewController中的initWithNibName中初始化实例变量。然而,这些变量失去它们的价值,并且在我在IBAction方法中访问它们的时候最终等于nil。

I have a very basic initialization problem with an OS X app I am writing. I am initializing instance variables in initWithNibName in my NSViewController. However, these variables lose their value and end up being equal to nil by the time I access them in my IBAction methods.

这是我的代码。我有一个应用程序有1个主窗口和1 NSViewController。应用程序委托在applicationDidFinishLoading中编程调用initWithNibName。视图出现,并且视图中的按钮成功调用onButtonClick。但是,在该方法中,myString的值为null。日志清楚地显示它在initWithNibName方法期间已成功初始化。所以它失去了它的值在initWithNibName和按钮点击之间。

Here is my code. I have an application that has 1 main window, and 1 NSViewController. The app delegate is programatically calling initWithNibName in applicationDidFinishLoading. The view appears, and the button in the view successfully calls onButtonClick. However, in that method, the value of myString is null. The logs clearly show that it was successfully initialized during the initWithNibName method. So it lost its value between initWithNibName and the button click.

//MyAppDelegate.m
@implementation MyAppDelegate

@synthesize window; 

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    MainView* mainViewController = [[MainView alloc] initWithNibName:@"MainView" bundle:[NSBundle mainBundle]];
    [mainViewController loadView];
NSView *controllerView = [mainViewController view];
    [window setContentView:controllerView];
}

@end

//MainView.h
@interface MainView : NSViewController {
    NSButton *myButton;
    NSString* myString;
}

@property (assign) IBOutlet NSButton *myButton;
@property (assign) NSString* myString;

- (IBAction)onMyButtonClick:(id)sender;

@end

//MainView.m
@implementation MainView

@synthesize myButton;
@synthesize myString;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    NSLog(@"initWithNibName");
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Initialization code here.
        NSLog(@"Initializing...");
        [self setMyString:@"try to set the variable"];
        NSLog(@"String is %@", myString);
    }

    return self;
}

- (void)awakeFromNib {
    NSLog(@"awakeFromNib");
    //[self setMyString:@"init in here works, but is called too many times"];
}

- (IBAction)onMyButtonClick:(id)sender{
    NSLog(@"Button clicked");
    NSLog(@"%@", myString);  //Always nil when initialized in initWithNibName()!!
}

@end


//Logs

initWithNibName
Initializing...
String is try to set the variable
awakeFromNib
awakeFromNib
Button clicked
(null)

如何获得myString保留其值?

How can I get myString to retain its value?

我已经尝试过每种类型的属性声明我可以想到。我也尝试过各种赋值方法([self setMyString],self.myString = @blah,myString = [NSString stringWithFormat:@blah]等)。

I have tried every type of property declaration I can think of. I have also tried various methods of assignment ([self setMyString], self.myString = @"blah", myString = [NSString stringWithFormat:@"blah"], etc).

我读过,我应该在awakeFromNib()中进行初始化。这实际上工作。但是,我的最终初始化代码将包括一系列的[[MyObj alloc] init]调用,当它们被多次调用时,会产生多个指针,并且从那里疯狂。我的最终目标是有一个指针指向一个对象,控制器总是可以引用并总是获得相同的数据。对象由控制器初始化,控制器保存引用,并在用户与控制器交互时根据需要更新它。

I have read that I should do my initialization in awakeFromNib(). This actually does work. However, my eventual initialization code will include a series of [[MyObj alloc] init] calls, and when they get called multiple times, multiple pointers result and it's madness from there. My end goal is to have one pointer to an object, which the controller can always refer to and always get the same data. The object gets initialized by the controller, which holds the reference and updates it as needed as the user interacts with the controller.

因此,有任何方法可以初始化实例变量NSViewControllers,只有一次,并让他们保留他们的价值为控制器的生命?

So is there any way to initialize instance variables in NSViewControllers, only once, and have them retain their value for the life of the controller?

感谢

Kev

推荐答案

允许我回答这个问题,因为正确答案埋在我的问题的意见。

Allow me to answer this since the correct answer is buried in the comments in my question.

我想出了我做错了什么。我添加了一个视图控制器到每个我的笔尖,代表视图的控制器。然后我将按钮点击附加到该单独的视图控制器对象。 @ Francis-McGrew附加了一个项目,显示我应该将按钮点击附加到文件所有者,然后将文件所有者设置为视图控制器。基本上不需要将第二个视图控制器导入到nib中,而只需使用文件所有者作为占位符。

I figured out what I was doing wrong. I was adding a view controller to each of my nibs that represented the controller for the view. Then I was attaching the button clicks to that separate view controller object. @Francis-McGrew attached a project that showed me that I should be attaching the button clicks to the File Owner, then setting the file owner to be the view controller. There is basically no need to import a second view controller into the nib when you can just use file owner as a placeholder instead.

这篇关于实例变量在NSViewController中的initWithNib之后和IBAction之前丢失值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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