سؤال

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?

هل كانت مفيدة؟

المحلول

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

نصائح أخرى

Here's the answer. You add new files to your project, and just for giggles, we'll call them NewViewController of the UIViewController class. Then, highlight the second view controller in the storyboard, choose identity inspector, and under Custom Class (at the top) you change the class to the name of the recently added NewViewController class. Now, all your code will connect to objects and so on. Thanks again for the help all and now I can rest in peace lol.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top