问题描述
在我的AppController中,我加载一个视图与以下代码。
In my AppController I'm loading a View with the following code.
- (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
该视图稍后添加到带有代码的窗口
The view is later on added to the window with the code
[window addSubview:[settingsViewController view]];
一切正常,但是当我按下按钮时saveSettings调试器。 loginButton属性连接到Interface Builder中的按钮。
Everything works fine, but the action saveSettings doesn't get called when I press the button while using the debugger. The "loginButton" property is connected to the button in the Interface Builder.
您可以看到此代码有什么问题吗?
Can you see anything wrong with this code?
推荐答案
将IBOutlet更改为IBAction
Change IBOutlet to IBAction
- (IBAction) saveSettings:(id) sender
这是因为您尝试访问viewDidLoad被调用之前的对象。你必须等待viewDidLoad调用之前,有很多方法来实现这一点。
It's because you try to acces your object before viewDidLoad got called. you must wait before viewDidLoad got called, there are numerous ways to achieve that.
将它添加到你的SettingViewController viewDidLoad函数
add this to your SettingViewController viewDidLoad function
[[NSNotificationCenter defaultCenter] postNotificationName:@"SettingsViewDidLoad" object:nil];
,并将其传递给您要访问按钮的ViewController
and this to your the ViewController you want to access the button
- (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];
}
这篇关于将操作绑定到UIButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!