zoukankan      html  css  js  c++  java
  • 属性传值

    属性/方法传值

    //1.后面的界面定义了一个属性,用于保存,前一个界面,传过来的值

    //注:属性定义成字符串还是别的类型,取决于你的需求,本例我们须要一个字符串,用于UILabel显示

    //2.后面的界面创建完成之后,为属性赋值,(即:记录须要传递的值)

    //3.在须要使用值的地方,使用属性记录的值这样的通过定义属性,达到传值的方式,称为属性传值,

    //属性传值,一般用于从前一个界面向后一个界面传值;

    代码例如以下:

    #import "FirstViewController.h"
    #import "SecondViewController.h"
    #import "UIButton+Create.h"
    @interface FirstViewController ()
    {
        UITextField * _textField;//创建一个输入框
    }
    @end
    
    @implementation FirstViewController
    - (void)dealloc
    {
        [_textField release];
        [super dealloc];
    }
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        
        
        self.view.backgroundColor = [UIColor redColor];
        self.navigationItem.title = @"首页";
        /**
         *  1.在第一个界面创建一个输入框
         *  
         */
        _textField = [[UITextField alloc]initWithFrame:CGRectMake(50, 80, 200, 30)];
        _textField.borderStyle = UITextBorderStyleRoundedRect;
        [self.view addSubview:_textField];
        
        
        /**
         *  1.创建一个UIButton,
         *  2.并加入响应事件,从首页跳转到第二个页面.
         */
        UIButton * button = [UIButton systemButtonWithFrame:CGRectMake(100, 120, 50, 50) title:@"Push" target:self action:@selector(didClickButtonAction)];
        [self.view addSubview:button];
        
        
    	// Do any additional setup after loading the view.
    }
    
    - (void)didClickButtonAction
    {
        
        /**
         *  1.用push的方法推出下一个页面
         *  2.把首页输入框输入的字符串,通过SecondViewController类的属性NSString * text接收
         *  3.从而实现把首页输入框输入的字符串,传到第二页的UILabel上.
         */
        SecondViewController * secondVC = [[SecondViewController alloc]init];
        secondVC.text = _textField.text;
        [self.navigationController pushViewController:secondVC animated:YES];
        [secondVC release];
    }
    
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end



    #import "SecondViewController.h"
    
    @interface SecondViewController ()
    
    @end
    @implementation SecondViewController
    
    - (void)dealloc
    {
        [_label release];
        [super dealloc];
    }
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        self.view.backgroundColor = [UIColor orangeColor];
        self.navigationItem.title = @"第二页";
        /**
         *  1.在第二个界面创建一个UILabel
         *  2.把首页输入框输入的字符串,通过SecondViewController类的属性NSString * text接收
         *  3.然后通过赋值给UILabel
         */
        _label = [[UILabel alloc]initWithFrame:CGRectMake(50, 80, 200, 30)];
        _label.backgroundColor = [UIColor greenColor];
        _label.text = self.text;
        [self.view addSubview:_label];
        
    	// Do any additional setup after loading the view.
    }
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    




  • 相关阅读:
    web服务器-Apache
    nginx优化
    nginx下载限速
    nginx-URL重写
    HDU 5358 First One 求和(序列求和,优化)
    HDU 5360 Hiking 登山 (优先队列,排序)
    HDU 5353 Average 糖果分配(模拟,图)
    UVALive 4128 Steam Roller 蒸汽式压路机(最短路,变形) WA中。。。。。
    HDU 5348 MZL's endless loop 给边定向(欧拉回路,最大流)
    HDU 5344 MZL's xor (水题)
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/3940059.html
Copyright © 2011-2022 走看看