如何将一个视图控制器中的字符串变量访问到另一个视图控制器

发布于 2024-08-20 14:07:46 字数 685 浏览 7 评论 0原文

我是 iPhone 开发新手,现在我想访问所有视图控制器中的字符串变量,但我知道在委托方法中声明变量,但我无法访问它,请帮助我。

Mainviewcontroller-->Viewcontroller1_-->viewcontroller2-->viewcontroller3-->subwebview。

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

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

我正在使用 Rss feed 阅读器并检索数据。

我已经在我的字符串变量中声明了所有视图控制器,就像

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

我被困在这里一样。我无法继续。请帮助我。

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

一页 2024-08-27 14:07:46

最简单的方法是传入对parentViewController 的引用,然后您可以通过该引用访问变量。不确定您使用的类名是什么,但类似这样:

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

这当然需要您在子控制器中为父控制器创建一个属性:

ParentController *myParentController;

添加 @property@synthesize 也是如此

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

玩心态 2024-08-27 14:07:46

在主视图控制器中添加以下函数:

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

tmpViewController.myParentController = self;

在第二个视图控制器中添加以下属性:

FirstViewController *first;

然后照常合成它。

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.

牵你的手,一向走下去 2024-08-27 14:07:46

我不确定这是正确的方法,但我一直在遵循这个方法,创建一个新的标头类文件并将该标头导入所有视图控制器中。在新创建的文件中声明该字符串。

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.

淡看悲欢离合 2024-08-27 14:07:46

最简单的方法是将变量设置为 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...

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...
软糖 2024-08-27 14:07:46

viewController1 中调用 viewController2

将该代码放入 ViewController1.m

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

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

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文