2012-03-26 53 views
1

我已经创建了两个的viewController类,一个是another.i的超有超一的NSString变量,我在子类中访问,请指导我该怎么做。这里是我的代码如何访问超变量在子类中

@interface Superclass : UIViewController{ 
    NSString *message 
    } 
    @property(nonatomic,retain)NSString *message; 
    -(id)init; 
    @end 


@implementation Superclass 
@synthesize message; 
-(id)init{ 
{ 
[super init]; 
[email protected]"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]; 


} 

我的警报promt但没有消息。

回答

3

如何要初始化的Subclass

使用initWithNibName:bundle:方法?

在这种情况下,你的超init方法不会被调用。所以覆盖超类initWithNibName:bundle:方法并将其值设置为变量存在。

+0

是的,使用initWithNibname:绑定: – NoviceDeveloper 2012-03-26 13:07:23

+0

之后,使用'消息'的超类'对象,无论你想访问它...像superclass.message ... :) – Krunal 2012-03-26 13:10:10

+0

你还需要保留消息在你的超类 – 2012-03-26 13:11:06

0

可以使用super.variable访问超类变量。

+0

使用super.message,但没有成功:( – NoviceDeveloper 2012-03-26 13:08:38

+1

如果子类没有实现它自己的版本的属性,'self.variable'是你所需要的。 – jrturton 2012-03-26 13:08:57

+0

@ jrturton,我知道。我没有意识到他没有一个子类的版本。不管怎么说,还是要谢谢你。 – Vignesh 2012-03-26 13:19:35

0

声明变量作为众成超类

UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Alert" message:message delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil]; 
[alert show]; 
0

只使用

self.message = @ “东西”

,超类的成员将继承子类对象