美文网首页
iOS属性传值

iOS属性传值

作者: JaneEyre3X | 来源:发表于2018-04-20 15:49 被阅读0次

在APPDelegate中设置导航

#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    ViewController *v1 = [[ViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:v1];
    
    self.window.rootViewController = nav; 
   return YES;
}

在ViewController中实现lable

#import "ViewController.h"
#import "SecondViewController.h"
#import "Model.h"
@interface ViewController ()
{
    UITextField * textField;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"属性正向传值";
    
    textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 120, self.view.frame.size.width-20, 40)];
    textField.placeholder = @"请输入一个值";
    textField.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:textField];
    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(10, 200, self.view.frame.size.width-20, 35)];
    btn.backgroundColor = [UIColor grayColor];
    [btn setTitle:@"确定" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
}

- (void)btnClick:(id)sender
{
    
    SecondViewController * second = [[SecondViewController alloc] init];
    second.string = textField.text; //对需要传递的变量进行复制
    [self.navigationController pushViewController:second animated:YES];  
}

创建一个继承与NSObject的model;

.H中的
#import <Foundation/Foundation.h>

@interface Model : NSObject
@property(nonatomic,strong)NSString * string;
@end
.M中的

#import "Model.h"

@implementation Model

@end

创建一个控制器

#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController
@property(retain, nonatomic)NSString * string;

@end
#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController
@synthesize string;

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    UILabel * laa = [[UILabel alloc]initWithFrame:CGRectMake(10, 100, 80, 20)];
    laa.text = @"你的名字";
    [self.view addSubview:laa];
    
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 200, 20)];
    NSString *str = [NSString stringWithFormat:@"%@",string];
    label.text = str;
    [self.view addSubview:label];
    
}

相关文章

  • iOS的五种传值

    前言 iOS常见的五种传值分别为属性传值,通知传值,代理传值,block传值,单例传值 属性传值 用于正向传值,简...

  • iOS 传值方法(属性传值、代理传值、Block、通知、单例)

    iOS 传值方法(属性传值、代理传值、Block、通知、单例)简单的介绍一下几个传值方式 1、属性传值 在传值的时...

  • iOS中的传值方式

    在日常开发过程中,我们经常会遇到值传递。这里,介绍几种iOS开发中常见的传值方式。 1.属性传值 属性传值是iOS...

  • iOS传值的五种方式

    iOS传值的五种方式:分别为属性 代理 block 单例 通知 属性:属性传值是最简单的一种传值方式,此种方式适用...

  • ios常用的三种传值方式

    iOS中有多种方案可以实现页面之间的传值,例如:属性传值、代理传值、block传值、单例传值...。常用的三种传值...

  • ioS 页面(代理、通知、block、单例、属性)传值

    iOS 页面(代理、通知、block、单例、属性)传值 一、传值分类 页面传值基本分为两种:正向传值和反向传值。 ...

  • iOS 页面(代理、通知、block、单例、属性)传值

    iOS 页面(代理、通知、block、单例、属性)传值 一、传值分类 页面传值基本分为两种:正向传值和反向传值。 ...

  • Block传值

    iOS传值一共有四种:属性传值,代理传值,通知传值以及Block传值; 今天我们来说一下Block传值: 概念:带...

  • iOS传值的几种常用方式

    iOS常用的传值方式有以下几种: 属性传值、单例传值、代理传值、block传值、通知传值 接下来我就分别讲述一下这...

  • iOS传值方式

    在iOS中,常见的传值方式有以下几种:1.属性传值2.单例传值3.通知传值4.代理传值5.Block这些传值方式,...

网友评论

      本文标题:iOS属性传值

      本文链接:https://www.haomeiwen.com/subject/bjfzkftx.html