2012-03-26 55 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 = @ 「東西」

,超類的成員將繼承子類對象