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
    

    结果

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


    这里写图片描述

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

  • 相关阅读:
    B.Icebound and Sequence
    Educational Codeforces Round 65 (Rated for Div. 2) D. Bicolored RBS
    Educational Codeforces Round 65 (Rated for Div. 2) C. News Distribution
    Educational Codeforces Round 65 (Rated for Div. 2) B. Lost Numbers
    Educational Codeforces Round 65 (Rated for Div. 2) A. Telephone Number
    Codeforces Round #561 (Div. 2) C. A Tale of Two Lands
    Codeforces Round #561 (Div. 2) B. All the Vowels Please
    Codeforces Round #561 (Div. 2) A. Silent Classroom
    HDU-2119-Matrix(最大匹配)
    读书的感想!
  • 原文地址:https://www.cnblogs.com/coderfish/p/4875465.html
Copyright © 2011-2022 走看看