iOS開發系列----UI(視圖編程入門:Delegate、Block、單例、屬性傳值)

本章介紹三種逆向傳值方式(Delegate、Block、單例)、一種正向傳值方式(屬性)

Delegate傳值:
Delegate

核心代碼:
SecondViewController.h

#import <UIKit/UIKit.h>

//<1>聲明協議
@protocol SecondVCDelegate <NSObject>

- (void)changeText:(NSString *)text;

@end

@interface SecondViewController : UIViewController

//<2>聲明代理
@property (nonatomic, assign) id<SecondVCDelegate>delegate;

@end

SecondViewController.m

#import "SecondViewController.h"

@interface SecondViewController ()<
UITextFieldDelegate>

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor orangeColor];
    [self createTextField];
}

- (void)createTextField {
    UITextField * tf = [[UITextField alloc] initWithFrame:CGRectMake(50, 100, self.view.bounds.size.width-100, 30)];
    tf.placeholder = @"請輸入文字";
    tf.delegate = self;
    [self.view addSubview:tf];
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    //<5>調用代理
    if (self.delegate && [self.delegate respondsToSelector:@selector(changeText:)]) {
        [self.delegate changeText:textField.text];
    }
    [self dismissViewControllerAnimated:YES completion:nil];
    return YES;
}

@end

ViewController.m

#import "ViewController.h"
#import "SecondViewController.h"
#import "HTTools.h"

@interface ViewController ()<
UITextViewDelegate,
SecondVCDelegate>

@end

@implementation ViewController {
    UITextView * _tv;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self createTextView];
    [self createButton];
}

- (void)createTextView {
    _tv = [[UITextView alloc] initWithFrame:CGRectMake(100, 200, self.view.bounds.size.width-200, 200)];
    _tv.backgroundColor = [UIColor cyanColor];
    _tv.delegate = self;
    [self.view addSubview:_tv];
}

- (void)createButton {
    UIButton * button = [HTTools createButton:CGRectMake(100, 100, self.view.bounds.size.width-200, 100) bgColor:nil title:@"傳值" titleColor:[UIColor blackColor] tag:101 action:@selector(buttonClick:) vc:self];
    [self.view addSubview:button];
}

- (void)buttonClick:(UIButton *)button {
    SecondViewController * secondVC = [[SecondViewController alloc] init];
    //<3>設置代理
    secondVC.delegate = self;
    [self presentViewController:secondVC animated:YES completion:nil];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [_tv resignFirstResponder];
}

#pragma mark - UITextViewDelegate
//開始編輯狀態
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
    [UIView animateWithDuration:0.5 animations:^{
        //相對現在的位置向上移動100
        textView.transform = CGAffineTransformMakeTranslation(0, 50);
    } completion:nil];

    return YES;
}

//結束編輯狀態
- (BOOL)textViewShouldEndEditing:(UITextView *)textView {
    [UIView animateWithDuration:0.5 animations:^{
        //返回原始位置
        textView.transform = CGAffineTransformMakeTranslation(0, 0);
    } completion:nil];
    return YES;
}

//打字的時候就會調用
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    NSLog(@"%@",text);
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
    }
    return YES;
}

#pragma mark - SecondVCDelegate
//<4>實現自定義方法
- (void)changeText:(NSString *)text {
    _tv.text = [NSString stringWithFormat:@"%@---%@",_tv.text,text];
}

@end

Block傳值
Block

SecondViewController.h

#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController

//<1>聲明block
@property (nonatomic, copy) void(^block)(NSString *);

@end

SecondViewController.m

#import "SecondViewController.h"

@interface SecondViewController ()<
UITextFieldDelegate>

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor orangeColor];
    [self createTextField];
}

- (void)createTextField {
    UITextField * tf = [[UITextField alloc] initWithFrame:CGRectMake(50, 100, 250, 30)];
    tf.placeholder = @"請輸入文字";
    tf.delegate = self;
    [self.view addSubview:tf];
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    //<3>調用block
    _block(textField.text);
    [self dismissViewControllerAnimated:YES completion:nil];
    return YES;
}

@end

ViewController.m

#import "ViewController.h"
#import "SecondViewController.h"
#import "HTTools.h"

@interface ViewController ()<
UITextViewDelegate>

@end

@implementation ViewController {
    UITextView * _tv;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self createTextView];
    [self createButton];
}

- (void)createTextView {
    _tv = [[UITextView alloc] initWithFrame:CGRectMake(100, 200, self.view.bounds.size.width-200, 200)];
    _tv.backgroundColor = [UIColor cyanColor];
    _tv.delegate = self;
    [self.view addSubview:_tv];
}

- (void)createButton {
    UIButton * button = [HTTools createButton:CGRectMake(100, 100, self.view.bounds.size.width-200, 100) bgColor:nil title:@"傳值" titleColor:[UIColor blackColor] tag:101 action:@selector(buttonClick:) vc:self];
    [self.view addSubview:button];
}

- (void)buttonClick:(UIButton *)button {
    SecondViewController * secondVC = [[SecondViewController alloc] init];
    //<2>實現block
    secondVC.block = ^(NSString * text){
        _tv.text = text;
    };
    [self presentViewController:secondVC animated:YES completion:nil];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [_tv resignFirstResponder];
}

#pragma mark - UITextViewDelegate
//開始編輯狀態
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
    [UIView animateWithDuration:0.5 animations:^{
        //相對現在的位置向上移動100
        textView.transform = CGAffineTransformMakeTranslation(0, 50);
    } completion:nil];
    return YES;
}

