【发布时间】:2012-01-18 17:40:55
【问题描述】:
我刚开始学习 iOS 编程,遇到了继承问题。有 2 个文件。
第一个文件
标题
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController {
int x;
}
@end
实施:
#import "ViewController.h"
#import "NewClass.h"
@implementation ViewController
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
x = 999;
NewClass *myClass = [[[NewClass alloc] init] autorelease];
}
@end
第二个文件
标题:
#import "ViewController.h"
@interface NewClass : ViewController
@end
实施:
#import "NewClass.h"
@implementation NewClass
-(id)init {
self = [super init];
if (self != nil) {
NSLog(@"%i",x);
}
return self;
}
@end
在 ViewController 中我将 x 设置为 999,在 NewClass 中我想得到它,但是当我调用 NSLog(@"%i",x); 时它给了我 0。
我在哪里做错了?
【问题讨论】:
-
当我在 NewClass 中调用 NSLog(@"%i",x) 时,我想看到 999。如何解决什么问题?
标签: iphone objective-c ios model-view-controller