【问题标题】:Classes Superclasses Subclasses类 超类 子类
【发布时间】:2011-04-02 10:05:47
【问题描述】:

我正在尝试将两个子类设为一个类:

// Old code

- (void)setPaging {
    [pagingScrollView addSubview:self.ImageScrollView];
}

@interface ImageScrollView : UIScrollView <UIScrollViewDelegate> {
    UIView        *imageView;
    NSUInteger     index;
}
@property (assign) NSUInteger index;
- (void)displayTiledImageNamed:(CGPDFPageRef)page size:(CGSize)imageSize;
@end

@implementation ImageScrollView
@synthesize index;
// ... my methods ...
@end

改为:

// NEW Code__________________________________________________________*

- (void)setPaging {
    if (D == 1) {
        // error: request for member 'ISVportrate' in something not a
        // structure or union
        [pagingScrollView addSubview:self.ISVportrate];
    } else if (D == 2) {
        //error: request for member 'ISVLandscape' in something not a
        // structure or union
        [pagingScrollView addSubview:self.ISVLandscape];
    }
}

@class ISVportrate;
@class ISVLandscape;
@interface ImageScrollView : UIScrollView <UIScrollViewDelegate> {
    UIView        *imageView;
    NSUInteger     index;
}
@property (assign) NSUInteger index;
- (void)displayTiledImageNamed:(CGPDFPageRef)page size:(CGSize)imageSize;
@end
@interface ISVportrate : ImageScrollView {}
@end
@interface ISVLandscape : ImageScrollView {}
@end

@implementation ISVportrate : ImageScrollView

// error: property 'index' attempting to use ivar 'index' declared in
// super class of 'ISVportrate'
@synthesize index;

// ... my methods ...
@end
@implementation ISVLandscape : ImageScrollView

// error: property 'index' attempting to use ivar 'index' declared in
// super class of 'ISVLandscape'
@synthesize index;

// ... my methods ...
@end

我做的不对,是吗?见上面我有4个错误……这是我第一次上课……帮助我理解,我想我几乎做对了。

【问题讨论】:

  • 这是什么编程语言?对于未来,请至少在标签中提及编程语言,但最好在问题帖子本身中提及。
  • 在我看来像 Objective-C。
  • iphone sdk xcode objc LOL 对不起,maaaaate! :P

标签: objective-c class subclass superclass


【解决方案1】:

@synthesizeImageScrollView@implementation 中,而不是在子类中。

self.ISVportrate 没有意义(除非你有一个名为-ISVportrate 的方法,这也没有意义)。

听起来您还没有完全了解面向对象的编程。您可能希望创建一个子类的适当实例并将其分配为包含它的任何视图的子视图....

【讨论】:

  • 好的,我已经开始喝伏特加了,所以我现在不太确定你在说什么,如果你能详细说明一下,我会清醒地看着它,我相信我会明白的。我现在确实在飞跃式地学习。
  • 所以你说@synthesize 进入ImageScrollView 的@implementation... 这是否意味着我应该保留ImageScrollView 的实现并为ISVportrate 和ISVlandscape 复制ImageScrollView 中的内容?因为现在我没有仅限图像滚动视图的接口:@implementation ISVportrate:ImageScrollView 和@implementation ISVlandscape:ImageScrollView 不是:@implementation ImageScrollView。我不确定 NSInteger 索引...必须在哪里声明等...谢谢伙计!
  • 阅读并重新阅读此内容:developer.apple.com/mac/library/documentation/Cocoa/Conceptual/… 您需要了解面向对象编程的基础知识才能成功。 (真的——这是最好的前进方式。)
猜你喜欢
  • 2014-03-23
  • 1970-01-01
  • 1970-01-01
  • 2021-03-13
  • 2013-10-17
  • 1970-01-01
  • 2013-02-25
相关资源
最近更新 更多