• 单例---视图间数据的传递:标签显示输入的内容【多个视图中】


    RootViewController.m

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        
        self.view.backgroundColor = [UIColor greenColor];
        
        //创建显示文字的label
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 200, 40)];
        label.tag = 102;
        label.backgroundColor = [UIColor grayColor];
        [self.view addSubview:label];
        [label release];
        
        //加入按钮
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        button.frame = CGRectMake(20, 20, 90, 60);
        [button setTitle:@"打开模态" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
        
        
    }
    
    - (void)viewWillAppear:(BOOL)animated {
    
        
         UILabel *label = (UILabel *)[self.view viewWithTag:102];
        
        //创建单例对象
        BackData *backData = [BackData shareData];
        
        label.text = backData.text;
        
        [super viewWillAppear:animated];
    }
    
    - (void)buttonAction {
    
        ModalViewController *modalCtrl = [[[ModalViewController alloc] init] autorelease];
        
        modalCtrl.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        
        [self presentViewController:modalCtrl animated:YES completion:NULL];
        
    }
    
    ModalViewController.m

    #import "BackData.h"
    
    @interface ModalViewController ()
    
    @end
    
    @implementation ModalViewController
    
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        
        self.view.backgroundColor = [UIColor redColor];
        
        //加入按钮
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        button.frame = CGRectMake(20, 20, 90, 60);
        [button setTitle:@"关闭模态" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
        
        //创建输入框
        UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(50, 100, 200, 40)];
        textField.tag = 101;
        textField.borderStyle = UITextBorderStyleRoundedRect;
        [self.view addSubview:textField];
        [textField release];
        
        
    }
    
    - (void)buttonAction {
        
        UITextField *field = (UITextField *)[self.view viewWithTag:101];
        
        //创建单例对象
        BackData *data = [BackData shareData];
        data.text = field.text;
        
        [self dismissViewControllerAnimated:YES completion:NULL];
        
    }

    BackData.h

    @interface BackData : NSObject
    
    @property (nonatomic, copy) NSString *text;
    
    + (BackData *)shareData;
    


    BackData.m

    static BackData *data = nil;
    
    @implementation BackData
    
    + (BackData *)shareData {
    
        if (data == nil) {
            data = [[BackData alloc] init];
        }
        
        return data;
    }




  • 相关阅读:
    再学 GDI+[38]: 文本输出 DrawString、TGPFont
    再学 GDI+[42]: 文本输出 字号单位
    再学 GDI+[41]: 文本输出 控制输出字符的个数
    再学 GDI+[45]: 文本输出 文本呈现质量
    博客园电子期刊2008年11月半月刊(上)发布啦
    博客园电子期刊2008年12月●半月刊(上)发布啦
    招聘频道功能更新:RSS订阅
    博客园新版招聘频道(job.cnblogs.com)上线测试啦
    《悟透JavaScript》到货了
    博客园电子期刊2008年11月●半月刊(下)发布啦
  • 原文地址:https://www.cnblogs.com/wzjhoutai/p/7122346.html
Copyright © 2020-2023  润新知