国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

iOS-跨界面?zhèn)髦岛涂鐟?yīng)用傳值

2019-11-14 18:09:47
字體:
供稿:網(wǎng)友

跨界面?zhèn)髦?/span>

從一個界面將一個結(jié)果值傳到另一個界面,這個是我們在開發(fā)過程中非常常見的一個問題。傳值本身并不是一個太復(fù)雜的問題,在此主要簡述一下常用的傳值方法。

我們傳值常用的方法主要有四種:

  • 1.屬性傳值
  • 2.代理傳值
  • 3.block傳值
  • 4.通知傳值
  • 5.KVO
  • 對象傳值

屬性傳值:

屬性傳值應(yīng)該來說是比較簡單的一種傳值方式,但是這種傳值方式有其局限性,常用的一種場合是我們從界面A跳轉(zhuǎn)到界面B,如何我們想講界面A的值傳到界面B,屬性傳值是比較方便的一種方式。如下圖所示,如果我們點擊A界面上的一個按鈕,跳轉(zhuǎn)到B界面,并且把A界面的一個值傳送到B界面。

先說明一下大致的原理,首先要創(chuàng)建兩個控制器A和B,在A中導(dǎo)入B的頭文件,在A的按鈕點擊事件中,添加A跳轉(zhuǎn)到B的代碼段。現(xiàn)在的問題是如何在跳轉(zhuǎn)的過程中把A界面上的值傳到B界面呢?

我們可以給B添加一個屬性,在點擊按鈕從A跳轉(zhuǎn)到B的時候,將A界面要傳送的值賦給B的屬性,這樣在B界面可以使用(self.屬性)直接獲取從A界面?zhèn)鬟^來的值。

代碼段如下:(下面的代碼段是一個最簡單的演示)

 

1 #import <UIKit/UIKit.h>2 3 @interface ViewController : UIViewController4 {5     NSString *send;//我們在界面跳轉(zhuǎn)的時候,將send的值傳到下一個界面6 }7 - (IBAction)changeScreenButtonClick:(UIButton *)sender;8 @end
A.h
 1 #import "ViewController.h" 2 #import "BViewController.h" 3 @interface ViewController () 4  5 @end 6  7 @implementation ViewController 8  9 - (void)viewDidLoad10 {11     [super viewDidLoad];12     // Do any additional setup after loading the view, typically from a nib.13     send = @"傳值操作";14 }15 16 - (void)didReceiveMemoryWarning17 {18     [super didReceiveMemoryWarning];19     // Dispose of any resources that can be recreated.20 }21 22 - (IBAction)changeScreenButtonClick:(UIButton *)sender23 {24     BViewController *b = [[BViewController alloc]init];25     b.receiveValue = send;26     UIWindow *window = [UIapplication sharedApplication].delegate.window;27     window.rootViewController = b;28 }29 @end
A.m
1 #import <UIKit/UIKit.h>2 3 @interface BViewController : UIViewController4 @PRoperty(nonatomic,copy) NSString *receiveValue;//接收A界面?zhèn)鬟^來的值5 @end
B.h
 1 #import "BViewController.h" 2  3 @interface BViewController () 4  5 @end 6  7 @implementation BViewController 8  9 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil10 {11     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];12     if (self) {13         // Custom initialization14     }15     return self;16 }17 18 - (void)viewDidLoad19 {20     [super viewDidLoad];21     // Do any additional setup after loading the view from its nib.22     NSLog(@"從A界面?zhèn)鬟^來的值為:%@",self.receiveValue);23 }24 25 - (void)didReceiveMemoryWarning26 {27     [super didReceiveMemoryWarning];28     // Dispose of any resources that can be recreated.29 }30 31 @end
B.m

 

代理傳值:

代理傳值對應(yīng)初學(xué)者來說有一點難度,但是多使用幾次就好了,像在系統(tǒng)中我們代理這種設(shè)計使用的非常廣泛,在此主要說明使用代理傳值的方法。我們在頁面跳轉(zhuǎn)的過程中,將借助于導(dǎo)航,使用push從A界面跳轉(zhuǎn)到B界面,使用pop從B界面返回到A界面。

