ios界面傳值方式

界面跳轉傳值

順向傳值

  1. 在目標的.h文件中,定義一個屬性
	// 定義一個屬性,正向傳值
	@property (strong, nonatomic) NSString* passedValue;
  1. 直接用代碼,在目標的.h文件中,定義數據變量
    // 界面傳值
    RedViewController* vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"aaa"];
    vc.passedValue = @"11111";
    [self presentViewController:vc animated:YES completion:nil];
  1. 使用id時候
// 重寫這個方法,獲取目標ViewController,完成真正的傳值
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    RedViewController* vc = segue.destinationViewController;
    vc.passedValue = @"1aaaaa";
}

逆向傳值,代理和block有點像回調

  1. 使用代理方式(delegate)
    1. 申明代理
@protocol passValueProtocol <NSObject>

    1. // 定義屬性
@property (assign, nonatomic) id<passValueProtocol> delegate;

    1. 傳值
[self.delegate passValue:self.content.text];

  1. 使用block來傳值
    1. // 定義一個block
typedef void (^ PassValueBlock)(NSString* info);

    1. // 申明屬性
@property (copy, nonatomic) PassValueBlock passValueBlock;

    1. // 傳值
    if (self.passValueBlock) {
        self.passValueBlock(self.content.text);
    }
  1. 通知傳值
    1. 註冊通知
    // 註冊監聽
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handlerNoti:) name:@"getValue" object:nil];
    1. 發送通知(很少使用)
    //監聽傳值
    [[NSNotificationCenter defaultCenter]postNotificationName:@"getValue" object:nil userInfo:@{@"info": self.content.text}];

代碼展示

B.h文件

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

// 定義一個block
typedef void (^ PassValueBlock)(NSString* info);
// 定義協議
@protocol passValueProtocol <NSObject>
// 定義一個代理方法
-(void) passValue: (NSString*) value;

@end

@interface RedViewController : UIViewController

// 正向傳值
// 定義一個屬性,正向傳值
@property (strong, nonatomic) NSString* passedValue;

// 逆向傳值
// 定義代理屬性
@property (assign, nonatomic) id<passValueProtocol> delegate;

// 定義block屬性
@property (copy, nonatomic) PassValueBlock passValueBlock;

@end

NS_ASSUME_NONNULL_END

.m文件

#import "RedViewController.h"

@interface RedViewController ()
// 一個lable用戶顯示正向傳值
@property (weak, nonatomic) IBOutlet UILabel *lable;
// 一個輸入框用於逆向傳值
@property (weak, nonatomic) IBOutlet UITextField *content;

@end

@implementation RedViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.lable.text = self.passedValue;
    // Do any additional setup after loading the view.
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self dismissViewControllerAnimated:YES completion:nil];
}

// 點擊按鈕逆向傳值
- (IBAction)clickValue:(id)sender {
    // 代理傳值
    [self.delegate passValue:self.content.text];
    
    // 使用block
    if (self.passValueBlock) {
        self.passValueBlock(self.content.text);
    }
    //監聽傳值
    [[NSNotificationCenter defaultCenter]postNotificationName:@"getValue" object:nil userInfo:@{@"info": self.content.text}];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

A.m

- (void)viewDidLoad {
    [super viewDidLoad]; 
    // 註冊監聽
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handlerNoti:) name:@"getValue" object:nil];
}

-(void) handlerNoti: (NSNotification*) not {
    NSDictionary* userInfo = not.userInfo;
    NSLog(@"獲取通知傳值%@", userInfo[@"info"]);
}

- (IBAction)clickToUIViewController:(id)sender {
    // 界面傳值
    RedViewController* vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"aaa"];
    // 正向傳值
    vc.passedValue = @"11111";
    // 代理逆向傳值
    vc.delegate = self;
    // block逆向傳值
    vc.passValueBlock = ^(NSString * _Nonnull info) {
        NSLog(@"block獲取值爲%@", info);
    };
    [self presentViewController:vc animated:YES completion:nil];
}

// 重寫這個方法,獲取目標ViewController,實現真正的傳值
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    RedViewController* vc = segue.destinationViewController;
    // 正向傳值,通過segua連線
    vc.passedValue = @"1aaaaa";
}

// segua跳轉
- (IBAction)clickSegua:(id)sender {
    [self performSegueWithIdentifier:@"abcd" sender:nil];
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章