【问题标题】:Passing an Object to UIViewController Class将对象传递给 UIViewController 类
【发布时间】:2011-04-14 08:42:52
【问题描述】:

我正在开发一个应用程序,我想在其中将一个类的对象从一个 UIViewController 传递到另一个,我该怎么做?

【问题讨论】:

    标签: iphone objective-c object uiviewcontroller


    【解决方案1】:

    当你初始化你的新视图控制器时,定义一个变量如下:

     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 成为allocinitMyClass *oMyClass = [[MyClass alloc] init];
    【解决方案2】:

    假设您有一个firstViewController 和一个secondViewController。 假设您想将NSString *testString; 从第一个视图传递到第二个视图。

    在这种情况下,你应该在 secondViewController.h 文件中使用 @property 声明这个 NSString,并在 secondViewController.m 文件中使用 @synthesize 它。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-13
      • 2011-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多