iOS 首页投稿(暂停使用,暂停投稿)iOS学习笔记

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

2016-04-12  本文已影响652人  Go_Spec

属性传值

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

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)且不够灵活.在之后我写的文章中会介绍其他几种传值方式,更加灵活

上一篇下一篇

猜你喜欢

热点阅读