Delegate

来源:互联网 发布:mac os x 10.11cdr 编辑:程序博客网 时间:2024/05/19 16:35

ViewController:

#import <UIKit/UIKit.h>

#import "SecondViewController.h"


@interface ViewController : UIViewController<PassValueDelegate>


@end


#import "ViewController.h"

#import "Header.h"

#import "SecondViewController.h"



@interface ViewController ()

@property(nonatomic,strong)UILabel *firstLabel;

@property(nonatomic,strong)UIButton *firstButton;

@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    self.view.backgroundColor = [UIColorwhiteColor];

    [self.viewaddSubview:self.firstLabel];

    [self.viewaddSubview:self.firstButton];

}


-(UILabel *)firstLabel{

    if (!_firstLabel) {

        _firstLabel = [[UILabelalloc]initWithFrame:CGRectMake(50,100,kScreen_Width-100 ,60)];

        _firstLabel.backgroundColor = [UIColoryellowColor];

    }

    return_firstLabel;

}


-(UIButton *)firstButton{

    if (!_firstButton) {

        _firstButton = [UIButtonbuttonWithType:UIButtonTypeCustom];

        _firstButton.frame =CGRectMake(100,200,kScreen_Width-200,60);

        _firstButton.backgroundColor = [UIColorredColor];

        [_firstButtonaddTarget:selfaction:@selector(pushNext:)forControlEvents:UIControlEventTouchUpInside];

    }

    return_firstButton;

}




-(void)pushNext:(UIButton *)sender{

    NSLog(@"走起");

    

    SecondViewController *secondVC = [[SecondViewControlleralloc]init];

    secondVC.delegate = self;

    

    [self.navigationControllerpushViewController:secondVCanimated:YES];

    

    //[self presentViewController:secondVC animated:YES completion:nil];

}


-(void)passValue:(NSString *)valueReturn{

    self.firstLabel.text = valueReturn;

}



- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end


SecondViewController:

#import <UIKit/UIKit.h>


@protocol PassValueDelegate <NSObject>


@required

-(void)passValue:(NSString *)valueReturn;


@end


@interface SecondViewController : UIViewController


@property(nonatomic,weak)id<PassValueDelegate> delegate;

@end


#import "SecondViewController.h"


@interface SecondViewController ()

@property (strong,nonatomic)IBOutletUITextField *secondTF;



@end


@implementation SecondViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    self.view.backgroundColor = [UIColorlightGrayColor];

}


- (IBAction)popBack:(id)sender {

    if(self.delegate respondsToSelector:@selector(passValue:)){

[self.delegate passValue:self.secondTF.text];

}

    

   // [self dismissViewControllerAnimated:YES completion:nil];

    [self.navigationControllerpopViewControllerAnimated:YES];

}



- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end

Hearder:

#ifndef EpisodeDelegate_Header_h

#define EpisodeDelegate_Header_h


#define kScreen_Width [[UIScreen mainScreen] bounds].size.width

#define kScreen_Hight [[UIScreen mainScreen] bounds].size.hight


#endif



0 0
原创粉丝点击