2011-04-14 87 views

回答

7

定义一个变量,如下所示:

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]; 

别忘了你要定义类型(M​​yClass的)在NextViewController的对象。

样品

NextViewController。 ħ

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

@property (nonatomic,retain) MyClass *myClassObject; 

NextViewController。

@synthesize myClassObject; 
+1

你可能想的是'alloc'是一个'alloc'和'init':'MyClass的* oMyClass = [[MyClass alloc] init];' – Rengers 2011-04-14 10:11:34

+0

@Rengers:谢谢 – 2011-04-14 10:17:52

0

假设你有一个firstViewControllersecondViewController。 假设您想要将NSString *testString;从第一个视图传递到第二个视图。

在这种情况下,您应该在secondViewController.h文件中使用@property以及在secondViewController.m文件中使用@synthesize声明此NSString。

在firstViewController,当您通过secondViewController *secondView = [[secondViewController alloc] initWith...];创建secondViewController(实例,使用行:secondView.stringYouCreatedUsingSynthesize = testString;

相关问题