iOS 前后传值 delegate

今天复习两个界面的传值。有两个界面,当点击第一个界面的按钮时,第一个界面的UITextField的值就会传给第二个界面的UITextField,同理,当点击第二个界面的按钮时,第二个界面的UITextField的值就会传给第一个界面的UITextField。

首先创建两个继承于ViewController的类,分别是MainViewController and SecondViewController.

MainViewController.h

//设置两个属性

@property (nonatomic,retain) UITextField *firstTextField;

@property (nonatomic,retain) NSString *firstString;

     mainViewController.m

#import "SecondViewController.h"

//当第二个页面给第一个页面传值时需要用到协议

@interface MainViewController ()<SecondViewCoontrollerDelegate>


@end


@implementation MainViewController


-(void)dealloc

{

    [_firstTextFieldrelease];

    [_firstString release];

    [superdealloc];

}


-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

   self = [superinitWithNibName:nibNameOrNil bundle:nibBundleOrNil];

   if (self) {

        

    }

    return self;

}

- (void)viewDidLoad {

    [superviewDidLoad];

    [self.viewsetBackgroundColor:[UIColorwhiteColor]];

    [selfcreate];

}


- (void)create

{

    self.firstTextField = [[UITextFieldalloc] initWithFrame:CGRectMake(100,200, 175, 40)];

    [_firstTextFieldsetBorderStyle:3];

    [_firstTextFieldsetText:_firstString];

    [self.viewaddSubview:_firstTextField];

    [_firstTextFieldrelease];

    

    UIButton *button = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    [buttonsetFrame:CGRectMake(200,300, 70, 40)];

    [button setTitle:@"按钮"forState:UIControlStateNormal];

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

    [self.viewaddSubview:button];

}


- (void)buttonAction:(id)sender

{

    SecondViewController *secondViewController = [[SecondViewControlleralloc] init];

    [secondViewControllersetDelegate:self];

    secondViewController.secondString =self.firstTextField.text;

    [self.navigationControllerpushViewController:secondViewController animated:YES];

    [secondViewControllerrelease];

}


- (void)sendMessageToFirstViewController:(NSString *)text

{

   self.firstTextField.text = text;

}



   SecondViewController.h
//协议第一步,声明协议

@protocol SecondViewCoontrollerDelegate <NSObject>


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


@end


@interface SecondViewController :UIViewController


@property (nonatomic,retain) UITextField *secondTextField;

@property (nonatomic,retain) NSString *secondString;

//协议第二步:指定代理人

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



  SecondViewcontroller.m


-(void)dealloc

{

    [_secondStringrelease];

    [_secondTextFieldrelease];

    [superdealloc];

}


- (void)viewDidLoad {

    [superviewDidLoad];

    [self.viewsetBackgroundColor:[UIColorwhiteColor]];

    [selfcreate];

}


- (void)create

{

    self.secondTextField = [[UITextFieldalloc] initWithFrame:CGRectMake(100,200, 175, 40)];

    [_secondTextFieldsetBorderStyle:3];

    [_secondTextFieldsetText:_secondString];

    [self.viewaddSubview:_secondTextField];

    [_secondTextFieldrelease];

    

    UIButton *button = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    [buttonsetFrame:CGRectMake(200,300, 70, 40)];

    [button setTitle:@"按钮"forState:UIControlStateNormal];

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

    [self.viewaddSubview:button];

}


- (void)buttonAction:(id)sender

{

    [self.navigationController popToRootViewControllerAnimated:YES];


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

        [self.delegatesendMessageToFirstViewController:self.secondTextField.text];

    }

}




AppDelegate.m

-(void)dealloc

{

    [_secondString release];

    [_secondTextField release];

    [super dealloc];

}


- (void)viewDidLoad {

    [super viewDidLoad];

    [self.view setBackgroundColor:[UIColor whiteColor]];

    [self create];

}


- (void)create

{

    self.secondTextField = [[UITextField alloc] initWithFrame:CGRectMake(100, 200, 175, 40)];

    [_secondTextField setBorderStyle:3];

    [_secondTextField setText:_secondString];

    [self.view addSubview:_secondTextField];

    [_secondTextField release];

    

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    [button setFrame:CGRectMake(200, 300, 70, 40)];

    [button setTitle:@"按钮" forState:UIControlStateNormal];

    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button];

}


- (void)buttonAction:(id)sender

{

    [self.navigationController popToRootViewControllerAnimated:YES];

//协议第三步:设置方法

    if ([self.delegate respondsToSelector:@selector(sendMessageToFirstViewController:)]) {

        [self.delegate sendMessageToFirstViewController:self.secondTextField.text];

    }

}


Appdelegate.m



-(void)dealloc

{

    [_window release];

    [super dealloc];

}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    [_window setBackgroundColor:[UIColor whiteColor]];

    [_window makeKeyAndVisible];

    [_window release];

    

    MainViewController *mainViewController = [[MainViewController alloc] init];

    UINavigationController *navigationController = [[UINavigationController alloc] init];

    [_window setRootViewController: navigationController];

    [navigationController pushViewController:mainViewController animated:YES];

    [navigationController release];

    [mainViewController release];

    return YES;

}





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值