将对象传递给 UIViewController 类
我正在开发一个应用程序,我想将一个类的对象从一个 UIViewController 传递到另一个 UIViewController,我该怎么做?
I am working on an app in which I want to pass an object of a class from one UIViewController to another, How I can do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您初始化新的视图控制器时,请按如下方式定义一个变量:
不要忘记您必须在 NextViewController 中定义一个类型为 (MyClass) 的对象。
示例
NextViewController.h
NextViewController.m
when you initialize your new View Controller define a variable as follows:
Don't forget you have to define an object of type (MyClass) in NextViewController.
Sample
NextViewController.h
NextViewController.m
假设您有一个
firstViewController
和一个secondViewController
。假设您想将
NSString *testString;
从第一个视图传递到第二个视图。在这种情况下,您应该在 secondaryViewController.h 文件中使用
@property
声明此 NSString,并在 secondaryViewController.m 文件中使用@synthesize
声明此 NSString。在第一个ViewController中,当您创建第二个ViewController的实例时(通过SecondViewController *SecondView = [[SecondViewController alloc] initWith...];,使用以下行:SecondView.stringYouCreatedUsingSynthesize = testString;
Say you have a
firstViewController
and asecondViewController
.Say, you want to pass on a
NSString *testString;
from the first to the second view.In that case, you should declare this NSString using
@property
in the secondViewController.h-file, and@synthesize
it in the secondViewController.m-file.In the firstViewController, when you create an instance of the secondViewController (through
secondViewController *secondView = [[secondViewController alloc] initWith...];
, use the line:secondView.stringYouCreatedUsingSynthesize = testString;