子类化 UIScrollview 不会触发委托方法

Subclassing UIScrollview does not trigger delegate methods

提问人:stefanosn 提问时间:2/3/2022 最后编辑:stefanosn 更新时间:2/5/2022 访问量:146

问:

大家好,我正在尝试在我的 ViewController 中为 UIScrollview 的单独类中实现 scrollViewDidScroll。不触发方法...任何帮助都表示赞赏。

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

在我的视图控制器中,我是这样设置的

C8MyProfileScrollView *profileTopScrollView = [[C8MyProfileScrollView alloc]initWithFrame:self.scrollView.frame];
self.scrollView.delegate=profileTopScrollView.myOwnDelegate;
iOS Objective-C UIDscrollView UIDindowView 委托

评论

0赞 Larme 2/3/2022
你想要一个我猜是有相同的委托吗?你有 2 个 scrollView?因为目前,如果您不保留对 的引用,则在调用委托之前,它可能会过早地解除分配。那么为什么不是实例呢?scrollViewUIScrollViewprofileTopScrollViewprofileTopScrollViewscrollViewC8MyProfileScrollView
0赞 Adeel Miraj 2/3/2022
你能解释一下你想实现什么,目的是什么吗?myOwnDelegate
0赞 stefanosn 2/3/2022
@Larme 我的错误将其更改为 NSObject...
0赞 stefanosn 2/3/2022
@AdeelMiraj我的代码可能令人困惑。我正在尝试将存在于我的 Viewcontroller 中的 UIScrollview 的委托设置为一个单独的类,并在这个单独的类中实现委托方法 viewdidscroll...

答:

1赞 DonMag 2/5/2022 #1

您误解了委托/协议模式。

快速示例...

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

示例视图控制器...


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

滚动滚动视图将输出大量行,如下所示:

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:]

评论

0赞 stefanosn 2/12/2022
我只是找时间测试它!效果很好!非常感谢,我很感激DonMag先生!