第二個界面中的lable顯示第一個界面textField中的文本 首先我們建立一個RootViewControllers和一個DetailViewControllers,在DetailViewControllers中聲明一個textString屬性,用於接收傳過來的字元串, RootViewCont
(一)屬性傳值
(二)代理(委托)傳值
代理傳值 適用於 反向傳值 (從後往前傳) 1.1 創建協議 及協議方法 在反向傳值的頁面(SecondViewController)中
1.2 創建協議類型的屬性 在SecondViewController中創建屬性id<postValueDelegate> delegate
1.3 調用屬性 即delegate
在SecondViewController頁面中 對象傳值的方法中調用
[self.delegate postValue:self.textName.text];
1.4 在第一頁 即顯示修改過信息的頁面
遵循協議 實現協議方法 指定代理對象(即 在頁面傳值參數的方法中為代理賦值)
secondVc.delegate=self;
文件目錄:
FirstViewController.h
#import <UIKit/UIKit.h> #import "SecondViewController.h" @interface FirstViewController : UIViewController<UITextFieldDelegate,postValue> @property(strong,nonatomic) UITextField *textName; @property(strong,nonatomic) UIButton *nextbutton; @end
FirstViewController.m
#import "FirstViewController.h" @interface FirstViewController () @end @implementation FirstViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor=[UIColor redColor]; self.textName=[[UITextField alloc] initWithFrame:CGRectMake(100, 100, 200,50)]; self.textName.borderStyle=UITextBorderStyleLine; self.textName.delegate=self; [self.view addSubview:self.textName]; self.nextbutton=[[UIButton alloc] initWithFrame:CGRectMake(200, 200, 100, 50)]; [self.nextbutton setTitle:@"下一頁" forState:UIControlStateNormal]; self.nextbutton.backgroundColor=[UIColor grayColor]; [self.nextbutton addTarget:self action:@selector(nextPage) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:self.nextbutton]; } -(BOOL)textFieldShouldReturn:(UITextField *)textField { if ([textField isFirstResponder]) { [textField resignFirstResponder]; } return YES; } -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { [self.textName resignFirstResponder]; } -(void)nextPage { SecondViewController *secondVc=[[SecondViewController alloc] init]; secondVc.str=self.textName.text; secondVc.deletage=self; [self presentViewController:secondVc animated:YES completion:nil]; } -(void)postValue:(NSString *)str { self.textName.text=str; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
SecondViewController.h
#import <UIKit/UIKit.h>
// 創建協議
@protocol postValue <NSObject> -(void)postValue:(NSString *)str; @end @interface SecondViewController : UIViewController<UITextFieldDelegate> @property(strong,nonatomic) UITextField *textInfo; @property(strong,nonatomic) UIButton *backbutton; @property(strong,nonatomic) NSString *str; @property(strong,nonatomic) id<postValue> deletage; @end
SecondViewController.m
#import "SecondViewController.h" @interface SecondViewController () @end @implementation SecondViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor=[UIColor colorWithRed:0.541 green:0.878 blue:0.831 alpha:1.000]; self.textInfo=[[UITextField alloc] initWithFrame:CGRectMake(100, 100, 200, 50)]; self.textInfo.borderStyle=UITextBorderStyleLine; self.textInfo.text=self.str; self.textInfo.delegate=self; [self.view addSubview:self.textInfo]; self.backbutton=[[UIButton alloc] initWithFrame:CGRectMake(100, 200, 100, 50)]; [self.backbutton setTitle:@"上一頁" forState:UIControlStateNormal]; self.textInfo.text=self.str; [self.backbutton addTarget:self action:@selector(backPage) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:self.backbutton]; } -(BOOL)textFieldShouldReturn:(UITextField *)textField { if ([textField isFirstResponder]) { [textField resignFirstResponder]; } return YES; } -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { [self.textInfo resignFirstResponder]; } -(void)backPage { if (self.deletage!=0) { [self.deletage postValue:self.textInfo.text]; } [self dismissViewControllerAnimated:YES completion:nil]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
AppDelegate.h
#import <UIKit/UIKit.h> #import"FirstViewController.h" @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @end
AppDelegate.m
#import "AppDelegate.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { FirstViewController *firstVc=[[FirstViewController alloc] init]; self.window.rootViewController=firstVc; return YES; }
.......
@end
(三)代碼塊傳值
(四)通知傳值
(五)單例傳值