Existing two Views (Viewcontroller,viewcontroller1), jump to ViewController1 from Viewcontroller with parameters in ViewController1 With valid data back to Viewcontroller after the data is selected, the delegate implements the above functions.
Project: Click to download
First,viewcontroller main code
button click Event-(ibaction) CLICKSEARCHBTN: (ID) sender {[[[ uiapplication sharedapplication] Keywindow] endediting:yes]; ViewController1 *_viewcontroller = [[[[ViewController1 alloc] init] autorelease]; _viewcontroller.delegate = self; _viewcontroller.transtext = Self.textField.text; [Self.navigationcontroller Pushviewcontroller:_viewcontroller animated:yes];} Proxy method for Viewcontroller1delegate-(void) Selectdata: (nsstring*) text{ Dispatch_async (Dispatch_get_global_queue ( Dispatch_queue_priority_default, 0), ^{ [Self.listarray removeallobjects]; for (int i=0; i<5; i++) { [Self.listarray addobject:[nsstring stringwithformat:@ '%d rows of data:%@_%d ', I+1,text, Arc4random_uniform (+)]; } Dispatch_async (Dispatch_get_main_queue (), ^{ [Self.datatableview reloaddata]; }); }
TwoViewController1 Main code
viewcontroller1.h//delegate written before #import, otherwise it might delegate cannot find protocol Declaration@protocol Viewcontroller1delegate, @protocol viewcontroller1delegate <NSObject> @optional-(void) Selectdata: (nsstring*) Text, @end//viewcontroller1.m-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) indexpath{ if ([Self.delegate respondstoselector: @selector (selectdata:)]) { //pass value via a delegate agreement [self.delegate Selectdata:[self.listarray ObjectAtIndex:indexPath.row]]; [Self.navigationcontroller Popviewcontrolleranimated:yes];} }
Three
iOS two view delegate (delegate) pass value