2010-02-09 113 views
3

我是新的iphone开发,现在我想访问所有视图控制器中的字符串变量,但我知道在委托方法中声明变量,但我不能访问它,请帮助我。如何在一个视图控制器访问字符串变量到另一个视图控制器

Mainviewcontroller - > Viewcontroller1 _--> viewcontroller2 - > viewcontroller3 - > subwebview。

我已经创建了一个主视图控制器,子视图类是Viewcontroller1,viewcontroller2,viewcontroller3。子网页视图是所有三个视图控制器(Viewcontroller1,Viewcontroller2,Viewcontroller3)的子类。

现在我想从所有viewcontroller(Viewcontroller1,Viewcontroller2,Viewcontroller3)访问subwebview中的字符串变量,我如何访问SUBWEBVIEW中的字符串变量。

我正在使用Rss订阅源并检索数据。

我已经宣布所有的视图控制器在我的字符串变量一样,

NSString * currentElement; 
NSMutableString * currentTitle, * currentDate, * currentSummary, * currentLink; 

我在这里停留。我无法继续进行。请帮助我。

谢谢。

回答

5

最简单的方法是只传入对parentViewController的引用,然后通过该引用访问变量。不知道你用的是什么类的名称,但这样的事情:

MyChildController *tmpViewController = [[MyChildController alloc] initWithNibName:@"ChildView" bundle:nil]; 
tmpViewController.myParentController = self; 

当然,这需要你创建在子控制器属性父之一:

ParentController *myParentController; 

添加@property@synthesize它还有

+0

此ParentController的属性属性是什么。 – SNR

+0

@ raj2raaz - 非原子,保留会正常工作 – willcodejavaforfood

0

我不知道这是正确的方式,但我一直在关注这一个,创建一个新的头类文件和进口,在所有viewcontrollers头。在新创建的文件中声明该字符串。要做到这一点

+0

您以这种方式失去了OOP的所有好处 – Nader

0

最简单的方法就是让你的subwebview你的变量的属性。

@interface SubWebView : UIViewController { 
    NSString * currentElement; 
    NSMutableString *currentTitle; 
    NSDate *currentDate; 
    NSMutableString *currentSummary; 
    NSURL *currentLink; 
} 

@property (copy) NSString *currentElement; 
@property (copy) NSString *currentTitle; 
@property (copy) NSDate *currentDate; 
@property (copy) NSString *currentSummary; 
@property (copy) NSURL *currentLink; 

@end 

@implementation SubWebView 

@synthesize currentElement; 
@synthesize currentTitle; 
@synthesize currentDate; 
@synthesize currentSummary; 
@synthesize currentLink; 

@end 

// Then to access these properties 

subwebview.currentDate = [NSDate date]; 
// etc... 
0

呼叫viewController2viewController1

地点的代码在ViewController1.m

//////****************/////// 

viewController2=[[ViewController2 alloc] initWithNibName:@"ViewController2" bundle:nil]; 
[email protected]"ABC"; 

//////****************/////// 

确保string2应在ViewController2.h文件中定义

2

添加下面的功能在你的主要观点控制器:

SecondViewController *second= [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil]; 

tmpViewController.myParentController = self; 

添加下面的属性在你的第二个视图控制器:

FirstViewController *first; 

然后合成它像往常一样。

相关问题