How can i pass data back to parent view controller. am using addSubView method to show child view

隐身守侯 提交于 2019-12-25 00:14:49

问题


How can i pass data back to parent view controller. im using addSubView method to show child view.

addFields = [[AddFields alloc] initWithNibName:@"AddFields" bundle:nil];
[self.view addSubview:addFields.view];

Now i want to sent data from "AddFields" view to my parent controller. How can i do that. Can anyone help me..


回答1:


One simple way to do is using NSNotificationCenter see this example

Post you notification from your AddFields when you want to send the object

[[NSNotificationCenter defaultCenter] postNotificationName:@"notificaiton_name" object:object_you_want_to_send];

And in your parent view controller add observer for the NSNotificationCenter

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(method_you_want_to_call:) name:@"notificaiton_name" object:nil];

Define your method_you_want_to_call: something like this

-(void) method_you_want_to_call:(NSNotification *) obj{
     //lets say you are sending string as object
     NSString *string=(NSString *) [obj object] ;
}

NOTE: notificaiton_name must match

Other way you can use Delegate Protocol method... See this example




回答2:


Use Delegate for backward passing of data between child to parent view.




回答3:


The most common method is as follows: First initialise a string/int/float/.. (according to your need) in the parent controller. For example: NString *string;

Then in the AddFields view, import parent controller and then make an object of the parent controller view.

ParentViewController *object=[[ParentViewController alloc]initWithNibName:@"ParentViewController" bundle:nil];

            object.string=theValueYouWantToPass;


来源:https://stackoverflow.com/questions/12524407/how-can-i-pass-data-back-to-parent-view-controller-am-using-addsubview-method-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!