0

好的,所以我有一个UIView名为 的自定义类NumberTicker,它创建了几个UIScrollViews并填充它们UILabels以便进行滚动NumberTicker。但是,我无法正确访问 的属性scrollviews,例如。

[self.scrollview setHidden:YES];
// or
[self.scrollview setContentOffset:offset];

上述内容,实际上任何设置或获取 属性的尝试UIScrollView都不起作用,除非在scrollview创建 时,然后将其添加为subview.

这是我的NumberTicker.h文件:

//  NumberTicker.h
#import <Foundation/Foundation.h>
#import "ProfileViewController.h"

@interface NumberTicker : UIScrollView <UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UIScrollView *digitOne;
@property (weak, nonatomic) IBOutlet UIScrollView *digitTwo;
@property (weak, nonatomic) IBOutlet UIScrollView *digitThree;
@property (weak, nonatomic) IBOutlet UIScrollView *digitFour;
@property (weak, nonatomic) IBOutlet UIScrollView *digitFive;
@property (weak, nonatomic) IBOutlet UIScrollView *digitSix;
@property (weak, nonatomic) IBOutlet UIScrollView *digitSeven;
@property (weak, nonatomic) IBOutlet UIScrollView *digitEight;


- (id) initWithDigits:(int)digits andFrame:(CGRect)frame;
- (void) setNumberTickerTo:(int)number;

@end

和 NumberTicker.m:

//  NumberTicker.m

#import "NumberTicker.h"

@implementation NumberTicker

- (id) initWithDigits:(int)digits andFrame:(CGRect)frame
{
    self = [super init];
    if (self) {
        // add digits
        [self setDigitEight:_digitEight];
        [self setDigitOne:_digitOne];
        if (digits > 6) {
            // make 8 digit ticker
            [self setFrame:frame];
            for (int i = 1; i <= digits; i++) {
                switch (i) {
                    case 1:
                        [self addSmallScrollView:_digitOne withXPos:(frame.size.width - (i*30))];
                        break;
                    case 2:
                        [self addSmallScrollView:_digitTwo withXPos:(frame.size.width - (i*30))];
                        break;
                    case 3:
                        [self addSmallScrollView:_digitThree withXPos:(frame.size.width - (i*30))];
                        break;
                    case 4:
                        [self addSmallScrollView:_digitFour withXPos:(frame.size.width - (i*30))];
                        break;
                    case 5:
                        [self addSmallScrollView:_digitFive withXPos:(frame.size.width - (i*30))];
                        break;
                    case 6:
                        [self addSmallScrollView:_digitSix withXPos:(frame.size.width - (i*30))];
                        break;
                    case 7:
                        [self addSmallScrollView:_digitSeven withXPos:(frame.size.width - (i*30))];
                        break;
                    case 8:
                        [self addSmallScrollView:_digitEight withXPos:(frame.size.width - (i*30))];
                        break;
                    default:
                        break;
                }
            }
        } else {
            // make 6 digit ticker
            [self setFrame:frame];

            for (int i = 1; i <= digits; i++) {
                switch (i) {
                    case 1:
                        [self addScrollView:_digitOne withXPos:(frame.size.width - (i*40))];
                        break;
                    case 2:
                        [self addScrollView:_digitTwo withXPos:(frame.size.width - (i*40))];
                        break;
                    case 3:
                        [self addScrollView:_digitThree withXPos:(frame.size.width - (i*40))];
                        break;
                    case 4:
                        [self addScrollView:_digitFour withXPos:(frame.size.width - (i*40))];
                        break;
                    case 5:
                        [self addScrollView:_digitFive withXPos:(frame.size.width - (i*40))];
                        break;
                    case 6:
                        [self addScrollView:_digitSix withXPos:(frame.size.width - (i*40))];
                        break;
                    default:
                        break;
                }
            }
        }
    }
    return self;
}

- (void) addScrollView:(UIScrollView *)scrollView withXPos:(int)xPos
{
    scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(xPos, 0, 40, 60)];
    scrollView.contentSize = CGSizeMake(40, 660);
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.userInteractionEnabled = NO;

// All of these properties are set properly, however I can't access them again in another method or class, after the `scrollview` has been added as a `subview`

    for (int i = 0; i <= 10; i++) {
        // create and add labels to scrollview
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, i*60, 40, 60)];
        [label setFont:[UIFont fontWithName:@"Helvetica-Bold" size:70]];
        [label setTextAlignment:NSTextAlignmentCenter];
        if (i == 10) {
            [label setText:@"0"];
        } else {
            [label setText:[NSString stringWithFormat:@"%i", i]];
        }
        [scrollView addSubview:label];
    }
    [self addSubview:scrollView];
}

