在我的AppController中,我正在使用以下代码加载View.
- (void) loadSettingsController { settingsViewController = [[SettingsViewController alloc] initWithNibName:@"SettingsView" bundle:nil]; UIButton *button = settingsViewController.loginButton; [button addTarget:self action:@selector(saveSettings:) forControlEvents:UIControlEventTouchUpInside]; } - (IBOutlet) saveSettings:(id) sender
稍后将视图添加到带有代码的窗口中
[window addSubview:[settingsViewController view]];
一切正常,但在使用调试器时按下按钮时,不会调用saveSettings操作."loginButton"属性连接到Interface Builder中的按钮.
你能看到这段代码有什么问题吗?
将IBOutlet更改为IBAction
- (IBAction) saveSettings:(id) sender
这是因为你在调用viewDidLoad之前尝试访问你的对象.你必须在viewDidLoad被调用之前等待,有很多方法可以实现.
将其添加到您的SettingViewController viewDidLoad函数中
[[NSNotificationCenter defaultCenter] postNotificationName:@"SettingsViewDidLoad" object:nil];
这对你想要访问按钮的ViewController
- (void) loadSettingsController { settingsViewController = [[SettingsViewController alloc] initWithNibName:@"SettingsView" bundle:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(initControls) name:@"SettingsViewDidLoad" object:nil]; } -(void)initControls{ UIButton *button = settingsViewController.loginButton; [button addTarget:self action:@selector(saveSettings:) forControlEvents:UIControlEventTouchUpInside]; }