如何使 Xcode 自动完成委托方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18763362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 03:47:09  来源:igfitidea点击:

How can I make Xcode autocomplete delegate methods?

iphoneiosobjective-cxcode

提问by Sie7e

This code uses a delegate

此代码使用委托

[[[UIAlertView alloc] initWithTitle: @ "Error" message: @ "You can leave the text blank" delegate: self cancelButtonTitle: @ "Quit" otherButtonTitles: @ "OK", nil] show];

The delegate in question is:

有问题的代表是:

 (void) alertView: (UIAlertView *) alertView clickedButtonAtIndex: (NSInteger) buttonIndex {

My question is this: How do you autocomplete for Xcode delegate methods?.

我的问题是:你如何自动完成 Xcode 委托方法?

In Eclipse you can do from the Source menu option Generate Delegate methods.

在 Eclipse 中,您可以从源菜单选项生成委托方法。

Regards and thanks!

问候和感谢!

回答by Injectios

Your class should support UIAlertViewDelegate protocol

你的类应该支持 UIAlertViewDelegate 协议

@interface YourClass : NSObject 
<
  UIAlertViewDelegate
>

start typing "dash" - you'll see all supported method included UIAlertViewDelegatemethods. To reduce the autocomplete methods list: type -alert

开始输入“破折号” - 您将看到所有支持的方法包括UIAlertViewDelegate方法。要减少自动完成方法列表:键入-alert

Simply use "Go to definition" to see all "UIAlertViewDelegate" methods. Start typing one of the following methods in your XCode

只需使用“转到定义”即可查看所有“UIAlertViewDelegate”方法。开始在您的XCode

enter image description here

在此处输入图片说明

If method under @requiredsection you have to implement it in your delegate object otherwise you'll get warning. If under @optionalmethods you don't have to implement any of these methods (you won't get warnings), but in most cases you should do this to let it work as you expect.

如果@required部分下的方法必须在您的委托对象中实现它,否则您会收到警告。如果在@optional方法下您不必实现任何这些方法(您不会收到警告),但在大多数情况下,您应该这样做以使其按预期工作。

回答by gran33

If the method is notrequired, you won't get any warning about unimplemented methods. XCode and Eclipse are different.

如果该方法不是必需的,你不会得到有关未实现的方法的任何警告。XCode 和 Eclipse 是不同的。

In second thought, u can use this trick

再想一想,你可以使用这个技巧

回答by cleexiang

Another choice, you can use appcode instead of xcode, "Generate" feature can be found.

另一种选择,您可以使用 appcode 而不是 xcode,可以找到“生成”功能。

回答by CH Wing

try press escin keyboard, Xcode : Version 11.3.1 (11C504)

尝试按下esc键盘,Xcode:版本 11.3.1 (11C504)

enter image description here

在此处输入图片说明

回答by D-eptdeveloper

In Xcode you can create your own class with the custom component as per your need and create a delegate methods in side that for responding like people uses for parsing and AsyncImageView and etc..

在 Xcode 中,您可以根据需要使用自定义组件创建自己的类,并在侧面创建一个委托方法,用于响应人们用于解析和 AsyncImageView 等的响应。

you can visit herefor Async class demo with the respoct of this you can write your own class for UIAlertView

您可以访问此处查看异步类演示,对此您可以编写自己的类UIAlertView

回答by Sie7e

Thank you very much to all.

非常感谢大家。

In this case all "@protocol UIAlertViewDelegate" methods are under @optional.I understand that in this case it is not necessary my class should support UIAlertViewDelegate protocol because all methods are under @optional.

在这种情况下,所有“@protocol UIAlertViewDelegate”方法都在@optional 下。我理解在这种情况下,我的类没有必要支持UIAlertViewDelegate 协议,因为所有方法都在@optional 下。

The solutions is:

解决办法是:

right-click on UIAlertViewDelegate or UIAlertView, click on "Jump to definition", then you can see the @protocol. Inside you can see methods with @optional and @requiered. If method under @required section you have to implement it in your delegate object otherwise you'll get warning. If under @optional methods you don't have to implement any of these methods (you won't get warnings)

右键单击 UIAlertViewDelegate 或 UIAlertView,单击“跳转到定义”,然后可以看到@protocol。在里面你可以看到带有@optional 和@requiered 的方法。如果@required 部分下的方法你必须在你的委托对象中实现它,否则你会收到警告。如果在@optional 方法下,您不必实现任何这些方法(您不会收到警告)

Thank you again

再次感谢你

回答by 7usam

This is what I do for delegates that I use frequently.

这就是我为我经常使用的委托所做的。

  • Find the required methods and optional ones that you will always implement by accessing the UIAlertViewDelegate definition.
  • Add empty implementations for these methods.
  • Add a snippet with the shortcut being the delegate name and the content being my empty implementations (select then drag and drop to snippet library)
  • Next time I use that delegate all I need to do is to start typing my shortcut (Ex: AlertViewDelegate to auto-insert my empty implementations
  • 通过访问 UIAlertViewDelegate 定义,找到您将始终实现的必需方法和可选方法。
  • 为这些方法添加空的实现。
  • 添加一个片段,快捷方式是委托名称,内容是我的空实现(选择然后拖放到片段库)
  • 下次我使用该委托时,我需要做的就是开始输入我的快捷方式(例如:AlertViewDelegate 以自动插入我的空实现

Bonus:

奖金:

I also sometimes add in some default code in the implementations that is always going to be there, example:

我有时也会在实现中添加一些默认代码,这些代码总是存在的,例如:

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSString* title = [alertView.title lowercaseString];
    if (buttonIndex != alertView.cancelButtonIndex) {
        if ([title isEqualToString:<#action title#>]) {
            <#statements#>
        }
    }else{
        <#cancel code#>
    }
}