2012-02-13 76 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; 
相关问题