iOS--頁面間的代理傳值(屬性、代理(委託)、代碼塊、單例、通知),ios--頁面

來源:互聯網
上載者:User

iOS--頁面間的代理傳值(屬性、代理(委託)、代碼塊、單例、通知),ios--頁面
(一)屬性傳值 

 

 

(二)代理(委託)傳值 代理傳值 適用於  反向傳值 (從後往前傳) 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

 

(三)代碼塊傳值

 

(四)通知傳值

 

 

(五)單例傳值

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.