iOS

来源:互联网 发布:2016东华理工行知分院 编辑:程序博客网 时间:2024/06/06 02:43

把代理写进一个类里面,这个类.h不声明,所以.m也就不用实现。既然不实现,那留着它也就无意义了,所以,我把它删了。

流程就是这样 : viewController-->FirstViewController-->SecondViewController,其中返回的过程中,Second 会 传值给 First, 而 First 则会给 ViewController 传值。依次看代码就好:

//

//  ZJJPassTheNumberDelegate.h

//  ProjectZJJ

//

//  Created by ZJJ on 2017/8/4.

//  Copyright © 2017 ZJJ. All rights reserved.

//


#import <Foundation/Foundation.h>


@protocol ZJJPassTheNumberDelegate <NSObject>


- (void)passTheNumberWithNum:(int)num;


@end


//

//  ViewController.h

//  ProjectZJJ

//

//  Created by ZJJ on 2017/8/3.

//  Copyright © 2017 ZJJ. All rights reserved.

//


#import <UIKit/UIKit.h>


@interface ViewController : UIViewController



@end

//  Created by ZJJ on 2017/8/3.

//  Copyright © 2017 ZJJ. All rights reserved.

//


#import "ViewController.h"

#import "FirstViewController.h"



@interface ViewController () <ZJJPassTheNumberDelegate>


@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

   

    self.view.backgroundColor = [UIColorcyanColor];

    

}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    

    FirstViewController *first = [[FirstViewControlleralloc] init];

    first.delegate =self;

    [selfpresentViewController:firstanimated:YEScompletion:nil];

    

}


- (void)passTheNumberWithNum:(int)num {

    

    NSLog(@"第一个类传过来的值为:%d",num);

    

}



- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}



@end

//

//  FirstViewController.h

//  ProjectZJJ

//

//  Created by ZJJ on 2017/8/4.

//  Copyright © 2017 ZJJ. All rights reserved.

//


#import <UIKit/UIKit.h>

#import "ZJJPassTheNumberDelegate.h"


@interface FirstViewController :UIViewController


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


@end


//

//  FirstViewController.m

//  ProjectZJJ

//

//  Created by ZJJ on 2017/8/4.

//  Copyright © 2017 ZJJ. All rights reserved.

//


#import "FirstViewController.h"

#import "SecondViewController.h"


@interface FirstViewController () <ZJJPassTheNumberDelegate>


@end


@implementation FirstViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view.

    

    self.view.backgroundColor = [UIColorwhiteColor];

    

    UIButton *button = [UIButtonbuttonWithType:UIButtonTypeSystem];

    button.frame =CGRectMake(100,100, 100,50);

    button.backgroundColor = [UIColorcyanColor];

    [button setTitle:@"返回"forState:UIControlStateNormal];

    [button setTitleColor:[UIColoryellowColor] forState:UIControlStateNormal];

    [button addTarget:selfaction:@selector(back)forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:button];


    

}


- (void)back {

    

    if ([self.delegaterespondsToSelector:@selector(passTheNumberWithNum:)]) {

        [self.delegatepassTheNumberWithNum:10];

    }

    

    [selfdismissViewControllerAnimated:YEScompletion:nil];

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    

    SecondViewController *second = [[SecondViewControlleralloc] init];

    second.protocal =self;

    [selfpresentViewController:secondanimated:YEScompletion:nil];

    

    

}


- (void)passTheNumberWithNum:(int)num {

    

    NSLog(@"第二个类传过来的值为:%d",num);

}

- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


//

//  SecondViewController.h

//  ProjectZJJ

//

//  Created by ZJJ on 2017/8/4.

//  Copyright © 2017 ZJJ. All rights reserved.

//


#import <UIKit/UIKit.h>

#import "ZJJPassTheNumberDelegate.h"


@interface SecondViewController :UIViewController


@property (nonatomic,assign) id <ZJJPassTheNumberDelegate> protocal;


@end


//

//  SecondViewController.m

//  ProjectZJJ

//

//  Created by ZJJ on 2017/8/4.

//  Copyright © 2017 ZJJ. All rights reserved.

//


#import "SecondViewController.h"


@interface SecondViewController ()


@end


@implementation SecondViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view.

    

    self.view.backgroundColor = [UIColorlightGrayColor];

}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    

    if ([self.protocalrespondsToSelector:@selector(passTheNumberWithNum:)]) {

        

        [self.protocalpassTheNumberWithNum:100];

        

    }

    

    [selfdismissViewControllerAnimated:YEScompletion:nil];

}


- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end




原创粉丝点击