NSThread 建立執行緒和更新UI

2021-06-07 09:18:17 字數 838 閱讀 3886

nsthread建立執行緒:

第一種:[nsthread detachnewthreadselector:@selector(

mythreadmainmethod

:) totarget:self withobject:nil];

第二種:nsthread* mythread = [[nsthread alloc] initwithtarget:self

selector:@selector(mythreadmainmethod:)

object:nil];

[mythread start];

第一種是建立之後馬上跑到mythreadmainmethod去做事情去了,而第二種還需要手動呼叫 start 函式,所以在啟動執行緒之前,對執行緒進行配置,比如設定 stack 大小,執行緒優先順序等;

更新ui:

-(void)changetext:(nsstring *)str

-(void)mythreadmainmethod

}- (void)viewdidload

iphone開發中,在主線程之外的執行緒更新主線程所顯示的介面元素,不能直接呼叫主線程的類的更新介面的方法,否則介面看不到任何結果

.而是需要有個緩衝,叫做

performselectoronmainthread

[self performselectoronmainthread:@selector(updatelabel:) withobject:updatedata waituntildone:yes];

比如說你已經定義了乙個- (void)updatelabel:(nsstring *)txt{}的方法.在此方法中更新介面即可.即時看到結果.

NSThread建立多執行緒

乙個nsthread的物件就代表乙個執行緒。建立 啟動執行緒 nsthread thread nsthread alloc initwithtarget self selector selector download object nil thread start 主線程的相關用法 nsthread...

多執行緒 NSThread

我們想要載入網路上的一張 define url imag 然後初始化按鈕和乙個沒有的imageview 也可以再加乙個lable顯示資訊 主線程直接載入 self.imageview.image uiimage imagewithdata self loaddatawithurl url imag ...

iOS 多執行緒 NSThread

ibaction startthreadbuttonpressed uibutton sender 建立新的執行緒,執行緒的函式為 startthebackgroundjob.具體的 startthebackgroundjob 函式定義如下.void startthebackgroundjob 在第...