How to Access String Variable in One View Controller to Another view Controller

我怕爱的太早我们不能终老 提交于 2019-11-29 05:16:42
willcodejavaforfood

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

rajesh evv

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.

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.

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...
IPhoneCrazy

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!