如何命名代码块并以不同的方法调用它? [英] How to name a block of code and call it in a different method?
问题描述
我使用Grand Central Dispatch方法在队列中执行我的应用程序。我在该队列的计算中决定按钮的帧。我希望我的应用程序重新绘制其scren并在旋转后计算新帧。以下是我做的一些伪代码解释:
I use Grand Central Dispatch methods to do some executions of my app in a queue. I decide the frames for buttons in a calculation on that queue. I want my app to re-draw its scren and calculate new frames after rotation. Here is some pseudo code explanation from what i do:
CGFloat a=123, b=24;
dispatch_async(drawingQue, ^{
//needed loops to get the total button count-how many ones will be drawn et..
for(int x=0;x<someCount<x++){
for(int y=0;y<anotherCount;y++){
//needed frame&name ect assingments
button.frame= CGRectMake(x+y, x-y, a, b);
[button setTitle:@"abc"];}}
};
这里我想要的是,如何给这个块命名并在
Here what i want is, how can i give this block a name and re-use it in the
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
}
委托方法?例如,如果轮换是横向的,我想使用 a = 234
而不是123 ..请帮助。请提前感谢..
delegate method? For instance, if the rotation is landscape, i want to use a=234
instead of 123.. Any help please. Thanks in advance..
推荐答案
声明块类型的实例变量并使用 Block_copy
保持阻止:
Declare an instance variable of block type and use Block_copy
to keep the block:
@interface My {
void (^myBlock)(void);
}
@end
myBlock = Block_copy(^{
...block code...
});
// later call it
myBlock();
// don't forget to release it in dealloc
它是在将块存储在其文字范围之外<( ^ {...}
)之前复制这一点非常重要,因为存储了原始块在堆栈上,当范围退出时将会死亡。
It is important to copy the block before storing it outside of the scope of its literal (^{...}
), because the original block is stored on stack and will die when the scope exits.
这篇关于如何命名代码块并以不同的方法调用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!