很多東西 不總結 覺的有點不舒服 這裡mark下
第一是延遲初始化
首先
@property (retain, nonatomic) NSMutableArray *buttonsArray;
@synthesize buttonsArray;
//mark code-(NSMutableArray*)buttonsArray{ if (!buttonsArray) { buttonsArray = [[NSMutableArray alloc] init]; } return buttonsArray;}
UINewsDetailPlayView * newsDetailPlayView =[[UINewsDetailPlayView alloc] initFromNib];[self.buttonsArray addObject:newsDetailPlayView];
這裡把對象添加到數組中 也就是儲存指標的意思吧
for (UINewsDetailPlayView *detailPlayView in self.buttonsArray) { [detailPlayView.playButton setSelected:NO];//這樣遍曆 取出playButton 設定選中狀態 }
為什麼要這樣寫呢?因為遇到一個問題 手動的給一個button添加了事件 代碼如下
[newsDetailPlayView.playButton addTarget:self action:@selector(playAudio:) forControlEvents:UIControlEventTouchUpInside];
但是在playAudio中又聲明了一個NSTimer導致了 無法對PlayButton進行操作 代碼如下
-(void)playAudio:(UIButton *)button{ int audioFlag = button.tag - PLAY_BUTTON_ROOT_TAG; NSArray *list = [self.newsData.programaMedia componentsSeparatedByString:@","]; NSString *mediaUrl = [list objectAtIndex:audioFlag]; if (button.selected) { [[MMSPlayer sharedMMSPlayer] stop]; [button setSelected:NO]; isPlaying = false; }else{ [self startLoadingAnimation]; [[MMSPlayer sharedMMSPlayer] play:mediaUrl]; [button setSelected:YES]; isPlaying = true; //mark code [[NSTimer scheduledTimerWithTimeInterval: 10 target: self selector: @selector(handleTimer:) userInfo: nil repeats: NO]retain]; }}-(void)handleTimer:(NSTimer *)timer{ //mark code for (UINewsDetailPlayView *detailPlayView in self.buttonsArray) { [detailPlayView.playButton setSelected:NO]; } [AlertUtils alertWithMessage:@"緩衝逾時 請檢查網路資源是否可用"]; [[MMSPlayer sharedMMSPlayer] stop]; [self stopLoadingAnimation]; [timer release];}