[英]How do I get a variable in the superclass for use in the subclass of that superclass?
[英]How to access superclass variable in subclass
我创建了两个viewController类,其中一个是另一个超类.i在超类中有一个nsstring变量,我必须在子类中访问,请指导我如何操作。 这是我的代码
@interface Superclass : UIViewController{
NSString *message
}
@property(nonatomic,retain)NSString *message;
-(id)init;
@end
@implementation Superclass
@synthesize message;
-(id)init{
{
[super init];
message=@"Hello";
return self;
}
@interface Subclass : Superclass{
}
@end
@implementation Subclass
- (void)viewDidLoad {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Alert" message:self.message delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
[alert show];
[alert release];
[super viewDidLoad];
}
我的警告提示但没有消息。
你是如何初始化Subclass
?
使用initWithNibName:bundle:
方法?
在这种情况下,不会调用您的超类init
方法。 因此,在超类中覆盖initWithNibName:bundle:
方法,并将值设置为该变量。
只是用
self.message = @“something”
,超类的成员将由子类对象继承
你可以使用super.variable
来访问超类变量。
将变量声明为public为超类
比
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Alert" message:message delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
[alert show];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.