現(xiàn)在我們假設(shè)一種場景,我們需要從界面A傳值到界面B,同時也要從界面B傳值到界面A,如何使用代理來實現(xiàn)呢?

首先使用代理傳值,我們需要知道怎么自定義代理,首先先普及一下代理的相關(guān)知識。

假如我們需要在A界面?zhèn)髦档紹界面,我們需要在A界面中定義一些協(xié)議方法,只需要聲明方法即可,不需要實現(xiàn),如果其他類想要訪問這些協(xié)議方法,只需要遵守這些協(xié)議即可。在A中定義的協(xié)議方法,相當(dāng)于一個接口,你想使用A的接口,就要遵守A的協(xié)議方法,例如在B中,想要訪問A的協(xié)議方法,B就要遵守A的協(xié)議。傳值的話,B從A的協(xié)議方法中就能獲取到A界面中的值。

如何自己寫一個協(xié)議,下面是基本的格式:

 1 @protocol 協(xié)議名稱 <NSObject> 2 //協(xié)議方法 3 @end 

假設(shè)我們現(xiàn)在要在A界面中寫一個協(xié)議,具體代碼如下(傳值是從界面A傳到界面B):

 

 1 #import <UIKit/UIKit.h> 2   3 //協(xié)議 4 @protocol aScreenDelegate <NSObject> 5 -(void)sendValueFromScreenaTOScreenb:(NSString *)value; 6 @end 7  8 @interface ViewController : UIViewController 9 @property(nonatomic,assign)id<aScreenDelegate>delegate;10 - (IBAction)changeScreenButtonClick:(UIButton *)sender;11 @end
A.h
 1 #import "ViewController.h" 2 #import "BViewController.h" 3 @interface ViewController () 4  5 @end 6  7 @implementation ViewController 8  9 - (void)viewDidLoad10 {11     [super viewDidLoad];12     // Do any additional setup after loading the view, typically from a nib.13 }14 15 - (void)didReceiveMemoryWarning16 {17     [super didReceiveMemoryWarning];18     // Dispose of any resources that can be recreated.19 }20 21 - (IBAction)changeScreenButtonClick:(UIButton *)sender22 {23     BViewController *b = [[BViewController alloc]init];24     NSString *send = @"wyg";25     self.delegate = b;26     [_delegate sendValueFromScreenaTOScreenb:send];27     [self.navigationController pushViewController:b animated:YES];28 }29 @end
