【发布时间】:2011-04-14 08:42:52
【问题描述】:
我正在开发一个应用程序,我想在其中将一个类的对象从一个 UIViewController 传递到另一个,我该怎么做?
【问题讨论】:
标签: iphone objective-c object uiviewcontroller
我正在开发一个应用程序,我想在其中将一个类的对象从一个 UIViewController 传递到另一个,我该怎么做?
【问题讨论】:
标签: iphone objective-c object uiviewcontroller
当你初始化你的新视图控制器时,定义一个变量如下:
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;
【讨论】:
alloc 成为alloc 和init:MyClass *oMyClass = [[MyClass alloc] init];
假设您有一个firstViewController 和一个secondViewController。
假设您想将NSString *testString; 从第一个视图传递到第二个视图。
在这种情况下,你应该在 secondViewController.h 文件中使用 @property 声明这个 NSString,并在 secondViewController.m 文件中使用 @synthesize 它。
在firstViewController中,创建secondViewController的实例时(通过secondViewController *secondView = [[secondViewController alloc] initWith...];,使用行:secondView.stringYouCreatedUsingSynthesize = testString;
【讨论】: