Search code examples
xcodexcode5xcode-storyboard

Connecting code to UI in storyboard with multiple ViewControllers, Xcode


I have a storyboard project in Xcode. I have added an additional ViewController and dropped in a button, text box, etc. I can get from the initial to the secondary View Controller, but I cannot seem to be able to connect the button to the code. I've tried adding additional classes with XIBs and even from a blank adding in the XIBs. How do I connect the code I have written for a button to do work on the second View Controller?


Solution

  • If you have the button created on the storyboard. You have to make an

    -(IBAction)clickButton...
    

    in the .h and .m code files. You can easily connect the code to the button by dragging the line from the dot (in front of the code name) to the button.

    If you have created the Button fully in code. You have to specify which code to be executed on click.

    [self.buttonName addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];