2012-02-13 74 views
2

我已經creted一個UIView子類:如何將值從UIViewController傳遞給UIView?

@interface GraphClass : UIView 
{ 
    NSString *str; 
} 

,我已經指派GraphClass:給名爲UIViewController類的觀點:

@interface GraphViewController : UIViewController 
{ 


} 

現在我想傳遞的NSString *量,值從另一個

@interface MonthsListController : UIViewController 
{ 
} 
in viewDIdLoad 

- (void)viewDidLoad 
{ 
GraphClass *graph = [[GraphClass alloc]init]; 

graph.str = @"abc"; 

但UIViewController類我得到NULL值,如果我打印GraphClass

STR值
-(void)drawRect:(CGRect)rect 
{ 
    NSLog(@"Amount is = %@",str);//NULL VAlue :(
} 
} 
+0

僅有[搜索] (https://www.google.com/search?q=%40property+and+%40synthesize)for @ @ property' and'@ synthesize' ke ywords。 – 2012-02-13 12:54:02

+0

有很多方法可以做到這一點。嘗試在'AppDelegate'中使用'str',這樣就可以輕鬆保存或打印值。讓我知道它是否有幫助。 – Illep 2012-02-13 12:57:30

+0

是啊阿迪爾我已經設置屬性也:(:(#)(#)(#)(#)(#)(#)(#)(#)(#)(#)(#)或代碼:( – Shreedhar 2012-02-13 12:57:53

回答

2

MonthsListController.h

#import GraphClass.h 

GraphClass *graph; 

@property(nonstatic,retain)GraphClass *graph; 

MonthsListController.m

@synthesis graph; 

viewDidLoad中

self.graph= [[GraphClass alloc]init]; 
[self.graph str:@"xx"]; 

- (無效)的drawRect:(的CGRect)RECT

NSLog(@"%@",graph.str); 
+0

沒有工作 – Shreedhar 2012-02-13 13:12:16

0
#import "GraphClass.h" 

//在UIViewController類

GraphClass *graph = [[GraphClass alloc] init]; 

graph.recStr=Amount; 

[self.view addSubview:graph]; 

//在GraphClass

@interface GraphClass : UIView 
{ 
    NSString *recStr; 
} 

@propery (nonatomic,strong) NSString *recStr; 
相關問題