[IOS基础知识]视图集切换数据传递

程序经常会用到两个场景切换,并需要彼此传递数据,本文介绍ViewController和SecondViewController之间传递参数方式,界面效果如上图。

点击ViewController中的"Sent Second"按钮,会将文本编辑框中的内容传给SecondViewController中的文本编辑框中,点击SecondViewController中的"Return First"按钮,将其中文本编辑框中的内容传回ViewController。

1.ViewController -> SecondViewController:

首先在SecondViewController中定义一个变量:NSString *nsText;

.h文件:

@property (copy,nonatomic) NSString *nsText;

.m文件:

@synthesize nsText;


ViewControlle中重写方法 prepareForSegue:,详细如下:

.h文件:

#import"XYSecondViewController.h"

.m文件

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

{

    XYSecondViewController *xySecondViewController = segue.destinationViewController;

    xySecondViewController.nsText =textField.text;

}


2.SecondViewController -> ViewController:

采用委托方法,首先在SecondViewController定义委托:

.h文件:

@protocol XYSecondViewControllerDelegate <NSObject>


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


@end


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

.m文件:

@synthesize delegate;

在按钮响应方法中添加:

    [self.delegateaddItem:textField.text];


ViewController中声明委托对象并实现委托XYSecondViewControllerDelegate 中的addItem:方法:

.h文件:

@interface ViewController :UIViewController <XYSecondViewControllerDelegate>

.m文件:

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

{

    // 声明委托对象,这个很重要

    XYSecondViewController *xySecondViewController = segue.destinationViewController;

    xySecondViewController.delegate =self;

    // ...

}


- (void)addItem:(NSString *)text

{

    [textFieldsetText:text];

}


完整代码:

//

//  ViewController.h

//  TestDelegate

//

//  Created by皮特 on 15-5-15.

//  Copyright (c) 2015 __MyCompanyName__. All rights reserved.

//


#import<UIKit/UIKit.h>

#import"XYSecondViewController.h"


@interface ViewController :UIViewController <XYSecondViewControllerDelegate>

@property (weak,nonatomic) IBOutletUITextField *textField;

- (IBAction)doTextFieldEditEndBtn:(id)sender;


@end


//

//  ViewController.m

//  TestDelegate

//

//  Created by皮特 on 15-5-15.

//  Copyright (c) 2015 __MyCompanyName__. All rights reserved.

//


#import"ViewController.h"


@interfaceViewController ()


@end


@implementation ViewController

@synthesize textField;

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

{

    XYSecondViewController *xySecondViewController = segue.destinationViewController;

    xySecondViewController.delegate =self;

    xySecondViewController.nsText =textField.text;

}


- (void)viewDidLoad

{

    [superviewDidLoad];

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

}


- (void)viewDidUnload

{

    [selfsetTextField:nil];

    [superviewDidUnload];

   // Release any retained subviews of the main view.

}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

   return (interfaceOrientation !=UIInterfaceOrientationPortraitUpsideDown);

}


- (void)addItem:(NSString *)text

{

    [textFieldsetText:text];

}


- (IBAction)doTextFieldEditEndBtn:(id)sender {

    [senderresignFirstResponder];

}

@end


//

//  XYSecondViewController.h

//  TestDelegate

//

//  Created by皮特 on 15-5-15.

//  Copyright (c) 2015 __MyCompanyName__. All rights reserved.

//


#import<UIKit/UIKit.h>


@protocol XYSecondViewControllerDelegate <NSObject>


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


@end


@interface XYSecondViewController :UIViewController

- (IBAction)textFieldDoneEdit:(id)sender;

@property (weak,nonatomic) IBOutletUITextField *textField;

- (IBAction)doBackFirstBtn:(id)sender;


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

@property (copy,nonatomic) NSString *nsText;

@end


//

//  XYSecondViewController.m

//  TestDelegate

//

//  Created by 皮特 on 15-5-15.

//  Copyright (c) 2015 __MyCompanyName__. All rights reserved.

//


#import"XYSecondViewController.h"


@interfaceXYSecondViewController ()


@end


@implementation XYSecondViewController

@synthesize textField;

@synthesize delegate;

@synthesize nsText;


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

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

       // Custom initialization

    }

   return self;

}


- (void)viewDidLoad

{

    [superviewDidLoad];

// Do any additional setup after loading the view.

    if (nsText != nil) {

       textField.text =nsText;

    }

}


- (void)viewDidUnload

{

    delegate = nil;

    nsText = nil;

    [selfsetTextField:nil];

    [superviewDidUnload];

   // Release any retained subviews of the main view.

}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

    return (interfaceOrientation == UIInterfaceOrientationPortrait);

}


- (IBAction)doBackFirstBtn:(id)sender {

    [self.delegateaddItem:textField.text];

    [selfdismissModalViewControllerAnimated:YES];

}


- (IBAction)textFieldDoneEdit:(id)sender {

    [senderresignFirstResponder];

}


@end








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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值