day 023 再谈代理Delegate

两个界面,一个界面输入数字,并提供跳转到另一个界面的Button;另一个界面提供运算符,并计算运算结果,利用一个Button按钮跳转到第一个界面,并将运算的结果导入到第一个界面中

//
//  main.m
//  DelegateDemo
//
//  Created by PXD on 15-4-16.
//  Copyright (c) 2015ๅนด PXD. All rights reserved.
//


#import <UIKit/UIKit.h>
#import "AppDelegate.h"


int main(int argc, char * argv[]) {
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}


//
//  ViewController.h
//  DelegateDemo
//
//  Created by PXD on 15-4-16.
//  Copyright (c) 2015ๅนด PXD. All rights reserved.
//


#import <UIKit/UIKit.h>
#import "CalculatorViewController.h"


@interface ViewController : UIViewController <CalculatorDelegate>




@end

//
//  ViewController.m
//  DelegateDemo
//
//  Created by PXD on 15-4-16.
//  Copyright (c) 2015年 PXD. All rights reserved.
//


#import "ViewController.h"
#import "CalculatorViewController.h"
#import "Constants.h"


@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *firstArgTextField;
@property (weak, nonatomic) IBOutlet UITextField *secondArgTextField;
@property (weak, nonatomic) IBOutlet UILabel *resultsLabel;
@end


@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)calculate:(id)sender {
    //创建计算的界面
    CalculatorViewController *cvc = [[CalculatorViewController alloc] initWithNibName:@"CalculatorViewController" bundle:nil firstParam:[_firstArgTextField.text integerValue] secondParam:[_secondArgTextField.text integerValue]];
    
    //设置代理为本身 代理里面的方法在当前这个类里面实现
    cvc.delegate = self;
    
    //push
    [self.navigationController pushViewController:cvc animated:YES];
}


#pragma mark -- CalculatorDelegate
- (void)calculationDidFinished:(NSInteger)result andOperation:(kOperationType)type{
    NSString *operationString = (type == kOperationTypeAdd) ? @"+" : @"-";
    //将结果显示到label上
    self.resultsLabel.text = [NSString stringWithFormat:@"%@ %@ %@ = %ld", _firstArgTextField.text , operationString,  _secondArgTextField.text , result];
}




@end

//
//  CalculatorViewController.h
//  DelegateDemo
//
//  Created by PXD on 15-4-16.
//  Copyright (c) 2015年 PXD. All rights reserved.
//


#import <UIKit/UIKit.h>
#import "Constants.h"


@protocol CalculatorDelegate <NSObject>
- (void)calculationDidFinished:(NSInteger)result andOperation:(kOperationType)type;
@end


@interface CalculatorViewController : UIViewController


@property (nonatomic, assign) id<CalculatorDelegate> delegate;


//自定义一个init方法
//[[aa alloc] init]
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil firstParam:(NSInteger)first secondParam:(NSInteger)second;


@end

//
//  CalculatorViewController.m
//  DelegateDemo
//
//  Created by PXD on 15-4-16.
//  Copyright (c) 2015年 PXD. All rights reserved.
//


#import "CalculatorViewController.h"


@interface CalculatorViewController ()
@property (nonatomic, assign) NSInteger firstParam;
@property (nonatomic, assign) NSInteger secondParam;
@end


@implementation CalculatorViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil firstParam:(NSInteger)first secondParam:(NSInteger)second{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.firstParam = first;
        self.secondParam = second;
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)addButtonDidClicked:(id)sender {
    NSInteger result = _firstParam + _secondParam;
    
    //在返回到上一个界面之前 必须先吧结果返回去
    //当返回到上一个界面之后,这个界面将会被删除
    if ([_delegate respondsToSelector:@selector(calculationDidFinished:andOperation:)]){
        [_delegate calculationDidFinished:result andOperation:kOperationTypeAdd];
    }
    
    //返回上一个界面
    [self.navigationController popViewControllerAnimated:YES];
}


- (IBAction)minusButtonDidClicked:(id)sender {
    NSInteger result = _firstParam - _secondParam;
    
    //在返回到上一个界面之前 必须先吧结果返回去
    //当返回到上一个界面之后,这个界面将会被删除
    if ([_delegate respondsToSelector:@selector(calculationDidFinished:andOperation:)]){
        [_delegate calculationDidFinished:result andOperation:kOperationTypeMinus];
    }
    
    //返回上一个界面
    [self.navigationController popViewControllerAnimated:YES];
}


@end


//
//  Constants.h
//  DelegateDemo
//
//  Created by PXD on 15-4-16.
//  Copyright (c) 2015ๅนด PXD. All rights reserved.
//


#ifndef DelegateDemo_Constants_h
#define DelegateDemo_Constants_h


typedef enum{
    kOperationTypeAdd,
    kOperationTypeMinus
}kOperationType;


#endif



Delegate小结

1.delegate作为代理,可以实现传值的方法

 2. delegate的位置在中介位置的h文件声明中,如该程序的CalculatorUiewController中,接收前一个控制器的数据,并对数据进行处理,最后将数据传回

      特别的,对该h文件中的类方法中,需建立一个实现该代理方法的id

 3.任何一个需要调用delegate的控制器均需要导入该代理方法

4. 在应用代理方法的时候,需引入该语句设置代理为本身 
    cvc.delegate = self;表示代理里面的方法在当前这个类里面实现

5.该界面利用storybord搭建


最后,对于self方法的应用还不是很熟悉,将在最近予以总结

对于私人相册项目同样有用到Delegate方法,将整理后补充在后面

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值