Believe in your own future, will thank yourself right now Sinner Yun

Sinner_Yun

数据保存

      

 

 

#import "ViewController.h"

#import "CompanyViewController.h"

#import "ResultViewController.h"

 

@interface ViewController ()

 

{

    CompanyViewController *_company;

    ResultViewController *_result;

}

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad

{

    [super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

    //创建2个视图控制器

    _company = [[CompanyViewController alloc]init];

    _result = [[ResultViewController alloc]init];

    

    //将他们的view添加到当前视图控制器中

    [self.view addSubview:_result.view];

    [self.view addSubview:_company.view];

    

    //屏幕高度

    CGFloat height = [UIScreen mainScreen].bounds.size.height;

    

    //2个button

    for (int i = 0; i<2; i++) {

        UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];

        btn.frame = CGRectMake(170*i, height-40, 150, 40);

        if (!i) {

            [btn setTitle:@"company" forState:UIControlStateNormal];

        } else {

            [btn setTitle:@"result" forState:UIControlStateNormal];

        }

        btn.tag = 100+i;

        btn.backgroundColor = [UIColor cyanColor];

        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];

        [self.view addSubview:btn];

    }

}

 

//button点击事件

- (void)btnClick:(UIButton *)sender

{

    //点击公司按钮就将结果页放到下面

    //点击结果按钮就将公司页放到下面

    if (sender.tag == 100) {

        NSLog(@"company");

        [self.view sendSubviewToBack:_result.view];

    } else {

        NSLog(@"result");

        //点击结果页的时候刷新界面界面

        [_result refreshData];

        [self.view sendSubviewToBack:_company.view];

    }

}

 

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

@end

 

 

 

 

 

 

 

 

 

 

#import "CompanyViewController.h"

#import "AppDelegate.h"

 

@interface CompanyViewController ()

 

@end

 

@implementation CompanyViewController

 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}

 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    [self.view endEditing:YES];

}

 

- (void)viewDidLoad

{

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColor orangeColor];

    

    //创建3个输入框

    NSArray *strArr = [NSArray arrayWithObjects:@"公司名", @"CEO", @"注册资金", nil];

    for (int i = 0; i<3; i++) {

        UITextField *tf = [[UITextField alloc]initWithFrame:CGRectMake(40, 80+i*60, 240, 30)];

        tf.borderStyle = UITextBorderStyleRoundedRect;

        tf.placeholder = strArr[i];

        tf.tag = 100+i;

        [self.view addSubview:tf];

    }

    

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

    btn.frame = CGRectMake(200, 240, 60, 40);

    btn.backgroundColor = [UIColor cyanColor];

    [btn setTitle:@"保存" forState:UIControlStateNormal];

    [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];

}

 

- (void)btnClick

{

    NSLog(@"点击了保存按钮");

    

    //[UIApplication sharedApplication]获取到当前应用程序,通过应用程序找到程序的代理(也就是刚开始的AppDelegate的对象)

    AppDelegate *ad = [UIApplication sharedApplication].delegate;

    //如果代理中保存数据的数组为空,就分配一下空间

    if (!ad.dataArr) {

        ad.dataArr = [NSMutableArray array];

    }

    

    UITextField *name = (UITextField *)[self.view viewWithTag:100];

    UITextField *ceo = (UITextField *)[self.view viewWithTag:101];

    UITextField *money = (UITextField *)[self.view viewWithTag:102];

    

    //将3个输入框的文字转化成一个字符串

    NSString *infoStr = [NSString stringWithFormat:@"公司:%@,ceo:%@,money:%@",name.text,ceo.text,money.text];

    

    //保存到代理的数组中

    [ad.dataArr addObject:infoStr];

    

    //清空

    name.text = @"";

    ceo.text = @"";

    money.text = @"";

    

    [self.view endEditing:YES];

}

 

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

 

 

 

 

 

 

 

 

 

 

#import <UIKit/UIKit.h>

 

@interface ResultViewController : UIViewController

 

//对外暴露一个方法,用来刷新数据

- (void)refreshData;

 

@end

 

 

 

 

 

 

 

 

#import "ResultViewController.h"

#import "AppDelegate.h"

 

@interface ResultViewController ()

 

{

    UILabel *_infoLabel;

}

 

@end

 

@implementation ResultViewController

 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}

 

- (void)viewDidLoad

{

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColor orangeColor];

    

    _infoLabel = [[UILabel alloc]initWithFrame:self.view.frame];

    _infoLabel.numberOfLines = 0;

    [self.view addSubview:_infoLabel];

    

}

 

//.h中声明的,可以在外部调用,用来刷新数据

- (void)refreshData

{

    //找到应用程序的代理对象

    AppDelegate *ad = [UIApplication sharedApplication].delegate;

    

    //将代理对象的数组中保存的字符串用换行拼接成一个新的,并展示

    _infoLabel.text = [ad.dataArr componentsJoinedByString:@"\n"];

}

 

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

 

posted on 2014-03-23 20:24  Sinner_Yun  阅读(261)  评论(0编辑  收藏  举报

导航