日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

iOS之界面传值(通知,属性,协议,NSUserDefaults,KVC)

發布時間:2024/5/17 c/c++ 91 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS之界面传值(通知,属性,协议,NSUserDefaults,KVC) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

通知傳值
通知是在跳轉控制器之間常用的傳值代理方式。NSNotificationCenter提供了一種解耦的方式,就是任何對象都可以發送通知到中心,同時任何對象可以監聽中心的通知。

  • 發送通知(傳值頁面)
//通知中心NSNotificationCenter,發送通知- (IBAction)changeColorAction2:(id)sender {UIColor *color = [UIColor greenColor];[[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeColorKey" object:color];[[self navigationController] popViewControllerAnimated:YES]; }
  • 注冊通知(取值頁面)
//注冊通知[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeColor:) name:@"ChangeColorKey" object:nil];
  • 響應接收通知
//響應通知中心NSNotificationCenter- (void)changeColor:(NSNotification *)notification{if([notification object] != nil){UIColor *color = [notification object];[self.view setBackgroundColor:color];} }
  • 移除通知
- (void)dealloc {//移除通知[[NSNotificationCenter defaultCenter] removeObserver:self name:@"ChangeColorKey" object:nil]; }

注意:注意參數notification Observer為要刪除的觀察者,一定不能置為nil。

協議傳值
A頁面push到B頁面,如果B頁面的信息想回傳(回調)到A頁面,就用代理傳值,其中B定義協議和聲明代理,A確認并實現代理,A作為B的代理。

  • 在需要傳值給其他類的類頭文件中定義一個協議
@protocol ViewControllerAdelegate-(void)changeBgColorFromCtrlA:(ViewControllerA *)aView withColor:(UIColor *)color;@end
  • 在該類中聲明一個代理屬性
@interface ViewControllerA : UIViewController@property( assign, nonatomic ) id< ViewControllerAdelegate > delegate;@end
  • 在.m中實現
@implementation ViewControllerA @synthesize delegate;
  • 在需要觸發傳值的方法中調用協議中的方法
//使用協議代理Delegate -(IBAction)changeColorAction:(id)sender{[delegate changeBgColorFromCtrlA:self withColor:[UIColor grayColor]];[[self navigationController] popViewControllerAnimated:YES]; }
  • 在傳值給的類中的.h文件中引用該協議
@interface ViewControllerB : UIViewController<ViewControllerAdelegate>@end
  • 在.m中
ViewControllerA *_ViewControllerA = [stryBoard instantiateViewControllerWithIdentifier:@"ViewControllerA"];_ViewControllerA.delegate = self;
  • 然后實現該方法
//響應協議代理Delegate -(void)changeBgColorFromCtrlA:(ViewControllerA *)aView withColor:(UIColor *)color{[self.view setBackgroundColor:color]; }

順便附上個人寫的”通知傳值“和”協議傳值“的全部demo

  • ViewControllerA的.h文件
// Created by Ydw on 16-03-17. // Copyright (c) 2016年 com. All rights reserved. //#import <UIKit/UIKit.h> @class ViewControllerA;@protocol ViewControllerAdelegate-(void)changeBgColorFromCtrlA:(ViewControllerA *)aView withColor:(UIColor *)color;@end@interface ViewControllerA : UIViewController@property( assign, nonatomic ) id< ViewControllerAdelegate > delegate;-(IBAction)changeColorAction:(id)sender; -(IBAction)changeColorAction2:(id)sender;@end
  • ViewControllerA的.m文件
// Created by Ydw on 16-03-17. // Copyright (c) 2016年 com. All rights reserved. //#import "ViewControllerA.h" #import "ViewControllerB.h" @interface ViewControllerA ()@end@implementation ViewControllerA @synthesize delegate;- (void)viewDidLoad {[super viewDidLoad];self.title = @"Ctrl A"; }//使用協議代理Delegate - (IBAction)changeColorAction:(id)sender {[delegate changeBgColorFromCtrlA:self withColor:[UIColor grayColor]];[[self navigationController] popViewControllerAnimated:YES]; }//通知中心NSNotificationCenter,發送通知 - (IBAction)changeColorAction2:(id)sender {UIColor *color = [UIColor greenColor];[[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeColorKey" object:color];[[self navigationController] popViewControllerAnimated:YES]; }- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated. }@end
  • ViewControllerB的.h文件
// Created by Ydw on 16-03-17. // Copyright (c) 2016年 com. All rights reserved. //#import <UIKit/UIKit.h> #import "ViewControllerA.h" @interface ViewControllerB : UIViewController<ViewControllerAdelegate>@end
  • ViewControllerB的.m文件
// Created by Ydw on 16-03-17. // Copyright (c) 2016年 com. All rights reserved. //#import "ViewControllerB.h"@implementation ViewControllerB- (void)viewDidLoad {[super viewDidLoad];self.title = @"Ctrl B";self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Go CtrlA" style:UIBarButtonItemStylePlain target:self action:@selector(go)];//注冊通知[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeColor:) name:@"ChangeColorKey" object:nil]; }-(void)go {UIStoryboard *stryBoard=[UIStoryboard storyboardWithName:@"Main" bundle:nil];ViewControllerA *_ViewControllerA = [stryBoard instantiateViewControllerWithIdentifier:@"ViewControllerA"];_ViewControllerA.delegate = self;[[self navigationController] pushViewController:_ViewControllerA animated:YES];_ViewControllerA = nil;}//響應協議代理Delegate -(void)changeBgColorFromCtrlA:(ViewControllerA *)aView withColor:(UIColor *)color{[self.view setBackgroundColor:color]; }//響應通知中心NSNotificationCenter - (void)changeColor:(NSNotification *)notification{if([notification object] != nil){UIColor *color = [notification object];[self.view setBackgroundColor:color];} }- (void)dealloc{//移除通知[[NSNotificationCenter defaultCenter] removeObserver:self name:@"ChangeColorKey" object:nil]; }- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated. }@end

屬性傳值
屬性傳值是將A頁面所擁有的信息通過屬性傳遞到B頁面使用。B頁面定義了一個屬性,在A頁面中直接通過屬性賦值將A頁面中的值傳到B頁面。(相對而言,最簡單一種的傳值,就不具體說明了)

  • 首先在SecondViewController視圖中(即B頁面)需要有一個屬性用來存儲傳遞過來的值:
@property(nonatomic,retain) NSString *firstValue;//屬性傳值
  • 然后MainViewController視圖(即A頁面)需要引用SecondViewController視圖的頭文件,在視圖中的按鈕點擊事件中,通過SecondViewController的對象將需要傳遞的值存在firstValue中:
- (void)buttonAction:(UIButton *)button {SecondViewController *second = [[SecondViewController alloc]init];second.firstValue = _txtFiled.text;[self.navigationController pushViewController:second animated:YES]; }
  • 頁面跳轉之后,就能在SecondViewController視圖中,通過存值的屬性,取用剛才傳遞過來的值:
[_txtFiled setText:_firstValue];//顯示傳過來的值,firstValue保存傳過來的值

NSUserDefaults
1、NSUserDefaults記錄本地一些輕量級的數據,是單例類。其通過userDefaults對象來將數據保存到NSUserDefaults的plist文件中,這個文件實際上是一個plist文件,在沙盒中的/Library/Preferences
/NSUserDefaults.plist 這個位置中。
2、NSUserDefaults支持的數據格式有:NSNumber(Integer、Float、Double),NSString,NSDate,NSArray,NSDictionary,BOOL類型。
3、NSUserDefaults是本地數據持久化的一種,可以投機取巧地進行數據傳值,通過給NSUserDefaults設置一個DefaultsKey,就可以在任何頁面通過DefaultsKey來取到值。簡單的舉例說明一下:

  • NSUserDefaults存值
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];NSMutableArray *rootArray = nil;if ([defaults objectForKey:DefaultsKey]) {rootArray = [NSMutableArray arrayWithArray:[defaults objectForKey:DefaultsKey]];}else {rootArray = [NSMutableArray array];}NSDictionary *infoDictionary = @{NameKey:@"Ydw",PhoneKey: @"1314520"};[rootArray addObject:infoDict];[defaults setObject:rootArray forKey:DefaultsKey];[defaults synchronize];
  • NSUserDefaults取值
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];NSArray *rootArray = [defaults objectForKey:DefaultsKey];if (rootArray && rootArray.count > 0) {NSLog(@"%@", rootArray);}else {NSLog(@"讀取數據失敗!");}
  • NSUserDefaults刪除數據
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];[defaults removeObjectForKey:DefaultsKey];[defaults synchronize];NSLog(@"數據刪除成功!");

