We all know that every page in Android is an activity, and intent is a link to activity, in iOS, each page is a uiviewcontroller, And the jump in iOS directly through the Navigationcontroller Pushviewcontroller: You can, but also directly to the page to jump to assign values, and Android inside is through intent to the value of the second page, iOS can be used directly from the Uiviewcontroller point, the forward value from the back, but through the protocol to achieve, that is, Android inside the so-called interface. See below:
We created two new Uiviewcontroller, the first one to pass the second direct assignment, and the second to pass the first one we make is the protocol. The code is as follows:
The first of the Firstcontroller:
firstcontroller.m//mycontroller////Created by Xiaoyuan on 15/4/25.//Copyright (c) 2015 Xiaoyuan. All rights reserved.//#import "FirstController.h" #import "SecondController.h" @interface Firstcontroller () < Changevcdelegate> @end @implementation firstcontroller-(void) viewdidload {[Super viewdidload]; Self.navigationController.navigationBar.barTintColor =[uicolor Redcolor]; [Self.navigationController.navigationBar Settranslucent:no]; UIBUTTON*BTN =[uibutton Buttonwithtype:uibuttontyperoundedrect]; Btn.frame = CGRectMake (0, 100, 50, 50); Btn.backgroundcolor =[uicolor Graycolor]; Cgpoint Center = btn.center; center.x = self.view.center.x; Btn.center = center; [Btn settitle:@ "point Me" forstate:uicontrolstatenormal]; [Btn addtarget:self Action: @selector (Goon) forcontrolevents:uicontroleventtouchupinside]; [Self.view ADDSUBVIEW:BTN]; Do any additional setup after loading the view.} -(void) goon{Secondcontroller*two =[[seCondcontroller Alloc]init]; TWO.A = 1;//Direct Assignment two.delegate = self; [Self.navigationcontroller pushviewcontroller:two animated:yes];//Jump}//protocol method The second page returns the execution of this method through the protocol-(void) Passvalue: ( NSString *) str sencode: (NSString *) secondstr{Uialertview *alreat = [[Uialertview alloc] initwithtitle:[nsstring Strin gwithformat:@ "%@", str] message:[nsstring stringwithformat:@ "%@", Secondstr] delegate:self cancelbuttontitle:@ "Cancel" otherbuttontitles:@ "OK", nil]; [Alreat show];} -(void) didreceivememorywarning {[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} /* #pragma mark-navigation//in a storyboard-based application, you'll often want to do a little preparation before Nav igation-(void) Prepareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) Sender {//Get the new view controller using [SEG UE Destinationviewcontroller]. Pass the selected object to the new view Controller.} */@endThe second Secondcontroller: I post the. h and. m Files:
secondcontroller.h// mycontroller//// Created by Xiaoyuan on 15/4/25.// Copyright (c) 2015 Xiaoyuan. All rights reserved.//#import <uikit/uikit.h>//protocol @protocol changevcdelegate <nsobject>-(void) PassValue :(NSString *) str sencode: (NSString *) secondstr; @end @interface Secondcontroller:uiviewcontroller@property (nonatomic , assign) int a;//declaration Agent @property (nonatomic, weak) id<changevcdelegate> delegate; @end
secondcontroller.m//mycontroller////Created by Xiaoyuan on 15/4/25.//Copyright (c) 2015 Xiaoyuan. All rights reserved.//#import "SecondController.h" @interface Secondcontroller () @end @implementation secondcontroller-(void) viewdidload {[Super viewdidload]; Self.view.backgroundColor = [Uicolor Whitecolor]; Do any additional setup after loading the view. Uilabel*text =[[uilabel alloc]initwithframe:cgrectmake (0, 100, 400, 100)]; Text.backgroundcolor =[uicolor Graycolor]; [Text settext:[nsstring stringwithformat:@ "I am a page pass over%d", SELF.A]]; Text.textalignment =nstextalignmentcenter; [Self.view Addsubview:text]; Return key Uibarbuttonitem *rightbutton = [[Uibarbuttonitem alloc] initwithtitle:@ "Back" Style:uibarbuttonitemstyledone targe T:self Action: @selector (backaction)]; Self.navigationItem.leftBarButtonItem = Rightbutton; }//return via protocol-(void) backaction{[self.delegate passvalue:@ "I am the second page to pass the value of" sencode:@ "I was also the second page back"]; [Self.navigationcontrolLer Popviewcontrolleranimated:yes];} -(void) didreceivememorywarning {[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} /* #pragma mark-navigation//in a storyboard-based application, you'll often want to do a little preparation before Nav igation-(void) Prepareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) Sender {//Get the new view controller using [SEG UE Destinationviewcontroller]. Pass the selected object to the new view Controller.} */@end
It's done! The great God do not spray.
SOURCE download
iOS learning Uiviewcontroller with value jump and protocol implementation (IV)