Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 从ViewController调用AppController中的方法_Objective C_Cocoa - Fatal编程技术网

Objective c 从ViewController调用AppController中的方法

Objective c 从ViewController调用AppController中的方法,objective-c,cocoa,Objective C,Cocoa,我正在尝试为OSX Lion构建一个可可应用程序。 我的AppController代码中有这一行: self.viewController = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil appController:self]; [_view addSubview:[_viewController view]]; [[_viewController view] setFrame:[_

我正在尝试为OSX Lion构建一个可可应用程序。 我的AppController代码中有这一行:

self.viewController = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil appController:self];
[_view addSubview:[_viewController view]];
[[_viewController view] setFrame:[_view bounds]];
LoginViewController如下所示:

@implementation LoginViewController

@synthesize appController = _appController;

- (id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil appController:(AppController *)appController {

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self setAppController:appController];
        NSLog(@"Appcontroller init: %@", _appController);
    }    

    return self;
}

- (IBAction)login:(id)sender {

    NSLog(@"Appcontroller login: %@", _appController);
}
登录方法连接到按钮单击

日志:

2012-05-23 12:45:49.574 QBLoader[3241:503]应用控制器初始化:
2012-05-23 12:45:52.085 QBLoader[3241:503]Appcontroller登录:(空)

为什么第二个日志行为空?

既然您指出有多个
LoginViewController
实例,我会检查您的XIB,看看您是否在任何地方创建了该类型的对象。特别是我将从xib开始,在这里连接
-login:
操作。如果您只有一个
AppController
实例,常用的方法是在
main menu.xib
中创建对象,并将其连接到应用程序代理中的插座。然后您可以使用类似于
[[NSApp delegate]appController]
的方法来访问它。

您是否尝试在@synthetic行添加断点以查看值是否正在更改?您可能还需要记录self,以确保在两行中获得相同的对象。是否可能创建了多个
LoginViewController
实例,例如IB创建的一个?如果在两个
NSLog
s中使用
%p
打印出
self
,它们是否匹配?好的,似乎有多个LoginViewController实例。我不明白这怎么可能。我在xib文件中有一个LoginViewController对象。我删除了它,改为与文件所有者建立了所有连接。现在它工作得很好。谢谢你的帮助,没问题。我不知道你会这么快找到解决方案,所以我在网上发布了一个答案,让事情变得更加具体。
2012-05-23 12:45:49.574 QBLoader[3241:503] Appcontroller init: <AppController: 0x7fe2ab210440>
2012-05-23 12:45:52.085 QBLoader[3241:503] Appcontroller login: (null)