如何在XCode 4.2中自动生成协议的存根?

时间:2022-11-07 09:28:40

Can Xcode 4.2 auto-generate me the stubs for the protocols i defined in the header-file?

Xcode 4.2可以自动生成我在头文件中定义的协议的存根吗?

In this tutorial (http://kurrytran.blogspot.com/2011/10/ios-5-storyboard-and.html) in the note of point 4 the author says, Xcode will now auto generate the methods i need. Otherwise I did something wrong...

在本教程(http://kurrytran.blogspot.com/2011/10/ios-5-storyboard-and.html)中,作者在第4点的注释中说,Xcode现在将自动生成我需要的方法。否则我做错了什么......

4 个解决方案

#1


2  

From your question, I can't understand exactly what you need.

从你的问题来看,我无法准确理解你的需求。

If you need boilerplate code like the -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath implementation that comes when you subclass UITableViewController, then you should create your own templates as Grouchal suggests. Also check a similar question I asked here.

如果您需要样板代码,例如 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath实现,当您继承UITableViewController时,那么您应该像Grouchal建议的那样创建自己的模板。还请查看我在这里问过的类似问题。

I think that the tutorial you link to refers to code sense, for example:

我认为您链接的教程是指代码感,例如:

  • In a header file, add a protocol like <UITextFieldDelegate>:
  • 在头文件中,添加像 这样的协议:

@interface FirstViewController : UIViewController <UITextFieldDelegate>

@interface FirstViewController:UIViewController

  • Save, and then in your .m file, you can see the new methods that you can implement just by typing a dash "-" and then pressing "Escape" on your keyboard. It helps if you type the first letters, for example "-tex" and then "Escape" will display the methods of the UITextFieldDelegate.
  • 保存,然后在.m文件中,您可以通过键入短划线“ - ”然后按键盘上的“Escape”来查看可以实现的新方法。如果您键入第一个字母,例如“-tex”,然后“Escape”将显示UITextFieldDelegate的方法,它会有所帮助。

Try the same with UITableViewDelegate and UITableViewDataSource, you will see that you'll get a similar result as the one at step 4 of your tutorial (note that in the screenshot, the user has already typed "-tab" to get the list of methods).

尝试使用UITableViewDelegate和UITableViewDataSource,您将看到您将获得与教程第4步相似的结果(请注意,在屏幕截图中,用户已经键入“-tab”以获取方法列表)。

#2


1  

Check out Accessorizer if I'm not mistaken, it can do what you want.

如果我没有弄错,请查看Accessorizer,它可以做你想要的。

#3


1  

Firstly you might want to look into using templates to achieve your goals:

首先,您可能希望使用模板来实现目标:

http://blog.highorderbit.com/2009/03/15/customizing-xcode-cocoa-touch-file-templates/

http://blog.highorderbit.com/2009/03/15/customizing-xcode-cocoa-touch-file-templates/

The tutorial you have references also mentions in point 4:

您参考的教程也在第4点中提到:

Also note, if the method is not auto-generating you're probably doing something wrong. That's the main way I know I'm doing something wrong, or the program crashes.

另请注意,如果该方法不是自动生成的,那么您可能做错了什么。这是我知道我做错了什么或者程序崩溃的主要方式。

This might indicate that you have not followed the previous steps 100%

这可能表示您没有遵循前面的步骤100%

#4


1  

If you create a new class through the Xcode wizard ( i.e. File->New->File or cmd-N and select Objective-C class ) then if you pick an appropriate built-in superclass from those in the 'Subclass Of' dropdown, you will get a template implementation with the essential stubs implemented, and commented-out versions of other methods, that you can base your code on. I've used this successfully for subclasses of UITableViewController. Not quite in the league of the Eclipse stub generation, but better than nothing.

如果你通过Xcode向导创建一个新类(即File-> New-> File或cmd-N并选择Objective-C类),那么如果你从'Subclass Of'下拉列表中选择一个合适的内置超类,您将获得一个模板实现,其中包含实现的基本存根,以及其他方法的注释版本,您可以根据这些方法编写代码。我已经成功地将它用于UITableViewController的子类。不是Eclipse杂志生成的联盟,但总比没有好。

#1


2  

From your question, I can't understand exactly what you need.

从你的问题来看,我无法准确理解你的需求。

If you need boilerplate code like the -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath implementation that comes when you subclass UITableViewController, then you should create your own templates as Grouchal suggests. Also check a similar question I asked here.

如果您需要样板代码,例如 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath实现,当您继承UITableViewController时,那么您应该像Grouchal建议的那样创建自己的模板。还请查看我在这里问过的类似问题。

I think that the tutorial you link to refers to code sense, for example:

我认为您链接的教程是指代码感,例如:

  • In a header file, add a protocol like <UITextFieldDelegate>:
  • 在头文件中,添加像 这样的协议:

@interface FirstViewController : UIViewController <UITextFieldDelegate>

@interface FirstViewController:UIViewController

  • Save, and then in your .m file, you can see the new methods that you can implement just by typing a dash "-" and then pressing "Escape" on your keyboard. It helps if you type the first letters, for example "-tex" and then "Escape" will display the methods of the UITextFieldDelegate.
  • 保存,然后在.m文件中,您可以通过键入短划线“ - ”然后按键盘上的“Escape”来查看可以实现的新方法。如果您键入第一个字母,例如“-tex”,然后“Escape”将显示UITextFieldDelegate的方法,它会有所帮助。

Try the same with UITableViewDelegate and UITableViewDataSource, you will see that you'll get a similar result as the one at step 4 of your tutorial (note that in the screenshot, the user has already typed "-tab" to get the list of methods).

尝试使用UITableViewDelegate和UITableViewDataSource,您将看到您将获得与教程第4步相似的结果(请注意,在屏幕截图中,用户已经键入“-tab”以获取方法列表)。

#2


1  

Check out Accessorizer if I'm not mistaken, it can do what you want.

如果我没有弄错,请查看Accessorizer,它可以做你想要的。

#3


1  

Firstly you might want to look into using templates to achieve your goals:

首先,您可能希望使用模板来实现目标:

http://blog.highorderbit.com/2009/03/15/customizing-xcode-cocoa-touch-file-templates/

http://blog.highorderbit.com/2009/03/15/customizing-xcode-cocoa-touch-file-templates/

The tutorial you have references also mentions in point 4:

您参考的教程也在第4点中提到:

Also note, if the method is not auto-generating you're probably doing something wrong. That's the main way I know I'm doing something wrong, or the program crashes.

另请注意,如果该方法不是自动生成的,那么您可能做错了什么。这是我知道我做错了什么或者程序崩溃的主要方式。

This might indicate that you have not followed the previous steps 100%

这可能表示您没有遵循前面的步骤100%

#4


1  

If you create a new class through the Xcode wizard ( i.e. File->New->File or cmd-N and select Objective-C class ) then if you pick an appropriate built-in superclass from those in the 'Subclass Of' dropdown, you will get a template implementation with the essential stubs implemented, and commented-out versions of other methods, that you can base your code on. I've used this successfully for subclasses of UITableViewController. Not quite in the league of the Eclipse stub generation, but better than nothing.

如果你通过Xcode向导创建一个新类(即File-> New-> File或cmd-N并选择Objective-C类),那么如果你从'Subclass Of'下拉列表中选择一个合适的内置超类,您将获得一个模板实现,其中包含实现的基本存根,以及其他方法的注释版本,您可以根据这些方法编写代码。我已经成功地将它用于UITableViewController的子类。不是Eclipse杂志生成的联盟,但总比没有好。