A.m
1 #import <UIKit/UIKit.h>2 #import "ViewController.h"3 @interface BViewController : UIViewController<aScreenDelegate>4 @end
B.h
 1 @implementation BViewController 2  3 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 4 { 5     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 6     if (self) { 7         // Custom initialization 8     } 9     return self;10 }11 -(void)sendValueFromScreenaTOScreenb:(NSString *)value12 {13     NSLog(@"從A界面?zhèn)鬟^來的值:%@",value);14 }15 - (void)viewDidLoad16 {17     [super viewDidLoad];18     // Do any additional setup after loading the view from its nib.19     20 }
B.m

 假如你向從界面B,pop到界面A,并把界面B的值傳到界面A,這種使用屬性傳值不太方便,使用代理可以解決,從B界面往A傳值,協(xié)議方法應(yīng)該在B中寫明,在A中遵守協(xié)議,下面是具體代碼:

1 #import <UIKit/UIKit.h>2 #import "BViewController.h"3 @interface ViewController : UIViewController<bScreenDelegate>4 - (IBAction)changeScreenButtonClick:(UIButton *)sender;5 @end
A.h
 1 #import "ViewController.h" 2   3 @interface ViewController () 4  5 @end 6  7 @implementation ViewController 8  9 - (void)viewDidLoad10 {11     [super viewDidLoad];12     // Do any additional setup after loading the view, typically from a nib.13 }14 //代理方法15 -(void)sendValueFromBtoA:(NSString *)str16 {17     NSLog(@"從B界面?zhèn)鬟^來的值為:%@",str);18 }19 - (IBAction)changeScreenButtonClick:(UIButton *)sender20 {21     BViewController *b = [[BViewController alloc]init];22     b.delegate = self;23     [self.navigationController pushViewController:b animated:YES];24 }25 @end
A.m
 1 #import <UIKit/UIKit.h> 2  3 @protocol bScreenDelegate <NSObject> 4 -(void)sendValueFromBtoA:(NSString *)str; 5 @end 6  7 @interface BViewController : UIViewController 8 @property(nonatomic,assign) id<bScreenDelegate>delegate; 9 - (IBAction)popButtonClick:(UIButton *)sender;10 @end
B.h
 1 #import "BViewController.h" 2  3 @interface BViewController () 4  5 @end 6  7 @implementation BViewController 8  9 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil10 {11     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];12     if (self) {13         // Custom initialization14     }15     return self;16 }17 18 - (void)viewDidLoad19 {20     [super viewDidLoad];21     // Do any additional setup after loading the view from its nib.22     23 }24 - (IBAction)popButtonClick:(UIButton *)sender25 {26     NSString *str = @"wxy";27     [_delegate sendValueFromBtoA:str];28     [self.navigationController popViewControllerAnimated:YES];29 }30 @end
B.m

 

block傳值

使用block傳值,我們需要自定義代理,這種寫法相對來說是比較麻煩的,使用block傳值的話,會使得代碼量大大縮減,現(xiàn)在我們假設(shè)我們要把界面A上的值傳到界面B,使用block來實現(xiàn)。

現(xiàn)在假設(shè)我們想在界面A上直接獲取界面B上的信息,如何獲取,代碼如下:

1 #import <UIKit/UIKit.h>2 #import "BViewController.h"3 @interface ViewController : UIViewController4 @end
A.h
 1 #import "ViewController.h" 2 #import "BViewController.h" 3 @interface ViewController () 4  5 @end 6 @implementation ViewController 7  8 - (void)viewDidLoad 9 {10     [super viewDidLoad];11     // Do any additional setup after loading the view, typically from a nib.12     BViewController *b = [[BViewController alloc]init];13     [b converyValueToA:^(NSString *str) {14         NSLog(@"B界面上的值為:%@",str);15     }];16 }17 @end
A.m
1 #import <UIKit/UIKit.h>2 @interface BViewController : UIViewController3 -(void)converyValueToA:(void(^)(NSString *))block;4 @end
B.h
 1 #import "BViewController.h" 2  3 @interface BViewController () 4  5 @end 6  7 @implementation BViewController 8  9 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil10 {11     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];12     if (self) {13         // Custom initialization14     }15     return self;16 }17 -(void)converyValueToA:(void (^)(NSString *))block18 {19     NSString *name = @"wxy";20     block(name);21 }22 - (void)viewDidLoad23 {24     [super viewDidLoad];25     // Do any additional setup after loading the view from its nib.26     27 }28 29 @end
B.m

 

通知傳值

通知傳值有點類似于廣播,有發(fā)送者,有監(jiān)聽者,比如A想接受B的值,B要發(fā)送一個通知,A只要監(jiān)聽這個通知,就能接收到值,通知傳值就不細述。

例如發(fā)送一個通知:

NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center postNotificationName:通知名字 object:self userInfo:傳遞參數(shù)];

接收一個通知:

NSNotificationCenter *center = [NSNotificationCenter defaultCenter];

[center addObserver:self selector:@selector(receiveNofitication:) name:通知名字 object:nil];

-(void)receiveNofitication:(NSNotification *)notification
{//接收到通知,執(zhí)行相關(guān)代碼}

 

跨應(yīng)用傳值

 

     跨頁面跳轉(zhuǎn),顧名思義就是在一個應(yīng)用內(nèi)不同界面之間的跳轉(zhuǎn),如果我們想要從一個應(yīng)用程序跳轉(zhuǎn)到另一個應(yīng)用程序怎么辦,加入你的手機上同時安裝了淘寶和支付寶兩個應(yīng)用程序,你點擊支付的時候,手機會自動打開手機上安裝的應(yīng)用支付寶,這個功能如何實現(xiàn)。
     想要跨應(yīng)用跳轉(zhuǎn),我們首先要確保手機上安裝了這兩個應(yīng)用,或者模擬器上安裝了這兩個應(yīng)用,然后點擊控件觸發(fā)事件實現(xiàn)不同應(yīng)用之間的跳轉(zhuǎn)。
     我們先要分析一下如何實現(xiàn)這個功能:
     如果我們從一個應(yīng)用跳轉(zhuǎn)到另一個應(yīng)用,我們需要有另一個應(yīng)用的標示,我們應(yīng)該稱之為URL,點擊響應(yīng)事件,跳轉(zhuǎn)到另一個應(yīng)用即可。
 
     如何給應(yīng)用設(shè)置URL:TAGGET->Info->URL Types->URL Schemes
     在里面輸入標示即可。
    
     現(xiàn)在假設(shè)我在模擬器上安裝了兩個應(yīng)用,一個叫BuyApp,設(shè)置標示為buy,一個叫PayApp,設(shè)置標示為pay.(下圖為BuyApp界面)
 
 
 
現(xiàn)在我需要點擊按鈕,跳轉(zhuǎn)到支付界面。(并且在跳轉(zhuǎn)的時候傳遞兩個參數(shù))
現(xiàn)在我們已經(jīng)知道支付界面的URL是pay.
我們可以在按鈕點擊事件中寫入下面的代碼即可:
 1 - (IBAction)toPayApp:(id)sender 2 { 3     //設(shè)置跳轉(zhuǎn)到應(yīng)用程序的鏈接 4     //雙斜杠之后是傳遞的參數(shù),多個參數(shù)使用&連接 5     NSURL *url = [NSURL URLWithString:@"payApp://name=iphone6&price=5288"]; 6     //跳轉(zhuǎn)前先判斷,是否可以打開鏈接 7     if ([[UIApplication sharedApplication] canOpenURL:url] == YES) 8     { 9         [[UIApplication sharedApplication] openURL:url];10     }11     else12     {13         NSLog(@"連接不能打開,應(yīng)用程序未安裝");14     }15 }
應(yīng)用跳轉(zhuǎn)

現(xiàn)在我們已經(jīng)進入到PayApp,如何在這個應(yīng)用中接受傳過來的參數(shù)呢?

我們可以在AppDelegate這個.m文件中添加下面的方法:

 1 - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation 

 當(dāng)前應(yīng)用程序被別的應(yīng)用程序喚醒時,執(zhí)行此方法。
參數(shù)分別是:
URL:程序跳轉(zhuǎn)的連接地址
sourceApplication:從哪個應(yīng)用程序跳轉(zhuǎn)過來。
我們可以從程序跳轉(zhuǎn)的連接地址中提取出傳遞過來的參數(shù)。這樣在第二個應(yīng)用中能夠獲取到第一個應(yīng)用傳遞過來的參數(shù)。
如果我們在PayApp界面執(zhí)行完畢后,返回到原程序,執(zhí)行方法與上面的步驟相同。
 
 
 
 
 
 

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 镇安县| 安塞县| 开远市| 南乐县| 抚州市| 邳州市| 上饶市| 长宁区| 和林格尔县| 乃东县| 皋兰县| 清河县| 庆云县| 韶山市| 云浮市| 宜章县| 剑川县| 临夏县| 西林县| 遂溪县| 开阳县| 新兴县| 辽阳县| 洛浦县| 清丰县| 灵武市| 上高县| 黑水县| 汉沽区| 上虞市| 弥渡县| 卓尼县| 河西区| 阳信县| 福泉市| 潮安县| 许昌市| 江油市| 长岭县| 思茅市| 青浦区|