如何在Objective-C中使用后台线程?

Mar*_*tin 14 multithreading objective-c ios

我在发展UIButton.

UIButton当我推动时,我正试图运行UIButton.

但我不能按下按钮,因为UIButton阻止了UIButton.

是否存在UIButton,我可以运行UIButtonUIButton并推UIButton

提前致谢.

Com*_*mpy 31

就个人而言,我会在UI的顶部运行一个HUD活动指示器,然后在后台运行你的循环.

//Start the HUD here

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    //Run your loop here

    dispatch_async(dispatch_get_main_queue(), ^(void) {
         //stop your HUD here
         //This is run on the main thread


    });
});
Run Code Online (Sandbox Code Playgroud)


vir*_*rus 5

方式一:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Write your code here 
});
Run Code Online (Sandbox Code Playgroud)

方式2:

如果您使用performSelectorInBackground:withObject:生成一个新线程。

方式3:

[NSThread detachNewThreadSelector:@selector(yourMethod:) toTarget:self withObject:nil];
Run Code Online (Sandbox Code Playgroud)