2013-10-29 27 views
0

,当我在我的按钮点击执行,以便声明,我的函数被调用如何在目标C

[myBtn addTarget:self action:@selector(myFunction) forControlEvents:UIControlEventTouchUpInside]; 

在我的功能,复杂的语句的集合将被执行,并采取豆蔻位的时间来运行,所以我要显示加载(UIActivityIndi​​catorView),如下所示:

-(void) addTradeAction { 
    //Show Loading 
    [SharedAppDelegate showLoading]; 

    //disable user interaction 
    self.view.userInteractionEnabled = NO; 

    //execute call webservice in here - may be take 10s 
    //Hide Loading 
    [ShareAppDelegate hideLoading]; 
} 

当myBtn(我的按钮)水龙头 - > 3S或4S之后,[ShareAppDelegate showLoading]被调用。

当我在其他函数上使用[ShareAppDelegate showLoading]时,这是很少见的 - >它工作得很好,我的意思是所有的语句都按顺序执行。

所有我想要的,当我点击我的按钮时,加载将被立即调用。提前

韩国社交协会

+2

显示'myFunction'的代码。一切是在主线上运行吗? – Wain

+1

使用完成处理程序块。 – ManicMonkOnMac

回答

2

执行在后台任务,并在你的情况下以正确的方式显示了活动的指标,就是:

-(void)myBackGroundTask 
{ 
    //here showing the 'loading' and blocking interaction if you want so 

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
     //here everything you want to perform in background 

     dispatch_async(dispatch_get_main_queue(), ^{ 
      //call back to main queue to update user interface 
     }); 
    }); 
} 

有了这种块的,你确信你的接口不冻结,并保持一个流畅的动画。

+0

来源:http://www.raywenderlich.com/31166/#mainthread – zbMax

0

戴上myFunction的对后台队列中运行,因为它可能使系统挂起:

- (void)myFunction { 
    dispatch_queue_t myQueue = dispatch_queue_create("myQueue", NULL); 

    // execute a task on that queue asynchronously 
    dispatch_async(myQueue, ^{ 
     // Put the current myFunction code here. 
    }); 

} 
1

如果您复杂的语句没有任何UI动画或UI相关的代码,那么你可以在不同的线程中执行该部分(除了mainThread)。一旦语句完成(或在完成模块中),您可以在那里删除装载Overlay。