• 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
    

    结果

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


    这里写图片描述

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

  • 相关阅读:
    iOS 应用开发入门指南
    修改Visual Studio2010的主题颜色
    C# 获取操作系统相关信息
    WPF Menu控件自定义Style
    Feedback or feedforward?
    Coprimeness
    Admissible, Stabilizability, and Bicoprime Factorization
    Directions of zeros and poles for transfer matrices
    Limit point, Accumulation point, and Condensation point of a set
    Linear System Theory: zero-input response of LTI system
  • 原文地址:https://www.cnblogs.com/coderfish/p/4875465.html
Copyright © 2020-2023  润新知