Iphone 从父视图控制器获取字符串?
我需要从父视图控制器获取一个NSString到它的子视图Iphone 从父视图控制器获取字符串?,iphone,objective-c,cocoa-touch,ipad,uiviewcontroller,Iphone,Objective C,Cocoa Touch,Ipad,Uiviewcontroller,我需要从父视图控制器获取一个NSString到它的子视图 @property (nonatomic, retain) NSString *childString; 所以我有了“父视图”->“子视图” 我需要从ParentView到ChildView获取字符串。我尝试添加一个方法,该方法在我的ParentView中返回一个字符串,并在ChildView中像这样调用它,但没有成功 不起作用: ViewController *vc = [[ViewController alloc] init]; s
@property (nonatomic, retain) NSString *childString;
所以我有了“父视图”->“子视图”
我需要从ParentView到ChildView获取字符串。我尝试添加一个方法,该方法在我的ParentView中返回一个字符串,并在ChildView中像这样调用它,但没有成功
不起作用:
ViewController *vc = [[ViewController alloc] init];
startDateLbl.text = [vc string];
对于如何实现这一点的任何帮助,我们将不胜感激。这一行
ViewController*vc=[[ViewController alloc]init]代码>将创建一个新对象
只是给你一个简单的代码,希望你得到它
ChildViewController *viewController = [[ChildViewController alloc] initWithNibName:@"ChildViewController" bundle:nil];
[self.navigationController pushViewController:viewController animated:YES];
//here set your label, make sure startDateLbl has getter property
viewController.startDateLbl.text = [vc string];
最简单的方法是在子视图中为字符串设置属性
@property (nonatomic, retain) NSString *childString;
然后在将视图推送到堆栈之前或之后,将字符串从父视图传递到子视图
ViewController *vc = [[ViewController alloc] initWithNibName:@"ViewController"];
vc.childString = parentString;
[self.navigationController pushViewController:vc animated:YES]; // this assumes navController
根据UIViewController类型,您可以访问父视图控制器
self.parentViewController.someString(请参阅UIViewController文档) 感谢您的回答,尽管设置字符串值仅在按下视图后进行设置时有效。不,您可以在alloc/init视图后的任何时间分配值。可以在推送视图之前或之后指定它们。我总是这样做。