我使用protocol和init方法获得了AudioView类
@protocol AudioViewDelegate <NSObject>
- (void)playButtonAction:(id)sender; //(1)
- (void)playButtonActionforAudioTrack:(AudioTrack *)audioTrack; //(2)
////
- (id)initWithFrame:(CGRect)frame forAudioTrack:(AudioTrack *)audioTrack withButtonTag:(int)buttonTag
我使用音轨启动课程。我有播放按钮,然后我在屏幕上点击它,在方法(1)中委托我得到了按钮的发件人标签。
如何在代表中为播放曲目返回audioTrack?
========================= 类
@protocol AudioViewDelegate <NSObject>
- (void)playButtonAction:(id)sender;
- (void)playButtonActionforAudioTrack:(AudioTrack *)audioTrack;
@end
@interface AudioView : UIView
@property (nonatomic, strong) UILabel *audioTitle;
@property (nonatomic, strong) UILabel *durationTitle;
@property (nonatomic, strong) UIButton *playButton;
@property (nonatomic, weak) id <AudioViewDelegate> delegate;
- (id)initWithFrame:(CGRect)frame forAudioTrack:(AudioTrack *)audioTrack withButtonTag:(int)tag;
//。米
- (id)initWithFrame:(CGRect)frame forAudioTrack:(AudioTrack *)audioTrack withButtonTag: (int)buttonTag
{
self = [super initWithFrame:frame];
if (self) {
self.frame = frame;
self.tag = 0011;
UIImageView *audioImageView = ..
//[audioView addSubview:audioImageView];
self.playButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.playButton.frame = audioImageView.frame;
self.playButton.tag = buttonTag;
[self.playButton setBackgroundImage:audioImageView.image forState:UIControlStateNormal];
[self addSubview:self.playButton];
[self.playButton addTarget:self.delegate action:@selector(playButtonAction:) forControlEvents:UIControlEventTouchUpInside];
self.audioTitle = ..
self.audioTitle.text = [NSString stringWithFormat:@"%@ - %@", audioTrack.artist, audioTrack.title];
[self addSubview:self.audioTitle];
self.durationTitle = ..
[self addSubview:self.durationTitle];
}
return self;
}
////委托
创建视图
for (NSDictionary *dic in audioArray)
{
AudioTrack *audioTrack = [[AudioTrack alloc]init];
audioTrack.artist = dic[@"artist"];
audioTrack.title = dic[@"title"];
audioTrack.duration = dic[@"duration"];
audioTrack.url = dic[@"url"];
[self.audioArray addObject:audioTrack];
AudioView *audioView = [[AudioView alloc]initWithFrame:CGRectMake(10, currentHeight + audioInset, 300, 20) forAudioTrack:audioTrack withButtonTag:[audioArray indexOfObject:dic]];
audioView.delegate = self;
[view addSubview:audioView];
///实施委托
- (void)playButtonAction:(id)sender
{
int audioTag = [sender tag]; //here I get tag of tapped button
AudioTrack *sectedTrack = [self.audioArray objectAtIndex:audioTag];
我找到了很长的路。是否有可能在代理中获得音频跟踪?
答案 0 :(得分:0)
这不是一个真正的答案,但评论很长。
好吧,如果A是一个想要将消息发送到它用作委托的其他类的对象的类,那么就像你所做的那样明智地为其他类中的所有方法声明一个协议,A想要呼叫。 让我们称之为ProtocolA和方法method1和method2。 method2接收int作为参数。 然后你有任何其他类B或C或类似的,符合协议ProtocolA。然后通常在A中使用名称为delegate的类型为id的变量。在某个时间点,当您要发送消息时,则调用[self.delegate method1];
这基本上都是关于委托模式的。你在哪里打电话给playButtonAction :?