Bind Action to UIButton

后端 未结 1 663
旧时难觅i
旧时难觅i 2021-02-05 17:57

In my AppController I\'m loading a View with the following code.

- (void) loadSettingsController {
    settingsViewController = [[SettingsViewController alloc]          


        
1条回答
  •  别那么骄傲
    2021-02-05 18:11

    Change IBOutlet to IBAction

    - (IBAction) saveSettings:(id) sender
    

    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.

    add this to your SettingViewController viewDidLoad function

     [[NSNotificationCenter defaultCenter] postNotificationName:@"SettingsViewDidLoad" object:nil];
    

    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];
    }
    

    0 讨论(0)
提交回复
热议问题