zoukankan      html  css  js  c++  java
  • iOS学习——页面间的传值

    iOS学习——页面间的传值

    学会ios的页面间传值。

    和android不同,iOS传值不是通过Intent,而是更直接的方法,直接在被传值类中定义一个存放值的变量,在上一个页面直接将值放入,达到传值的目的。

    故事板:

    故事板


    功能描述:

    Created with Raphaël 2.1.0开始在输入框内输入内容点击下一页按钮在Label中显示上一页输入框内容结束

    代码:

    第一个页面替换自己成自己写的类getDataViewController

    • createDataViewController.h
    //
    //  createDataViewController.h
    //  coderfish003
    //
    //  Created by apple40 on 15-7-20.
    //  Copyright (c) 2015年 coderfish. All rights reserved.
    //
    
    #import <UIKit/UIKit.h>
    
    @interface createDataViewController : UIViewController
    
    // 定义输入框
    @property (strong, nonatomic) IBOutlet UITextField *tfCreateData;
    
    @end
    
    • createDataViewController.m
    //
    //  createDataViewController.m
    //  coderfish003
    //
    //  Created by apple40 on 15-7-20.
    //  Copyright (c) 2015年 coderfish. All rights reserved.
    //
    
    #import "createDataViewController.h"
    #import "getDataViewController.h"
    
    @interface createDataViewController ()
    
    @end
    
    @implementation createDataViewController
    
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
    }
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    // 页面传值函数
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        NSString *data = self.tfCreateData.text;
        // 获得getDataViewController对象引用
        getDataViewController *controller = (getDataViewController *)[segue destinationViewController];
        // 给getDataViewController的getData标签内容赋值
        controller.getData = data;
    }
    
    @end
    

    第二个页面替换自己成自己写的类getDataViewController

    • getDataViewController.h
    //
    //  getDataViewController.h
    //  coderfish003
    //
    //  Created by apple40 on 15-7-20.
    //  Copyright (c) 2015年 coderfish. All rights reserved.
    //
    
    #import <UIKit/UIKit.h>
    
    @interface getDataViewController : UIViewController
    @property (strong, nonatomic) IBOutlet UILabel *label;
    @property(strong,nonatomic)NSString *getData;
    @end
    
    • getDataViewController.m
    //
    //  getDataViewController.m
    //  coderfish003
    //
    //  Created by apple40 on 15-7-20.
    //  Copyright (c) 2015年 coderfish. All rights reserved.
    //
    
    #import "getDataViewController.h"
    
    @interface getDataViewController ()
    
    @end
    
    @implementation getDataViewController
    
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        self.label.text = self.getData;
    }
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end
    

    结果

    在第一页的输入框输入文字,点下一页在下一页中显示文字
    这里写图片描述


    这里写图片描述

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    iOS9请求https问题-记录
    邓白氏码的申请-iOS公司开发者账号准备
    iOS学习笔记---oc语言第八天
    iOS学习笔记---oc语言第七天
    iOS学习笔记---oc语言第六天
    一道例题的详解
    使用使用for in 语句,并对数组中元素进行了增删操作,报错却不知怎么办?
    iOS学习笔记---oc语言第五天
    iOS学习笔记---oc语言第四天
    Xcode编译异常和警告汇总(持续更新中)
  • 原文地址:https://www.cnblogs.com/coderfish/p/4875465.html
Copyright © 2011-2022 走看看