【iOS】View跳转和传值

View跳转和传值相当于安卓的Activity之间的跳转和传值,也就是C#窗体中的Form跳转和传值,此乃做apps的必修课之一,下面用一个简单的例子说明如何在iOS实现View的跳转和传值。如下图所示:


第一个View有个输入框Text Field然后点击按钮可以跳转到下一个View,这个View摆放着一个Label用来接受上一个View中的Text Field传递过来的值,然后触摸任何位置可以返回上一个View继续输入。

一、场景布置

1、拖入一个View Controller


2、活用右下角的缩少和放大按钮,必须放大到一个View的大小才能拖入、编辑控件。在原有的View拖入一个Round Rect Button和Text Field并改好名字。


同时在新View中拖入一个Label。并将这个Label的范围拖大以将传递过来的文字显示全。


3、在工程文件下新建一个名为AController并继承于UIViewController的类。


4、点击新拖入来的View最下方的黑色控制符,在属性界面的第三个标签,指明其类为我们刚刚新建的AController,StoryBoard ID为a。


二、代码编写

下面就可以对代码进行编写。

1、首先如同《【iOS】点击按钮Button,更变标签文字Label的颜色》(点击打开链接)一样对两个View的各个组件进行注册,注册之后ViewController.h演变成这样:

//
//  ViewController.h
//  Storyboard_PassValue
//
//  Created by pc on 17-5-31.
//  Copyright (c) 2017年 pc. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITextField *textfield1;
- (IBAction)Goto:(id)sender;

@end
这里的Text Field我叫它textfield1,鼠标的动作叫做Goto。

而AController.h则演变成这样:

//
//  AController.h
//  Storyboard_PassValue
//
//  Created by pc on 17-5-31.
//  Copyright (c) 2017年 pc. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface AController : UIViewController
@property (strong, nonatomic) NSString *content;
@property (weak, nonatomic) IBOutlet UILabel *label1;

@end
除了对Label注册为label1之外,还多添加一个全局变量NSString *content用于接受传过来的值。

2、之后对ViewController.m作如下修改,首先要注意,这里涉及界面跳转和传值,是需要引入头文件AController.h的。

//
//  ViewController.m
//  Storyboard_PassValue
//
//  Created by pc on 17-5-31.
//  Copyright (c) 2017年 pc. All rights reserved.
//

#import "ViewController.h"
#import "AController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

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


//允许用户点击空白区域关闭键盘
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    [self.view endEditing:YES];
}
//允许用户通过点击return键关闭键盘
-(BOOL)textFieldShouldReturn:(UITextField *) textField{
    [textField resignFirstResponder];
    return YES;
}

- (IBAction)Goto:(id)sender {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];// 获取指定的Storyboard,storyboardWithName填写Storyboard的文件名
    AController *aController = [storyboard instantiateViewControllerWithIdentifier:@"a"];// 从Storyboard上按照identifier获取指定的界面(VC),identifier必须是唯一的
    aController.content=self.textfield1.text;
    [self presentViewController:aController animated:YES completion:nil];
}
@end

这里首先加入《【iOS】基本控件:文本输入框、开关、分段控件、滑块与输入键盘隐藏的问题》(点击打开链接)对Text Field弹出的输入键盘的处理。

之后精华在于Goto这个点击按钮之后将会触发的函数里面。利用aController.content=self.textfield1.text;将textfield的内容传到content这个字符串里面,可能有人问我为何不将AController.h中的label的注册改成@property (strong, nonatomic) IBOutlet UILabel *label1;然后直接aController.label1.text=self.textfield1.text;呢?这样就不用多设置一个content的NSString了呀?我也想啊,只是直接aController.label1.text=self.textfield1.text;不知道为何传不到值!只能多设置一个中间变量才行,没办法了。

最后对AController.m修改如下则完成:

//
//  AController.m
//  Storyboard_PassValue
//
//  Created by pc on 17-5-31.
//  Copyright (c) 2017年 pc. All rights reserved.
//

#import "AController.h"

@interface AController ()

@end

@implementation AController

//触摸关闭此界面
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    [self dismissViewControllerAnimated:YES completion:nil];
}
//此界面初始化的时候,将content的内容塞给label1
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.label1.text=self.content;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
这里主要是两句:一句是前文说过的,由于iOS那SB机制导致非要我写,在界面初始化的时候将中间变量塞给Label,self.label1.text=self.content;。还有一句,在触摸函数触发的,返回原界面的[self dismissViewControllerAnimated:YES completion:nil];。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值