本文介绍了将对象传递给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?

推荐答案

在初始化新的View Controller时,请定义一个变量,如下所示:

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

别忘了您必须在NextViewController中定义类型为(MyClass)的对象.

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

样品

NextViewController. h

NextViewController.h

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

@property (nonatomic,retain) MyClass *myClassObject;


NextViewController. m


NextViewController.m

@synthesize myClassObject;

这篇关于将对象传递给UIViewController类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-29 02:28