How to Access String Variable in One View Controll

2019-01-24 18:40发布

I am new to iphone development, Now i want to access the string variable in all the view controller, but i know to declare the variables in delegate method, but i cant access it, please help me out.

Mainviewcontroller-->Viewcontroller1_-->viewcontroller2-->viewcontroller3-->subwebview.

i have created one main view controller and the subview class are Viewcontroller1,viewcontroller2,viewcontroller3. And the subwebview is the subclass of all the three viewcontrollers(Viewcontroller1,Viewcontroller2,Viewcontroller3).

Now i want to access the string variables in subwebview from the all viewcontrollers(Viewcontroller1,Viewcontroller2,Viewcontroller3), How can i access the string variables in SUBWEBVIEW.

I am using Rss feed reader and retrieved the datas.

i have declared all the view controllers in my string variables like,

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

i am stuck here. and i am not able to proceed.please help me out.

Thanks.

5条回答
smile是对你的礼貌
2楼-- · 2019-01-24 19:06

Add the below function in your Main view controller:

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

tmpViewController.myParentController = self;

Add the below property in your second view controller:

FirstViewController *first;

Then synthesize it as usual.

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-01-24 19:09

Call viewController2 in viewController1

Place that code in ViewController1.m

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

viewController2=[[ViewController2 alloc] initWithNibName:@"ViewController2" bundle:nil];
viewController2.string2=@"ABC";

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

Make sure that string2 should be defined in the ViewController2.h file

查看更多
成全新的幸福
4楼-- · 2019-01-24 19:19

Easiest way to do this is to make your variables properties of your 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...
查看更多
老娘就宠你
5楼-- · 2019-01-24 19:26

The easiest is to just pass in a reference to the parentViewController and then you can access your variables through that reference. Not sure what class names you are using but something like this:

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

This of course requires you to create a property in the child controller for the parent one:

ParentController *myParentController;

Add a @property and @synthesize for it as well

查看更多
小情绪 Triste *
6楼-- · 2019-01-24 19:30

I'm not sure this is the right way, but i've been following this one, create a new header class file and import that header in all viewcontrollers. Declare the string in the newly created file.

查看更多
登录 后发表回答