Hello everyone i am trying to implement scrollViewDidScroll in a seperate class for a UIScrollview in my ViewController. Does not trigger the method...Any help appreciated.
C8MyProfileScrollView.h
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface C8MyProfileScrollView : NSObject <UIScrollViewDelegate>
@property (nonatomic, assign) id<UIScrollViewDelegate> myOwnDelegate;
-(void)scrollViewDidScroll:(UIScrollView *)scrollView;
@end
NS_ASSUME_NONNULL_END
C8MyProfileScrollView.m
#import "C8MyProfileScrollView.h"
@implementation C8MyProfileScrollView
-(id)init{
self = [super init];
self.myOwnDelegate = self;
return self;
}
- (void) scrollViewDidScroll:(UIScrollView *)scrollView {
NSLog(@"scrolling in seperate class");
}
@end
In my viewcontroller i set it like this
C8MyProfileScrollView *profileTopScrollView = [[C8MyProfileScrollView alloc]initWithFrame:self.scrollView.frame];
self.scrollView.delegate=profileTopScrollView.myOwnDelegate;
CodePudding user response:
You're misunderstanding the delegate/protocol pattern.
Quick example...
MyScrollDelegate.h
#import <UIKit/UIKit.h>
@interface MyScrollDelegate : NSObject <UIScrollViewDelegate>
@end
MyScrollDelegate.m
#import "MyScrollDelegate.h"
@implementation MyScrollDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
NSLog(@"%@ - in %s", [NSValue valueWithCGPoint:scrollView.contentOffset], __PRETTY_FUNCTION__);
}
@end
Example view controller...
ExtScrollDelegateViewController.h
#import <UIKit/UIKit.h>
@interface ExtScrollDelegateViewController : UIViewController
@end
ExtScrollDelegateViewController.m
#import "ExtScrollDelegateViewController.h"
#import "MyScrollDelegate.h"
@interface ExtScrollDelegateViewController ()
@property (strong, nonatomic) UIScrollView *scrollView;
@property (strong, nonatomic) MyScrollDelegate *myDelegateObj;
@end
@implementation ExtScrollDelegateViewController
- (void)viewDidLoad {
[super viewDidLoad];
_scrollView = [UIScrollView new];
_scrollView.translatesAutoresizingMaskIntoConstraints = NO;
UILabel *v1 = [UILabel new];
v1.backgroundColor = [UIColor greenColor];
v1.text = @"Label v1";
v1.translatesAutoresizingMaskIntoConstraints = NO;
UILabel *v2 = [UILabel new];
v2.backgroundColor = [UIColor greenColor];
v2.text = @"Label v2";
v2.translatesAutoresizingMaskIntoConstraints = NO;
_scrollView.backgroundColor = [UIColor yellowColor];
[_scrollView addSubview:v1];
[_scrollView addSubview:v2];
[self.view addSubview:_scrollView];
UILayoutGuide *g = [self.view safeAreaLayoutGuide];
UILayoutGuide *cg = [_scrollView contentLayoutGuide];
[NSLayoutConstraint activateConstraints:@[
[_scrollView.topAnchor constraintEqualToAnchor:g.topAnchor constant:20.0],
[_scrollView.leadingAnchor constraintEqualToAnchor:g.leadingAnchor constant:20.0],
[_scrollView.trailingAnchor constraintEqualToAnchor:g.trailingAnchor constant:-20.0],
[_scrollView.heightAnchor constraintEqualToConstant:200.0],
[v1.topAnchor constraintEqualToAnchor:cg.topAnchor constant:8.0],
[v1.leadingAnchor constraintEqualToAnchor:cg.leadingAnchor constant:8.0],
[v2.topAnchor constraintEqualToAnchor:v1.bottomAnchor constant:48.0],
[v2.leadingAnchor constraintEqualToAnchor:v1.trailingAnchor constant:480.0],
[v2.trailingAnchor constraintEqualToAnchor:cg.trailingAnchor constant:-8.0],
[v2.bottomAnchor constraintEqualToAnchor:cg.bottomAnchor constant:-8.0],
]];
_myDelegateObj = [MyScrollDelegate new];
_scrollView.delegate = _myDelegateObj;
}
@end
Scrolling the scroll view will output lots of lines like this:
ProjectName[76541:16417260] NSPoint: {109.5, 0} - in -[MyScrollDelegate scrollViewDidScroll:]
ProjectName[76541:16417260] NSPoint: {120.5, 0} - in -[MyScrollDelegate scrollViewDidScroll:]
ProjectName[76541:16417260] NSPoint: {131, 0} - in -[MyScrollDelegate scrollViewDidScroll:]
ProjectName[76541:16417260] NSPoint: {141.5, 0} - in -[MyScrollDelegate scrollViewDidScroll:]
ProjectName[76541:16417260] NSPoint: {151.5, 0} - in -[MyScrollDelegate scrollViewDidScroll:]
ProjectName[76541:16417260] NSPoint: {161, 0} - in -[MyScrollDelegate scrollViewDidScroll:]