The simple, but possibly problematic answer, is just to access it as you do the other properties:
viewController.rowLabels = @[@"Hello", @"World"];
viewController.testBlock = ^(NSInteger itemIndex) {
... viewController.foo ...
};
From your fragment we cannot know what viewController
is - e.g. it could be a local variable from the method this fragment is in or a global variable etc. If you are just reading the value in viewController
, as you are here, this does not matter[1].
The above works but there might be a problem: you probably have a strong reference cycle. The viewController
instance references the block through it's testBlock
property, and the block references the viewController
instance. If both these references are strong (likely) then you have a circular dependency and the viewController
instance and the block can never be freed by the system. You can break this cycle using a weak reference:
viewController.rowLabels = @[@"Hello", @"World"];
__weak ViewController *weakViewController = viewController; // make a weak reference to the instance
viewController.testBlock = ^(NSInteger itemIndex)
{
// temporarily make a strong reference - will last just as long as the block
// is executing once the block finishes executing the strong reference is
// removed and no strong reference cycle is left.
ViewController *myController = weakViewController;
// only execute if the `ViewController still exists
if (myController != nil)
{
... myController.foo ...
}
};
HTH
[1] note that the value you are reading is a reference to a ViewController
instance and you can modify properties of that instance, what you cannot do (and are not trying to do) is modify which instance the viewController
references if viewController
is a local variable.