IOS(UI)_属性传值

5 篇文章 0 订阅

属性传值

先在ViewController.m类

1.创建一个UILabel和UIButton
记得创建导航控制器 我在这里就不创建了 我在前面的这博客中有提到

    #import "ViewController.h"
#import "NextViewController.h"

@interface ViewController ()
{
    UILabel *_label;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //设置背景图片
    self.view.backgroundColor = [UIColor whiteColor];
    //设置标题
    self.title = @"ViewController";
    //创建一个标签
    _label = [UILabel new];
    _label.text = @"LOL";
    _label.backgroundColor = [UIColor yellowColor];
    _label.textAlignment = NSTextAlignmentCenter;//文本居中
    //关闭约束
    _label.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:_label];

    NSLayoutConstraint *constraint1 = [NSLayoutConstraint constraintWithItem:_label attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:100];
    [self.view addConstraint:constraint1];

    NSLayoutConstraint *constraint2 = [NSLayoutConstraint constraintWithItem:_label attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0];
    [self.view addConstraint:constraint2];
    //下面是一个自由布局
    NSLayoutConstraint *constraint3 = [NSLayoutConstraint constraintWithItem:_label attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1 constant:200];
    [self.view addConstraint:constraint3];

    NSLayoutConstraint *constraint4 = [NSLayoutConstraint constraintWithItem:_label attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1 constant:30];
    [self.view addConstraint:constraint4];
    //设置一个按钮
    UIButton *pushButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [pushButton setTitle:@"push" forState:UIControlStateNormal];
    [pushButton setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];//默认状态文字是紫色

    pushButton.translatesAutoresizingMaskIntoConstraints = NO;
    //创建点击事件
    [pushButton addTarget:self action:@selector(pushBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:pushButton];

    //按钮的自由布局
    NSLayoutConstraint *constraint11 = [NSLayoutConstraint constraintWithItem:pushButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_label attribute:NSLayoutAttributeBottom multiplier:1 constant:50];
    [self.view addConstraint:constraint11];

    NSLayoutConstraint *constraint12 = [NSLayoutConstraint constraintWithItem:pushButton attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0];
    [self.view addConstraint:constraint12];

    NSLayoutConstraint *constraint13 = [NSLayoutConstraint constraintWithItem:pushButton attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1 constant:100];
    [self.view addConstraint:constraint13];

    NSLayoutConstraint *constraint14 = [NSLayoutConstraint constraintWithItem:pushButton attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1 constant:30];
    [self.view addConstraint:constraint14];


}

#pragma mark ---------------------
-(void)pushBtnAction:(UIButton *)sender
{
    NextViewController *nextViewController = [[NextViewController alloc]init];

    nextViewController.text = _label.text;
    [self.navigationController pushViewController:nextViewController animated:YES];

}

NextViewController.h

    #import <UIKit/UIKit.h>

@interface NextViewController : UIViewController

@property(nonatomic,strong)NSString *text;


@end

NextViewController.m

#import "NextViewController.h"

@interface NextViewController ()

@end

@implementation NextViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"NextViewController";

    UILabel *label = [UILabel new];
    label.backgroundColor = [UIColor yellowColor];

    label.text = self.text;//属性传值

    label.textAlignment = NSTextAlignmentCenter;

    label.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:label];

    NSLayoutConstraint *constraint1 = [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:100];
    [self.view addConstraint:constraint1];

    NSLayoutConstraint *constraint2 = [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0];
    [self.view addConstraint:constraint2];

    NSLayoutConstraint *constraint3 = [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1 constant:200];
    [self.view addConstraint:constraint3];

    NSLayoutConstraint *constraint4 = [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1 constant:30];
    [self.view addConstraint:constraint4];
 }

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

@end

这里写图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值