سؤال

I love using animateWithDuration:options:completion. I have come across a few situations where I could really use this for my own scenario without an animation, but I am having difficulty writing the method body out. Obviously I can't just look at the UIView's code, only it's signatures.

Here was my attempt (I may have the method signatures all messed up too):

.h
+(void)performGenericBlock:(void(^)(BOOL))code actionWhenDone:(void(^)(void))action;

.m
+(void)performGenericBlock:(void(^)(BOOL))code actionWhenDone:(void(^)(void))action
{
   //Kind of lost here - here is psuedocode
   [do stuff:^(BOOL done){action}];
}

Basically I want to accomplish:

Perform the first block of code, when it is done, do the second block of code.

هل كانت مفيدة؟

المحلول

Not sure why you need two arguments, you should be able to just write the code in the original block.

In the following examples, assume we want to first call [self.test foo1] then [self.test foo2].

The simplest solution is to not create a new method at all. Simply call your code:

[self.test foo1];
[self.test foo2];

foo2 will be performed after foo1.

The second simplest solution is to create the function with only one argument:

[ViewController performGenericBlock:^{
    [self.test foo1];
    [self.test foo2];
}];

If you really do need 2 arguments, you would do the following:

[ViewController performGenericBlock:^{
    [self.test foo1];
} actionWhenDone:^{
    [self.test foo2];
}];

... and define the method like so:

+ (void)performGenericBlock:(void(^)(void))code actionWhenDone:(void(^)(void))action {
    code();
    action();
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top