There is a problem with making MySubclass
its own delegate. Presumably you don't want to run custom code for all of the UIScrollViewDelegate
methods, but you have to forward the messages to the user-provided delegate whether you have your own implementation or not. So you could try to implement all of the delegate methods, with most of them just forwarding like this:
- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
[self.myOwnDelegate scrollViewDidZoom:scrollView];
}
The problem here is that sometimes new versions of iOS add new delegate methods. For example, iOS 5.0 added scrollViewWillEndDragging:withVelocity:targetContentOffset:
. So your scrollview subclass won't be future-proof.
The best way to handle this is to create a separate, private object that just acts as your scrollview's delegate, and handles forwarding. This dedicated-delegate object can forward every message it receives to the user-provided delegate, because it only receives delegate messages.
Here's what you do. In your header file, you only need to declare the interface for your scrollview subclass. You don't need to expose any new methods or properties, so it just looks like this:
MyScrollView.h
@interface MyScrollView : UIScrollView
@end
All the real work is done in the .m
file. First, we define the interface for the private delegate class. Its job is to call back into MyScrollView
for some of the delegate methods, and to forward all messages to the user's delegate. So we only want to give it methods that are part of UIScrollViewDelegate
. We don't want it to have extra methods for managing a reference to the user's delegate, so we'll just keep that reference as an instance variable:
MyScrollView.m
@interface MyScrollViewPrivateDelegate : NSObject <UIScrollViewDelegate> {
@public
id<UIScrollViewDelegate> _userDelegate;
}
@end
Next we'll implement MyScrollView
. It needs to create an instance of MyScrollViewPrivateDelegate
, which it needs to own. Since a UIScrollView
doesn't own its delegate, we need an extra, strong reference to this object.
@implementation MyScrollView {
MyScrollViewPrivateDelegate *_myDelegate;
}
- (void)initDelegate {
_myDelegate = [[MyScrollViewPrivateDelegate alloc] init];
[_myDelegate retain]; // remove if using ARC
[super setDelegate:_myDelegate];
}
- (id)initWithFrame:(CGRect)frame {
if (!(self = [super initWithFrame:frame]))
return nil;
[self initDelegate];
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
if (!(self = [super initWithCoder:aDecoder]))
return nil;
[self initDelegate];
return self;
}
- (void)dealloc {
// Omit this if using ARC
[_myDelegate release];
[super dealloc];
}
We need to override setDelegate:
and delegate:
to store and return a reference to the user's delegate:
- (void)setDelegate:(id<UIScrollViewDelegate>)delegate {
_myDelegate->_userDelegate = delegate;
// Scroll view delegate caches whether the delegate responds to some of the delegate
// methods, so we need to force it to re-evaluate if the delegate responds to them
super.delegate = nil;
super.delegate = (id)_myDelegate;
}
- (id<UIScrollViewDelegate>)delegate {
return _myDelegate->_userDelegate;
}
We also need to define any extra methods that our private delegate might need to use:
- (void)myScrollViewDidEndDecelerating {
// do whatever you want here
}
@end
Now we can finally define the implementation of MyScrollViewPrivateDelegate
. We need to explicitly define each method that should contain our private custom code. The method needs to execute our custom code, and forward the message to the user's delegate, if the user's delegate responds to the message:
@implementation MyScrollViewPrivateDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
[(MyScrollView *)scrollView myScrollViewDidEndDecelerating];
if ([_userDelegate respondsToSelector:_cmd]) {
[_userDelegate scrollViewDidEndDecelerating:scrollView];
}
}
And we need to handle all of the other UIScrollViewDelegate
methods that we don't have custom code for, and all of those messages that will be added in future versions of iOS. We have to implement two methods to make that happen:
- (BOOL)respondsToSelector:(SEL)selector {
return [_userDelegate respondsToSelector:selector] || [super respondsToSelector:selector];
}
- (void)forwardInvocation:(NSInvocation *)invocation {
// This should only ever be called from `UIScrollView`, after it has verified
// that `_userDelegate` responds to the selector by sending me
// `respondsToSelector:`. So I don't need to check again here.
[invocation invokeWithTarget:_userDelegate];
}
@end
You can do your main development on Linux or Windows with Android Studio or Visual Studio Code. Then use git to move the code to macOS to test it with Xcode on an iOS simulator/device and deploy it to the App Store.
You could do all development on macOS but you can't do all development on Linux or Windows. I'm not too pleased with Apple for making overpriced machines and then forcing us to buy them. Since I can't afford a fast Apple computer, I am planning to do most of my development on Linux and then just do testing and deployment on my painfully slow Mac Mini.
Update
It seems like there are more possibilities now. Read the following articles:
Personally, I ended up buying a MacBook Pro for way too much money. I have to admit that it is convenient, but I have done very little up to this point that really required it. I'm doing all of my learning and development in Android Studio and usually use the Android emulator. Every now and then I fire up the iOS simulator, but I haven't been required to.
My advice is to keep using your current system (Windows or Linux) for as long as you are learning and even while you are developing your first Flutter apps. Eventually you may appreciate the convenience of having the iOS Simulator and Xcode on the same machine, but there is certainly no rush.
Best Answer
I ran into the same problem creating a subclass of the UIScrollView but solved it this way. As mentioned above, you can set yourself (subclass instance) as the delegate however this is what I did.
In the sub class I overrode the following methods
As far as the other
UIScrollViewDelegate
methods, the View Controller should be responsible for handling those in my opinion.