延遲初始化 指標地址 NSTimer

來源:互聯網
上載者:User

很多東西 不總結 覺的有點不舒服 這裡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];}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.