- (void) addSmallScrollView:(UIScrollView *)scrollView withXPos:(int)xPos
{
    scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(xPos, 0, 30, 50)];
    scrollView.contentSize = CGSizeMake(30, 550);
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.userInteractionEnabled = NO;
    scrollView.delegate = self;

    for (int i = 0; i <= 10; i++) {
        // create and add labels to scrollview
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, i*50, 30, 50)];
        [label setFont:[UIFont fontWithName:@"Helvetica-Bold" size:50]];
        [label setTextAlignment:NSTextAlignmentCenter];
        if (i == 10) {
            [label setText:@"0"];
        } else {
            [label setText:[NSString stringWithFormat:@"%i", i]];
        }
        [scrollView addSubview:label];
    }
    [self addSubview:scrollView];
}

- (void) setNumberTickerTo:(int)number
{
    NSMutableArray *digitsArray = [[NSMutableArray alloc] init];
    while (number > 0) {
        [digitsArray addObject:[NSNumber numberWithInt:(number % 10)]];
        number = number / 10;
    }
    for (int i = 1; i <= [digitsArray count]; i++) {
        switch (i) {
            case 1:
                [self.digitOne setContentOffset:CGPointMake(0, [[digitsArray objectAtIndex:0] integerValue] * 60) animated:YES]; // this doesn't work
                NSLog(@"%@", self.digitOne);
                [digitsArray removeObjectAtIndex:0];
                break;
            case 2:
                [_digitTwo setContentOffset:CGPointMake(0, [[digitsArray objectAtIndex:0] integerValue] * 60) animated:YES];
                [digitsArray removeObjectAtIndex:0];
                break;
            case 3:
                [_digitThree setContentOffset:CGPointMake(0, [[digitsArray objectAtIndex:0] integerValue] * 60) animated:YES];
                [digitsArray removeObjectAtIndex:0];
                break;
            case 4:
                [_digitFour setContentOffset:CGPointMake(0, [[digitsArray objectAtIndex:0] integerValue] * 60) animated:YES];
                [digitsArray removeObjectAtIndex:0];
                break;
            case 5:
                [_digitFive setContentOffset:CGPointMake(0, [[digitsArray objectAtIndex:0] integerValue] * 60) animated:YES];
                [digitsArray removeObjectAtIndex:0];
                break;
            case 6:
                [_digitSix setContentOffset:CGPointMake(0, [[digitsArray objectAtIndex:0] integerValue] * 60) animated:YES];
                [digitsArray removeObjectAtIndex:0];
                break;
            case 7:
                [_digitSeven setContentOffset:CGPointMake(0, [[digitsArray objectAtIndex:0] integerValue] * 60) animated:YES];
                [digitsArray removeObjectAtIndex:0];
                break;
            case 8:
                [_digitEight setContentOffset:CGPointMake(0, [[digitsArray objectAtIndex:0] integerValue] * 60) animated:YES];
                [digitsArray removeObjectAtIndex:0];
                break;
            default:
                break;
        }
    }
}

@end

NumberTicker然后我从我的方法调用ViewController.m viewDidLoad

- (void)viewDidLoad
{
    [super viewDidLoad];
    NumberTicker *numberTicker = [[NumberTicker alloc] initWithDigits:3 andFrame:CGRectMake(20, 20, 240, 60)];
    [self.view addSubview:numberTicker];
    [numberTicker setNumberTickerTo:345];
}

视图和NumberTicker一样显示在屏幕上,scrollviews一切看起来都很好。但是,我不能隐藏scrollviews, 用于setContentOffset滚动它们、动画它们或任何东西。

我尝试过综合属性、设置代表、使用 IB 制作视图然后适当地连接插座(我已经删除了这个,因为我对这个特定类的 IB 不感兴趣,而且它在任何情况下都不起作用.

如果我尝试

NSLog(@"%@", self.digitOne);

控制台将 self.digitOne 显示为 (null)。

我究竟做错了什么?我如何正确子类化UIView并以编程UIScrollViews方式在其中创建,以及稍后在其他方法中可编辑的属性?

4

2 回答 2

0

你有没有合成你的scrollview,我认为你只能使用'self'。如果属性是合成的,则表示。

于 2013-04-23T01:51:54.063 回答
0

您的addSmallScrollView方法只是将其参数变量重新分配给一个新的滚动视图,但它对作为其第一个参数传递的对象没有任何作用。例如,您实际上并没有分配任何东西_digitOne

我建议您更改addSmallScrollView为类似的东西createNewSmallScrollView并让它或多或少地像现在一样工作,但返回在方法内部实例化的滚动视图。然后你可以像这样使用它:

_digitOne = [self createNewSmallScrollViewWithXPos:(frame.size.width - (i*30))]
于 2013-04-23T01:02:14.483 回答