08_ui_传值


#import "AppDelegate.h"
#import "FirstViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // 设置window
    self.window = [[[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds] autorelease];
    self.window.backgroundColor = [UIColor grayColor];
    [self.window makeKeyAndVisible];
    
    // 设置根视图控制器
    FirstViewController *firstVC = [[[FirstViewController alloc] init] autorelease];
    UINavigationController *rootNC = [[[UINavigationController alloc] initWithRootViewController:firstVC] autorelease];
    self.window.rootViewController = rootNC;


第一个VC。m

#import "FirstViewController.h"
#import "FirstView.h"
#import "SecondViewController.h"

@interface FirstViewController () <UITextFieldDelegate, SecondViewControllerDelegate>

#pragma mark - 声明私有属性
@property (nonatomic, retain) FirstView *firstView;

@end

@implementation FirstViewController

#pragma mark - 设置自定义视图
- (void)loadView
{
    self.firstView = [[[FirstView alloc] initWithFrame:[UIScreen mainScreen].bounds] autorelease];
    self.view = _firstView;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = @"第一页";
    
    // 设置代理
    _firstView.textField.delegate = self;
    // 绑定事件
    [_firstView.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
}


#pragma mark - Button事件
- (void)buttonAction:(UIButton *)sender
{
    // 创建要跳转的视图控制器
    SecondViewController *secondVC = [[SecondViewController new] autorelease];
    
    // 获取输入框中的内容,并赋值给第二个控制器的string属性
    secondVC.string = _firstView.textField.text;
    secondVC.delegate = self;
    
    // 推出
    [self.navigationController pushViewController:secondVC animated:YES];
}

- (void)sendValue:(NSString *)str
{
    _firstView.textField.text = str;
}



#pragma mark - UITextFieldDelegate Methods
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

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


#pragma mark - 重写
#pragma mark dealloc
- (void)dealloc
{
    [_firstView release];
    
    [super dealloc];
}


第二个V。m

#import "FirstView.h"

@implementation FirstView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor cyanColor];
        [self addAllViews];
    }
    return self;
}

#pragma mark - 添加全部控件
- (void)addAllViews
{
    self.textField = [[[UITextField alloc] initWithFrame:CGRectMake(20, 100, self.frame.size.width - 40, 35)] autorelease];
    _textField.borderStyle = UITextBorderStyleRoundedRect;
    [self addSubview:_textField];
    
    self.button = [UIButton buttonWithType:UIButtonTypeSystem];
    _button.frame = CGRectMake(100, 150, self.frame.size.width - 200, 35);
    [_button setTitle:@"跳转第二页" forState:UIControlStateNormal];
    _button.layer.borderWidth = 1.2;
    _button.layer.borderColor = [UIColor blueColor].CGColor;
    _button.layer.cornerRadius = 5;
    [self addSubview:_button];
}



#pragma mark - 重写
#pragma mark dealloc
- (void)dealloc
{
    [_textField release];
    [_button release];
    
    [super dealloc];
}

第二个VC。h

#import <UIKit/UIKit.h>


@protocol SecondViewControllerDelegate <NSObject>

- (void)sendValue:(NSString *)str;

@end




@interface SecondViewController : UIViewController

#pragma mark - 声明传值
@property (nonatomic, copy) NSString *string;
@property (nonatomic, assign) id<SecondViewControllerDelegate> delegate;

第二个VC。m


#import "SecondViewController.h"
#import "SecondView.h"

@interface SecondViewController () <UITextFieldDelegate>

#pragma mark - 声明私有属性
@property (nonatomic, retain) SecondView *secondView;

@end

@implementation SecondViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        NSLog(@"%d  %s", __LINE__, __FUNCTION__);
    }
    return self;
}


#pragma mark - 设置自定义视图
- (void)loadView
{
    self.secondView = [[[SecondView alloc] initWithFrame:[UIScreen mainScreen].bounds] autorelease];
    self.view = _secondView;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = @"第二页";
    
    // 设置代理
    _secondView.textField.delegate = self;
    // 绑定事件
    [_secondView.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    // 取出_string属性中的值,放到输入框中
    _secondView.textField.text = _string;
}


#pragma mark - Button事件
- (void)buttonAction:(UIButton *)sender
{
    
    if ([_delegate respondsToSelector:@selector(sendValue:)]) {
        [_delegate sendValue:_secondView.textField.text];
    }
    
    
    // 返回上一控制器
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark - UITextFieldDelegate Methods
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

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

#pragma mark - 重写
#pragma mark dealloc
- (void)dealloc
{
    [_secondView release];
    [_string release];
    
    [super dealloc];
}

第二个V.m

#import "SecondView.h"

@implementation SecondView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor magentaColor];
        [self addAllViews];
    }
    return self;
}

#pragma mark - 添加全部控件
- (void)addAllViews
{
    self.textField = [[[UITextField alloc] initWithFrame:CGRectMake(20, 100, self.frame.size.width - 40, 35)] autorelease];
    _textField.borderStyle = UITextBorderStyleRoundedRect;
    [self addSubview:_textField];
    
    self.button = [UIButton buttonWithType:UIButtonTypeSystem];
    _button.frame = CGRectMake(100, 150, self.frame.size.width - 200, 35);
    [_button setTitle:@"返回第一页" forState:UIControlStateNormal];
    _button.layer.borderWidth = 1.2;
    _button.layer.borderColor = [UIColor blueColor].CGColor;
    _button.layer.cornerRadius = 5;
    [self addSubview:_button];
}



#pragma mark - 重写
#pragma mark dealloc
- (void)dealloc
{
    [_textField release];
    [_button release];
    
    [super dealloc];
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值