我刚开始学习iOS编程,并遇到继承问题。有2个文件。如何在iOS中继承工作sdk
第一个文件
页眉
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController {
UILabel *myLabel;
}
@property (nonatomic, retain) IBOutlet UILabel *myLabel;
@end
实现:
#import "ViewController.h"
#import "NewClass.h"
@implementation ViewController
@synthesize myLabel;
#pragma mark - View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
myLabel.text = @"ViewController text!";
NewClass *myClass = [[[NewClass alloc] init] autorelease];
[myClass setLabelText];
}
@end
第二个文件
页眉:
#import "ViewController.h"
@interface NewClass : ViewController
-(void) setLabelText ;
@end
实现:
#import "NewClass.h"
@implementation NewClass
- (void) setLabelText {
myLabel.text = @"NewClass text!";
}
-(id)init {
self = [super init];
if (self != nil) {
}
return self;
}
@end
我和IB设置myLabel
出口。
为什么当我打电话[myClass setLabelText];
什么都没有发生?还保留了“ViewController文本!”在标签视图上。 我的问题在哪里?我如何更改ViewController::myLabel.text
NewClass
更糟糕的是,因为myClass是ViewController的子类,所以当(或如果)加载时,它实际上会创建它自己的实例myClass ...并循环代码。 Voila - 堆栈溢出错误! –