问题描述:

How do I add code for a button on a ViewController? For example, in VisualStudio, I can simply double-click on a button and it will take me to an auto-genned clicked handler for the button in the codebehind. How do I go about doing something similar to this in xcode?

网友答案:

Steps to generate button code. 1. Create button using drag and drop from the list of components. Now click on this button(shown in second image ) It will automatically directs you in your code file. But if some problems occurs to direct you jn your code file. Follow this steps. 1. Click on your viewController where you added button. (In viewcontroller scene) 2. Click on the identity inspector check the class name. 3.Check same class name is there in the second part. If its same than now you can simple using control + three finger and drag it to your .swift file it will open it like this. Now select action in connection to get button click action. And remains outlet to get the button property.

But if you are not getting the same file select here and find your file name in this hierarchy. After getting file do same process again.

网友答案:

Just do what I say it's simple.

1.Open File Navigator and Select your View's viewController.h file.

2.Now left click on your button and click+control drag to viewController.h (make sure you drag between @interface ---- @end).

3.Give name to your method and make sure You select Action in Connection option.

enter image description here

4.Click connect and you will have your method declaration on viewcontroller.h .

5.Now switch to viewController.m and there you'll find your method at the end and there you will write all the defination of your method.

enter image description here

相关阅读:
Top