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

来源:http://www.cnblogs.com/bolin-123/archive/2016/03/15/5281625.html
-Advertisement-
Play Games

第二個界面中的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

 

(三)代碼塊傳值

 

(四)通知傳值

 

 

(五)單例傳值

 

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • CSS: HTML: JS:
  • 點擊按鈕,自動生成100個li,紅、黃、藍、綠四種顏色的順序顯示出現在頁面中 CSS: HTML: JS:
  • 一,效果圖。 二,工程圖。 三,代碼。 RootViewController.h RootViewController.m
  • 當內容及分類較多時,往往採用頂部標簽式導航欄,例如網易新聞客戶端的頂部分類導航,最近剛好有這樣的應用場景,參考網路上一些demo,實現了這種導航效果,記錄一些要點。 效果圖(由於視頻轉GIF掉幀,滑動和下拉動畫顯得比較生硬,剛發現quickTime可以直接錄製手機視頻,推薦一下,很方便) 其實就是在
  • Android應用檢查版本更新後,在通知欄下載,更新下載進度,下載完成自動安裝,效果圖如下: AndroidManifest文件中的versionCode用來標識版本,在伺服器放一個新版本的apk,versioncode大於當前版本,下麵代碼用來獲取versioncode的值 用當前versionc
  • 有時候我們在頁面跳轉的時候回傳遞相應的參數,如,你想把在第一個頁面的文本框里的內容顯示在第二個文本框中,或者你又想把第二個文本框中的內容改變之後到第一個頁面的文本框中,所有,這個時候我們就要用到頁面跳轉傳值 屬性傳值是正向傳值,只可以從前面一個頁面傳遞到第二個頁面,不可以從第二個頁面傳遞到第一個頁面
  • 在一些場景中,需要限制用戶的輸入字數,例如在textField里進行控制(textView也類似,崩潰原因也相同),如圖所示 系統會監聽文本輸入,需要註意的第一點是輸入法處於聯想輸入還未確定提交的時候,對於第三方輸入法例如搜狗,聯想狀態下的輸入,是監聽不到,除非點擊確定輸入到textField,才會
  • 單機搭建Android開發環境四,介紹了基於64位Ubuntu 12.04 Server版安裝git、repo、編譯環境,從內部伺服器下載Android5.1代碼並編譯測試。
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...