在模态视图的textField输入的文字改变首页的Label文字(使用代理和消息机制实现)

ModalViewController.h文件

#import <UIKit/UIKit.h>

@protocol ModalViewControllerDelegate <NSObject>

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

@end

@interface ModalViewController : UIViewController{
@private
    UITextField * _field;
}

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

@end



ModalViewController.m


@synthesize delegate;

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view. backgroundColor = [UIColor purpleColor];
    
    _field = [[UITextField alloc]initWithFrame:CGRectMake(320/2 - 120/2, 100, 120, 40)];
    _field.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:_field];
    [_field release];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(320/2 - 120/2, 150, 120, 40);
    [button setTitle:@"返回" forState:UIControlStateNormal];
    [button addTarget:self
               action:@selector(goToBack)
     forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
}

-(void)goToBack{
    //委托(代理)机制实现
//    if([self.delegate respondsToSelector:@selector(changeLabelText:)]){
//        [self.delegate changeLabelText:_field.text];
//    }
    //消息机制实现
    [[NSNotificationCenter defaultCenter] postNotificationName:kChangeLabelTextNotification  object:_field.text];
    
    if([[UIDevice currentDevice].systemVersion floatValue] < 6.0){
        [self dismissModalViewControllerAnimated:YES];
    }else{
        [self dismissViewControllerAnimated:YES completion:^{
            NSLog(@"back");
        }];
    }
}


XViewController.h


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

@interface XViewController : UIViewController<ModalViewControllerDelegate>

@end


XViewController.m


- (void)viewDidLoad
{
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor greenColor];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(320/2 - 120/2, 100, 120, 30)];
    label.text = @"Hello World";
    label.tag = 1;
    label.textAlignment = UITextAlignmentCenter;
    [self.view addSubview:label];
    [label release];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(320/2 - 120/2, 150, 120, 30);
    [button setTitle:@"跳转" forState:UIControlStateNormal];
    [button addTarget:self
               action:@selector(goTo)
     forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    //消息机制实现
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector
     (changeLabelText:) name:kChangeLabelTextNotification object:nil];
    
}

-(void)goTo{
    ModalViewController *modalVC = [[ModalViewController alloc]init];
    //委托(代理)机制实现
//    modalVC.delegate = self;
    modalVC.modalTransitionStyle = UIModalTransitionStylePartialCurl;
    if([[UIDevice currentDevice].systemVersion floatValue] < 6.0){
        [self presentModalViewController:modalVC animated:YES];
    }
    
}

//委托(代理)机制实现
/*-(void)changeLabelText:(NSString *)text{
    UILabel *label = (UILabel*)[self.view viewWithTag:1];
    label.text = text;
}*/

//消息机制实现
-(void)changeLabelText:(NSNotification *)notification{
    id text = notification.object;
    UILabel *label = (UILabel*)[self.view viewWithTag:1];
    label.text = text;
}


MyTestThree-Prefix.pch


#define kChangeLabelTextNotification @"ChangeLabelTextNotification"


需要注意的是:

1、

textField.borderStyle = UITextBorderStyleRoundedRect;

不设置的话,默认是不可见的

2、

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

这里没有使用道alloc,retain,copy等,因此不能使用release

3、

ModalViewController *modalVC = [[ModalViewController alloc]init];

不能去release掉,因为后来会要用到,因此最好的是将其放在.h文件中



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值