How to make iOS callback functions?
callback, ios, objective-c
Solution
1.You can do it with blocks!
You can pass some block to BottomView.
2. Or you can do it with target-action.
you can pass to BottomView selector `@selector(myMethod:)` as action, and pointer to the view controller as target. And after animation ends use `performeSelector:` method.
3. Or you can define delegate @protocol and implement methods in your viewController, and add delegate property in BottomView. @property (assign) id delegate;
If you make some animations in your BottomView, you can use UIView method
animateWithDuration:delay:options:animations:completion:
that uses blocks as callbacks
[UIView animateWithDuration:1.0f animations:^{
}];
update:
in ButtomView.h
@class BottomView;
@protocol BottomViewDelegate <NSObject>
- (void)bottomViewAnimationDone:(BottomView *) bottomView;
@end
@interface BottomView : UIView
@property (nonatomic, assign) id <BottomViewDelegate> delegate;
.....
@end
in ButtomView.m
- (void)notifyDelegateAboutAnimationDone {
if ([self.delegate respondsToSelector:@selector(bottomViewAnimationDone:)]) {
[self.delegate bottomViewAnimationDone:self];
}
}
and after animation complit you should call `[self notifyDelegateAboutAnimationDone];`
you should set you ViewController class to confirm to protocol BottomViewDelegate
in MyViewController.h
@interface MyViewController : UIViewController <BottomViewDelegate>
...
@end
and f.e. in `viewDidLoad` you should set `bottomView.delegate = self;`
Problem
I can't figure out how to make a function, that will give information to the parent object, that has done something, so I need your help. Example what I want to make: - `ViewController` class instantiate class `BottomView` and adds it as it's subview. - Make a call on instance of `BottomView` class to start animate something. - After the animation ends, I want to give a sign that the animation has ended to the `ViewController` class, that it could release/remove an instance `BottomView` from itself. I need something like a callback. Could you help please?