属性传值和协议传值
属性传值三部.....
1.在第二个页面.h中,定义name
//属性传值............1
@property (nonatomic, copy)NSString *name;
2.在第一页.m中然后在推出第二个页面前,把第一个按钮的title值传给第二个页面定义的name
SecondViewController *secondVC = [[SecondViewController alloc] init];
//属性传值..............2
secondVC.name = button.currentTitle; //把按钮名给第二个页面的name
[self.navigationController pushViewController:secondVC animated:YES];
[secondVC release];
3.在第二页.m中让TextField来接收第一页传过来的值
//属性传值........3
self.TextField.text = self.name; //接收从第一页传过来的name
协议传值六部走........
协议传值六个步骤分别是在第二个页面三部,在第一个页面三个;
1.首先在第二页的.h中自定义一个协议SecondPassValueDelegate,然后在里面写一个方法,用来改变按钮的title
//协议传值----1;
//指定一个协议
@protocolSecondPassValueDelegate <NSObject>
- (void)changeButtonTitle:(NSString*)title;
@end
2.在第二页.h中,定义一个代理人对象属性(记得为id类型,并且是assign)
//协议传值----2
//定一个代理人对象属性
//只有实现了上面定义的协议方法的对象,才能为第二个页面的代理人
@property (nonatomic , assign)id <SecondPassValueDelegate>delegate;
3.在第二页.m方法中 ,让自己的代理人去执行约定好的方法,获得里面的值
/协议传值----3
//让自己的代理人(delegate)去执行约定好的方法 获取里面的值
[self.delegate changeButtonTitle:_TextField.text];
4.在第一个页面签订第二个页面的协议 ,对应第一步的创建协议 (这里的协议可以在.h中写,也可以在.m中私有方法实现.---这里是后者 .)
//协议传值-----4
//第一个页面签订第二个页面的协议 对应第一步的创建协议
@interface FirstViewController () <SecondPassValueDelegate>
@end
5.在第一个页面的.m方法中 ,把自己指定为第二个页面的代理人
//把自己指定为第二个页面的代理人
secondVC.delegate = self;
6.在第一个页面中实现协议的方法
- (void)changeButtonTitle:(NSString *)title
{
//获得按钮
UIButton *button = (UIButton *)[self.view viewWithTag:1000];
//给按钮 重新设置标题
[button setTitle:title forState:UIControlStateNormal];
UIButton *button1 = (UIButton *)[self.view viewWithTag:2000];
[button1 setTitle:title forState:UIControlStateNormal];
}
类和文件
AppDelegate.m
#import"AppDelegate.h"#import"FirstViewController.h"@implementationAppDelegate-(BOOL)application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptions{self.window=[[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]];//Overridepointforcustomizationafterapplicationlaunch.self.window.backgroundColor=[UIColorwhiteColor];[self.windowmakeKeyAndVisible];FirstViewController*firstVC=[[FirstViewControlleralloc]init];UINavigationController*naviVC=[[UINavigationControlleralloc]initWithRootViewController:firstVC];self.window.rootViewController=naviVC;[firstVCrelease];[_windowrelease];returnYES;}-(void)dealloc{[_windowrelease];[superdealloc];}-(void)applicationWillResignActive:(UIApplication*)application{//Sentwhentheapplicationisabouttomovefromactivetoinactivestate.Thiscanoccurforcertaintypesoftemporaryinterruptions(suchasanincomingphonecallorSMSmessage)orwhentheuserquitstheapplicationanditbeginsthetransitiontothebackgroundstate.//Usethismethodtopauseongoingtasks,disabletimers,andthrottledownOpenGLESframerates.Gamesshouldusethismethodtopausethegame.}-(void)applicationDidEnterBackground:(UIApplication*)application{//Usethismethodtoreleasesharedresources,saveuserdata,invalidatetimers,andstoreenoughapplicationstateinformationtorestoreyourapplicationtoitscurrentstateincaseitisterminatedlater.//Ifyourapplicationsupportsbackgroundexecution,thismethodiscalledinsteadofapplicationWillTerminate:whentheuserquits.}-(void)applicationWillEnterForeground:(UIApplication*)application{//Calledaspartofthetransitionfromthebackgroundtotheinactivestate;hereyoucanundomanyofthechangesmadeonenteringthebackground.}-(void)applicationDidBecomeActive:(UIApplication*)application{//Restartanytasksthatwerepaused(ornotyetstarted)whiletheapplicationwasinactive.Iftheapplicationwaspreviouslyinthebackground,optionallyrefreshtheuserinterface.}-(void)applicationWillTerminate:(UIApplication*)application{//Calledwhentheapplicationisabouttoterminate.Savedataifappropriate.SeealsoapplicationDidEnterBackground:.}@end
FirstViewController.h
#import<UIKit/UIKit.h>@interfaceFirstViewController:UIViewController@end
FirstViewController.m
#import"FirstViewController.h"#import"SecondViewController.h"//协议传值-----4//第一个页面签订第二个页面的协议对应第一步的创建协议@interfaceFirstViewController()<SecondPassValueDelegate>@end@implementationFirstViewController-(id)initWithNibName:(NSString*)nibNameOrNilbundle:(NSBundle*)nibBundleOrNil{self=[superinitWithNibName:nibNameOrNilbundle:nibBundleOrNil];if(self){//Custominitialization}returnself;}-(void)viewDidLoad{[superviewDidLoad];//Doanyadditionalsetupafterloadingtheview.self.title=@"第一页";UIButton*button=[[UIButtonalloc]initWithFrame:CGRectMake(120,100,80,25)];[buttonsetTitle:@"按钮"forState:UIControlStateNormal];[buttonsetTitleColor:[UIColorblackColor]forState:UIControlStateNormal];button.layer.cornerRadius=5;button.tag=1000;button.backgroundColor=[UIColorcyanColor];[buttonaddTarget:selfaction:@selector(buttonClicked:)forControlEvents:UIControlEventTouchUpInside];[self.viewaddSubview:button];[buttonrelease];UIButton*button1=[[UIButtonalloc]initWithFrame:CGRectMake(120,160,80,25)];[button1setTitle:@"asdsadf"forState:UIControlStateNormal];[button1setTitleColor:[UIColoryellowColor]forState:UIControlStateNormal];button1.layer.cornerRadius=5;button1.tag=2000;button1.backgroundColor=[UIColorblueColor];[button1addTarget:selfaction:@selector(buttonClicked:)forControlEvents:UIControlEventTouchUpInside];[self.viewaddSubview:button1];[button1release];}-(void)buttonClicked:(UIButton*)button{SecondViewController*secondVC=[[SecondViewControlleralloc]init];//属性传值..............2secondVC.name=button.currentTitle;//把按钮名给第二个页面的name//协议传值------5//把自己指定为第二个页面的代理人secondVC.delegate=self;[self.navigationControllerpushViewController:secondVCanimated:YES];[secondVCrelease];}//协议传值-------6//实现协议方法-(void)changeButtonTitle:(NSString*)title{//获得按钮UIButton*button=(UIButton*)[self.viewviewWithTag:1000];//给按钮重新设置标题[buttonsetTitle:titleforState:UIControlStateNormal];UIButton*button1=(UIButton*)[self.viewviewWithTag:2000];[button1setTitle:titleforState:UIControlStateNormal];}-(void)didReceiveMemoryWarning{[superdidReceiveMemoryWarning];//Disposeofanyresourcesthatcanberecreated.}/*#pragmamark-Navigation//Inastoryboard-basedapplication,youwilloftenwanttodoalittlepreparationbeforenavigation-(void)prepareForSegue:(UIStoryboardSegue*)seguesender:(id)sender{//Getthenewviewcontrollerusing[seguedestinationViewController].//Passtheselectedobjecttothenewviewcontroller.}*/@end
SecondViewController.h
#import<UIKit/UIKit.h>//协议传值----1;//指定一个协议@protocolSecondPassValueDelegate<NSObject>-(void)changeButtonTitle:(NSString*)title;@end@interfaceSecondViewController:UIViewController@property(nonatomic,retain)UITextField*TextField;//属性传值............1@property(nonatomic,copy)NSString*name;//协议传值----2//定一个代理人对象属性//只有实现了上面定义的协议方法的对象,才能为第二个页面的代理人@property(nonatomic,assign)id<SecondPassValueDelegate>delegate;@end
SecondViewController.m
#import"SecondViewController.h"@interfaceSecondViewController()<UITextFieldDelegate>@end@implementationSecondViewController-(id)initWithNibName:(NSString*)nibNameOrNilbundle:(NSBundle*)nibBundleOrNil{self=[superinitWithNibName:nibNameOrNilbundle:nibBundleOrNil];if(self){//Custominitialization}returnself;}-(void)viewDidLoad{[superviewDidLoad];//Doanyadditionalsetupafterloadingtheview.self.title=@"第二页";self.view.backgroundColor=[UIColorbrownColor];UIButton*button=[[UIButtonalloc]initWithFrame:CGRectMake(120,150,100,35)];[buttonsetTitle:@"返回"forState:UIControlStateNormal];button.layer.cornerRadius=5;button.backgroundColor=[UIColormagentaColor];[buttonaddTarget:selfaction:@selector(buttonClicked:)forControlEvents:UIControlEventTouchUpInside];[self.viewaddSubview:button];self.TextField=[[UITextFieldalloc]initWithFrame:CGRectMake(80,80,160,50)];self.TextField.backgroundColor=[UIColorwhiteColor];self.TextField.delegate=self;self.TextField.clearButtonMode=UITextFieldViewModeAlways;//属性传值........3self.TextField.text=self.name;//接收从第一页传过来的nameself.TextField.borderStyle=UITextBorderStyleRoundedRect;//TextField的边框圆形的边框[self.viewaddSubview:self.TextField];[_TextFieldrelease];}-(BOOL)textFieldShouldReturn:(UITextField*)textField{[textFieldresignFirstResponder];returnYES;}-(void)buttonClicked:(UIButton*)button{//协议传值----3//让自己的代理人(delegate)去执行约定好的方法获取里面的值[self.delegatechangeButtonTitle:_TextField.text];[self.navigationControllerpopViewControllerAnimated:YES];}-(void)didReceiveMemoryWarning{[superdidReceiveMemoryWarning];//Disposeofanyresourcesthatcanberecreated.}/*#pragmamark-Navigation//Inastoryboard-basedapplication,youwilloftenwanttodoalittlepreparationbeforenavigation-(void)prepareForSegue:(UIStoryboardSegue*)seguesender:(id)sender{//Getthenewviewcontrollerusing[seguedestinationViewController].//Passtheselectedobjecttothenewviewcontroller.}*/@end
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。