首页 > 移动平台 > 详细

iOS 正向传值demo

时间:2015-10-29 18:35:44      阅读:426      评论:0      收藏:0      [点我收藏+]

建一个空工程在appdelegate里面

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

RootViewController *rvc = [[RootViewController alloc] init];
    self.window.rootViewController = rvc;
    [rvc release];

    
    
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

#import "RootViewController.h"
#import "MyControl.h"
#import "SecondViewController.h"

#define kDebugPrint NSLog(@"%s",__func__)
@interface RootViewController ()<UITextFieldDelegate>
{
    UITextField *_textField;
}
@end

@implementation RootViewController

/*
 正向传值
 创建第一个界面   通过第一个界面跳转到第二个界面
 如果由第一个界面向第二个界面 进行传值 正向传值
 属性传值
 
 
 第二张向第一张界面传值 反向传值
 
 下级界面向上一级界面传值---》反向传值
  */

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor grayColor];
    [self showUI];
}

- (void)showUI {
    _textField = [MyControl creatTextFieldWithFrame:CGRectMake(10, 30, 300, 30) placeHolder:@"请输入内容" delegate:self tag:1001];
    [self.view addSubview:_textField];
    
    UIButton *button = [MyControl creatButtonWithFrame:CGRectMake(10, 200, 300, 50) target:self sel:@selector(btnClick:) tag:201 image:nil title:@"切换到第二张"];
    [self.view addSubview:button];
}

//收键盘
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [_textField resignFirstResponder];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}
- (void)btnClick:(UIButton *)btn {
    
    //
    SecondViewController *svc = [[SecondViewController alloc] init];
    
    //正向传值 必须要在 跳转之前传值
    //属性传值
     svc.textStr =  _textField.text;
    
    
    //跳转到第二张界面 呈现显示第二张
    
    //second 第一次显示的时候 才调用viewDidLoad在这之前已经把值传过去了
    [self presentViewController:svc animated:YES completion:nil];

    [svc release];
}

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


@end
#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController

//用于正向传值  第一个界面可以通过这个 setter方法 把值传给 第二个界面
@property (nonatomic,copy) NSString *textStr;
@end
#import "SecondViewController.h"
#import "MyControl.h"
#define kDebugPrint NSLog(@"%s",__func__)

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

@implementation SecondViewController
- (void)dealloc {
    kDebugPrint;
    
    self.textStr = nil;//[_textStr release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor orangeColor];
    [self showUI];
    
}
- (void)showUI {
    UIButton *button = [MyControl creatButtonWithFrame:CGRectMake(10, 30, 300, 30) target:self sel:@selector(btnClick:) tag:301 image:nil title:@"返回"];
    [self.view addSubview:button];
    
    _label = [MyControl creatLabelWithFrame:CGRectMake(10, 100, 300, 50) text:@""];
    _label.backgroundColor = [UIColor grayColor];
    
    //直接赋值  在 创建label的时候已经从第一个界面传过来了
    _label.text = self.textStr;
    
    [self.view addSubview:_label];
}
- (void)btnClick:(UIButton *)btn {
    //返回上一级
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end


iOS 正向传值demo

原文:http://my.oschina.net/u/2410306/blog/523632

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!