如何创建宏以指向通过迭代找到的对象

How to create macro to point to an object found through iteration

我有一个方法可以找到视图的父级VC:

-(UIViewController *)mainViewController {
    UIViewController *viewController = nil;
    for (UIView *next = [self superview]; next; next = next.superview) {
        UIResponder *nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            viewController = (UIViewController *)nextResponder;
            break;
        }
    }
    return viewController;
}

我希望将来能够只使用 parentVC,这是我打算制作的宏。

唯一的问题是,我不知道如何将其转换为宏,所以我只能说 parentVC.view.alpha = 0.5f;

我假设宏将以某种方式利用块,以便它可以在查找父对象时处理此迭代 VC,同时仍然能够将其视为对象?但我不知道它是如何工作的。

能够以以下形式使用块:

((^NSObject*(void){/*logic+return here*/})())

最终宏:

#define parentVC (\
(^UIViewController*(void){\
UIViewController *viewController = nil;\
for (UIView *next = [self isKindOfClass:[UIViewController class]]?[((UIViewController *)self).view superview]:[((UIView *)self) superview]; next; next = next.superview) {\
UIResponder *nextResponder = [next nextResponder];\
if ([nextResponder isKindOfClass:[UIViewController class]]) {\
viewController = (UIViewController *)nextResponder;\
break;\
}\
}\
return viewController;\
})()\
)