Objective-C界面传值(一):属性传值

作者: Go_Spec | 来源:发表于2016-04-12 09:25 被阅读652次

属性传值

顾名思义,属性传值是通过类的属性来进行值得传递.属性传值是最容易理解的一种传值方式.通常程序中页面的从前向后传值应用的都是属性传值.下面我们来看一下代码的实现:

AppDelegate.m

- (void)dealloc
{
    [_window release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application     didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

    ViewController *vc = [[ViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    self.window.rootViewController = nav;

    [vc release];
    [nav release];
    [_window release];
    
    return YES;
}

ViewController.h

@interface ViewController : UIViewController

@property(nonatomic, copy) NSString *string;

@end

这里声明的字符串string就是我们要从第一页传到第二页的值.

ViewController.m

#import "ViewController.h"
#import "SecondViewController.h"

@interface ViewController ()

@property(nonatomic, retain) UITextField *textField;

@end

@implementation ViewController

- (void)dealloc
{
    [_textField release];
    [super dealloc];
}

- (void)loadView
{
    [super loadView];
    
    self.view.backgroundColor              = [UIColor whiteColor];
    self.navigationItem.title              = @"首页";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem         alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd     target:self action:@selector(didClickedRightBarButton:)];

    self.textField                         = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 175, 40)];
    self.textField.placeholder             = @"请输入文本";
    self.textField.borderStyle             =     UITextBorderStyleRoundedRect;
    [self.view addSubview:_textField];
}

- (void)didClickedRightBarButton:(UIBarButtonItem *)barButton
{
    SecondViewController *vc2 = [[SecondViewController alloc] init];
    vc2.secondString          = self.textField.text;
    //属性传值就在此执行,在我们push页面时需要在首页的按钮点击方法中建立一个新的SecondViewController的对象,在这里也就是vc2
    //而我们在SecondViewController中已经声明了一个属性secondString用来接收传过去的值
    //所以vc2现在要进行的操作我们可以理解为将要传的值存在vc2自己的属性secondString中
    [self.navigationController pushViewController:vc2 animated:YES];
}

ViewController中仅初始化一个textField用来输入字符串
直接使用navigationItemrightBarButton创建一个执行push页面方法的按钮,点击后可以将页面推送到第二页.

SecondViewController.h

  @interface SecondViewController : UIViewController
  
  @property(nonatomic, copy) NSString *secondString;
  
  @end

SecondViewController中创建一个属性secondString用来接收第一页传过来的值.

@interface SecondViewController ()

@property(nonatomic, retain) UITextField *textField;

@end

@implementation SecondViewController

- (void)dealloc
{
    [_textField release];
    [super dealloc];
}

- (void)loadView
{
    [super loadView];

    self.view.backgroundColor             = [UIColor whiteColor];
    self.navigationItem.title             = @"第二页";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(didClickedLeftBarButton:)];

    self.textField                        = [[UITextField alloc]     initWithFrame:CGRectMake(100, 100, 175, 40)];
    self.textField.placeholder            = @"请输入文本";
    self.textField.borderStyle            =         UITextBorderStyleRoundedRect;
    self.textField.text                   = self.secondString;
    //这里进行的就是将之前存在secondString中的值取出来放在textField中
    [self.view addSubview:_textField];
}

- (void)didClickedLeftBarButton:(UIBarButtonItem *)leftButton
{
    [self.navigationController popViewControllerAnimated:YES];
}

这里不再自定义button直接使用系统的按钮样式.

首页文本框中输入文本
会在第二页的文本框中进行显示

总结:属性传值通常用于从前向后的界面传值,当然从后向前通过属性传值也是可以实现的,不过不推荐使用.因为在界面过多的情况下,从后向前的属性传值过于繁琐(需要通过下标在栈中寻找要传值的ViewController)且不够灵活.在之后我写的文章中会介绍其他几种传值方式,更加灵活

相关文章

  • iOS的5种传值

    (-)属性传值 属性传值(场景)一般用于正向传值,即第一个界面传值给第二个界面 属性传值是这几大传值中最简单的传值...

  • Objective-C学习笔记之传值

    Objective-C的传值 一、属性传值 顾名思义,属性传值就是通过类的属性传值,也是objective–c中最...

  • iOS中界面传值的几种方式

    1.属性传值 属性传值适用于顺序传值,从前面的界面传值给后面的界面。 2.代理传值 用代理的方式实现界面间传值稍微...

  • UI总结-界面传值

    UI总结-界面传值(属性传值,协议传值,block传值,通知中指传值) 在编程过程中,界面传值是很重要的一部分,常...

  • iOS页面间传值详解(一)

    一、A页面传值给B界面: 采用的传值方式:属性传值,在A页面跳转B界面的地方直接给B界面的属性赋值即可。 二、A页...

  • iOS_UI_08_界面通信

    第八章 界面通信 一、属性传值 二、协议传值 三、Block传值

  • IOS开发 多界面传值

    本节学习内容: 1.多界面传值的基本概念 2.多界面传值的方法 3.多界面传值的应用 【多界面传值 属性】 cha...

  • Objective-C------- 传值总结

    一、属性传值 A界面->B界面传值 B_界面.h文件(接收者) B界面.h //1.声明属性 @pro...

  • Objective-C界面传值(一):属性传值

    属性传值 顾名思义,属性传值是通过类的属性来进行值得传递.属性传值是最容易理解的一种传值方式.通常程序中页面的从前...

  • ios界面传值2016.5

    五种方法 1.属性传值,适合界面A到界面B的传值2.单例, 多个界面传值3.通知 , 界面A跳...

网友评论

  • 夜羚:ViewController.h里面定义的string没用上。
  • 1073d142fdd4:传过去


    self.view.backgroundColor = [UIColor whiteColor];

    UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(50, 80, 200, 200)];


    UIImage *im=[[UIImage alloc]initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",self.img]]]];

    image.image=im;

    [self.view addSubview:image];

    UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(100, 300, 100, 30)];
    lable.text = self.str;



    [self.view addSubview:lable];
  • 1073d142fdd4:定义属性@property (nonatomic,strong)NSString *str,*img;
  • AdoveHitler:文章文采出众,颇有上古遗风,少侠好本事啊!

本文标题:Objective-C界面传值(一):属性传值

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