ios - 将委托(delegate)对象类型作为协议(protocol)方法参数传递

标签 ios objective-c delegates

我想将我的委托(delegate)对象作为具有声明类型的参数传递,所以我不必强制转换(如果我通过 (id)sender 代替):

@protocol myObjectDelegate <NSObject>
- (void)myObjectAsArgument:(myObject *)object;
@end

@interface myObject : NSObject
    // stuff...
    @property (nonatomic, strong) id <myObjectDelegate> delegate;
@end

这样做的正确方法是什么?就像我说的,我知道我可以这样做:
- (void)myObjectAsArgument:(id)object;

那会让我通过self in 作为参数,但我不喜欢使用强制转换语法。

谢谢。

笔记:

苹果也这样做:
@protocol UITableViewDataSource <NSObject>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
@end

@interface UITableView : UIScrollView
@property (nonatomic, strong) id <UITableViewDataSource> dataSource;
@end

这只是 UITableViewDataSource 协议(protocol)中的一种方法,它将 UITableView 类型作为参数传递。 ;)

最佳答案

由于这是顶级 Google 搜索结果,以下是操作方法:

@class myObject;  // just need this for compiling

@protocol myObjectDelegate <NSObject>
- (void)myObjectAsArgument:(myObject *)object;
@end

@interface myObject : NSObject
    // stuff...
@property (nonatomic, strong) id <myObjectDelegate> delegate;
@end

关于ios - 将委托(delegate)对象类型作为协议(protocol)方法参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25551714/

相关文章:

ios - Facebook图像未在Cordova应用程序的ios版本中显示

IOS 8 界面生成器 UITableViewCell 对齐

ios - 是否可以从 Websheet.app 内部打开到 Safari 的链接

ios - 将 NSString 变量发送到 WCF 服务

c# - 使用 Func<>(或 Action<>)或创建自己的委托(delegate)?

ios - 调用委托(delegate)方法

c# - Action 委托(delegate)、泛型、协变和逆变

ios - 音量在 iOS 7 中已弃用

css - 检测 css 位置溢出是否有效?

ios - iOS14.2 SwiftUI PageTabView 会多次调用 ChildView onAppear 方法