KVC
KVC是Key Value Coding的縮寫,即是鍵值編碼。在iOS中,提供了一種方法通過使用屬性的名稱(也就是Key)來間接訪問對象的屬性方法。實際上,就是通過類定義我們可以看到類的各種屬性,那么使用屬性的名稱就能訪問到類實例化后的對象的這個屬性值。這個方法可以不通過getter/setter方法來訪問對象的屬性。
NSArray/NSSet等都支持KVC。

  • 定義一個myInformation的類
@interface myInformation : NSObject { NSString *_name; int _age; int _height; int _weight; } @end
  • 聲明屬性對象
@interface myViewController : UIViewController @property (nonatomic, retain) myInformation *information; @end
  • 使用KVC讀取和改變類中的屬性的值
- (void)useKVC { information = [[myInformation alloc] init]; NSLog(@"information's init height = %@", [information valueForKey:@"height"]); [information setValue:[NSNumber numberWithInt:168] forKey:@"height"]; NSLog(@"information's height = %@", [information valueForKey:@"height"]); }
  • KVC的常用方法
- (id)valueForKey:(NSString *)key; - (void)setValue:(id)value forKey:(NSString *)key;

總結

以上是生活随笔為你收集整理的iOS之界面传值(通知,属性,协议,NSUserDefaults,KVC)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。