Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone iOS开发人员-如何将字符串obj从一个类传递到另一个类?_Iphone_Objective C_String_Cocoa Touch_Object - Fatal编程技术网

Iphone iOS开发人员-如何将字符串obj从一个类传递到另一个类?

Iphone iOS开发人员-如何将字符串obj从一个类传递到另一个类?,iphone,objective-c,string,cocoa-touch,object,Iphone,Objective C,String,Cocoa Touch,Object,可能重复: 我有两个视图控制器,我想从我的应用程序的上一个视图中获取一些信息。e、 g: 在我的应用程序中,我从第一页转到第二页。根据用户按下的按钮,我想更改第二个屏幕上的信息。最快最简单的方法是什么?我尝试导入类并重新生成,但这会重新创建字符串obj,并且不会保留我想要的信息。在第二个(“子”)视图控制器中,为字符串保留一个值(请参见第9节) 实例化第二个视图控制器时,在将其从第一个视图控制器推送到堆栈上之前,请设置字符串属性的值,例如,保留第一个控制器的字符串: mySecondViewC

可能重复:

我有两个视图控制器,我想从我的应用程序的上一个视图中获取一些信息。e、 g:

在我的应用程序中,我从第一页转到第二页。根据用户按下的按钮,我想更改第二个屏幕上的信息。最快最简单的方法是什么?我尝试导入类并重新生成,但这会重新创建字符串obj,并且不会保留我想要的信息。

在第二个(“子”)视图控制器中,为字符串保留一个值(请参见第9节)

实例化第二个视图控制器时,在将其从第一个视图控制器推送到堆栈上之前,请设置字符串属性的值,例如,保留第一个控制器的字符串:

mySecondViewController.infoString = myFirstViewController.infoString;
确保您的第二视图控制器管理字符串的内存(通常在控制器的
dealloc
方法中使用
release
消息,假设您的属性是用
retain
定义的)

第二个选项是将属性保留在应用程序委托中,或另一个管理应用程序数据的单例中。但是第一种方法要轻一点。

嗯,(至少)有两种可能性:

  • 将属性添加到下一个视图控制器,执行以下操作
  • 创建自定义初始化方法:

如果我理解正确,您需要的是在vc2中创建一个实例变量。然后,当您从vc1创建vc2实例时,您可以在呈现vc2之前访问该iVar以分配值等。以下是一个例子:

在ViewController2.h文件中:

@interface ViewController2
{
  NSString *string2;  //create an instance variable
}

@property (nonatomic, retain) NSString *string2;
在ViewController2.m文件中:

@implementation ViewController2
@synthesize string2;
在ViewController1.m文件中:

@implementation ViewController1

//ViewController2  *viewController2 = [[ViewController2 alloc] initWithNibName:@"ViewController2" bundle:nil];  //one way to instantiate 

viewController2.string2 = @"whatever string";  //here you assign the value to the instance variable string2 in viewController2



 //[self.navigationController pushViewController:childController animated:YES];  //etc. it depend on how you present viewcontroller2 

这已经被问和回答之前不是重复,但,超级有用的链接,谢谢!这在代码中是什么样子的?请阅读有关属性的链接。该链接包含解释如何在类中设置属性的代码段。然后只需设置该属性,通常在创建第二个视图控制器之后,在将其推到导航堆栈之前。很酷,但我还是有点迷茫。我尝试设置属性,但如何在不在其他视图控制器中重新创建的情况下访问它?因此,重新创建字符串obj。虽然我希望我可以使用它,但即使我导入了它,它也会一直说找不到该属性。也许可以将您的项目放在一边,开发一个演示测试应用程序,这样您就可以轻松地学习属性。
@implementation ViewController2
@synthesize string2;
@implementation ViewController1

//ViewController2  *viewController2 = [[ViewController2 alloc] initWithNibName:@"ViewController2" bundle:nil];  //one way to instantiate 

viewController2.string2 = @"whatever string";  //here you assign the value to the instance variable string2 in viewController2



 //[self.navigationController pushViewController:childController animated:YES];  //etc. it depend on how you present viewcontroller2