如何将一个视图控制器中的字符串变量访问到另一个视图控制器
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
最简单的方法是传入对parentViewController 的引用,然后您可以通过该引用访问变量。不确定您使用的类名是什么,但类似这样:
这当然需要您在子控制器中为父控制器创建一个属性:
添加
@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:
This of course requires you to create a property in the child controller for the parent one:
Add a
@property
and@synthesize
for it as well在主视图控制器中添加以下函数:
在第二个视图控制器中添加以下属性:
然后照常合成它。
Add the below function in your Main view controller:
Add the below property in your second view controller:
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.
最简单的方法是将变量设置为
subwebview
的属性。Easiest way to do this is to make your variables properties of your
subwebview
.在
viewController1
中调用viewController2
将该代码放入
ViewController1.m
确保
string2
应在中定义>ViewController2.h
文件Call
viewController2
inviewController1
Place that code in
ViewController1.m
Make sure that
string2
should be defined in theViewController2.h
file