//結束編輯狀態
- (BOOL)textViewShouldEndEditing:(UITextView *)textView {
    [UIView animateWithDuration:0.5 animations:^{
        //返回原始位置
        textView.transform = CGAffineTransformMakeTranslation(0, 0);
    } completion:nil];
    return YES;
}

//打字的時候就會調用
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    NSLog(@"%@",text);
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
    }
    return YES;
}

@end

單例傳值
Singleton

SecondViewController.m

#import "SecondViewController.h"
#import "HTTools.h"

@interface SecondViewController ()<
UITextFieldDelegate>

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor orangeColor];
    [self createTextField];
}

- (void)createTextField {
    UITextField * tf = [[UITextField alloc] initWithFrame:CGRectMake(50, 100, 250, 30)];
    tf.placeholder = @"請輸入文字";
    tf.delegate = self;
    [self.view addSubview:tf];
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    //單例傳值
    [HTTools shareInstanced].text = textField.text;
    [self dismissViewControllerAnimated:YES completion:nil];
    return YES;
}

@end

ViewController.m

#import "ViewController.h"
#import "SecondViewController.h"
#import "HTTools.h"

@interface ViewController ()<
UITextViewDelegate>

@end

@implementation ViewController {
    UITextView * _tv;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self createTextView];
    [self createButton];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if ([HTTools shareInstanced].text) {
        _tv.text = [HTTools shareInstanced].text;
    }
}

- (void)createTextView {
    _tv = [[UITextView alloc] initWithFrame:CGRectMake(100, 200, self.view.bounds.size.width-200, 200)];
    _tv.backgroundColor = [UIColor cyanColor];
    _tv.delegate = self;
    [self.view addSubview:_tv];
}

- (void)createButton {
    UIButton * button = [HTTools createButton:CGRectMake(100, 100, self.view.bounds.size.width-200, 100) bgColor:nil title:@"傳值" titleColor:[UIColor blackColor] tag:101 action:@selector(buttonClick:) vc:self];
    [self.view addSubview:button];
}

- (void)buttonClick:(UIButton *)button {
    SecondViewController * secondVC = [[SecondViewController alloc] init];
    [self presentViewController:secondVC animated:YES completion:nil];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [_tv resignFirstResponder];
}

#pragma mark - UITextViewDelegate
//開始編輯狀態
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
    [UIView animateWithDuration:0.5 animations:^{
        //相對現在的位置向上移動100
        textView.transform = CGAffineTransformMakeTranslation(0, 50);
    } completion:nil];

    return YES;
}

//結束編輯狀態
- (BOOL)textViewShouldEndEditing:(UITextView *)textView {
    [UIView animateWithDuration:0.5 animations:^{
        //返回原始位置
        textView.transform = CGAffineTransformMakeTranslation(0, 0);
    } completion:nil];
    return YES;
}

//打字的時候就會調用
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    NSLog(@"%@",text);
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
    }
    return YES;
}

@end

屬性傳值
屬性

核心代碼:
SecondViewController.h

#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController

@property (nonatomic, copy) NSString *text;

@end

SecondViewController.m

#import "SecondViewController.h"

@interface SecondViewController ()<
UITextFieldDelegate>

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor orangeColor];
    [self createTextField];
}

- (void)createTextField {
    UITextField * tf = [[UITextField alloc] initWithFrame:CGRectMake(50, 100, 250, 30)];
    tf.placeholder = @"請輸入文字";
    tf.text = self.text;
    tf.delegate = self;
    [self.view addSubview:tf];
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [self dismissViewControllerAnimated:YES completion:nil];
    return YES;
}

@end

ViewController.m

#import "ViewController.h"
#import "SecondViewController.h"
#import "HTTools.h"

@interface ViewController ()<
UITextViewDelegate>

@end

@implementation ViewController {
    UITextView * _tv;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self createTextView];
    [self createButton];
}

- (void)createTextView {
    _tv = [[UITextView alloc] initWithFrame:CGRectMake(100, 200, self.view.bounds.size.width-200, 200)];
    _tv.backgroundColor = [UIColor cyanColor];
    _tv.delegate = self;
    [self.view addSubview:_tv];
}

- (void)createButton {
    UIButton * button = [HTTools createButton:CGRectMake(100, 100, self.view.bounds.size.width-200, 100) bgColor:nil title:@"傳值" titleColor:[UIColor blackColor] tag:101 action:@selector(buttonClick:) vc:self];
    [self.view addSubview:button];
}

- (void)buttonClick:(UIButton *)button {
    SecondViewController * secondVC = [[SecondViewController alloc] init];
    secondVC.text = _tv.text;
    [self presentViewController:secondVC animated:YES completion:nil];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [_tv resignFirstResponder];
}

#pragma mark - UITextViewDelegate
//開始編輯狀態
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
    [UIView animateWithDuration:0.5 animations:^{
        //相對現在的位置向上移動100
        textView.transform = CGAffineTransformMakeTranslation(0, 50);
    } completion:nil];
    return YES;
}

//結束編輯狀態
- (BOOL)textViewShouldEndEditing:(UITextView *)textView {
    [UIView animateWithDuration:0.5 animations:^{
        //返回原始位置
        textView.transform = CGAffineTransformMakeTranslation(0, 0);
    } completion:nil];
    return YES;
}

//打字的時候就會調用
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    NSLog(@"%@",text);
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
    }
    return YES;
}

@end

demo地址

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章