将对象传递给 UIViewController 类

发布于 2024-11-01 09:42:13 字数 79 浏览 1 评论 0原文

我正在开发一个应用程序,我想将一个类的对象从一个 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 技术交流群。

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

发布评论

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

评论(2

贵在坚持 2024-11-08 09:42:13

当您初始化新的视图控制器时,请按如下方式定义一个变量:

 MyClass *myClass = [[MyClass alloc] init];
 myClass.username = @"Rahul Kushwaha"; 
 // assume a property called username is defined in MyClass of a NSString type.

 NextViewController *controller = [NextViewController alloc];
 controller.myClassObject = myClass ;
 [self.navigationController pushViewController:controller animated:YES];

不要忘记您必须在 NextViewController 中定义一个类型为 (MyClass) 的对象。

示例

NextViewController.h

#import "MyClass.h"
@interface NextViewController
{
  MyClass *myClassObject;
}

@property (nonatomic,retain) MyClass *myClassObject;

NextViewController.m

@synthesize myClassObject;

when you initialize your new View Controller define a variable as follows:

 MyClass *myClass = [[MyClass alloc] init];
 myClass.username = @"Rahul Kushwaha"; 
 // assume a property called username is defined in MyClass of a NSString type.

 NextViewController *controller = [NextViewController alloc];
 controller.myClassObject = myClass ;
 [self.navigationController pushViewController:controller animated:YES];

Don't forget you have to define an object of type (MyClass) in NextViewController.

Sample

NextViewController.h

#import "MyClass.h"
@interface NextViewController
{
  MyClass *myClassObject;
}

@property (nonatomic,retain) MyClass *myClassObject;

NextViewController.m

@synthesize myClassObject;
那请放手 2024-11-08 09:42:13

假设您有一个 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 a secondViewController.
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;

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