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

RootViewController.m

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        //注冊通知
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(recieveData:)
                                                     name:BackNotification object:nil];
    }
    return self;
}

- (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)recieveData:(NSNotification *)notification {

     UILabel *label = (UILabel *)[self.view viewWithTag:102];

    NSString *text = [notification.userInfo objectForKey:@"text"];

    label.text = text;

}

- (void)buttonAction {

    ModalViewController *modalCtrl = [[[ModalViewController alloc] init] autorelease];

    modalCtrl.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;

    [self presentViewController:modalCtrl animated:YES completion:NULL];

}

ModalViewController.m

#define  BackNotification     @"BackNotification"
@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];

    NSString *text = field.text;

    NSDictionary *dic = @{@"text":text};

    //发送通知
    [[NSNotificationCenter defaultCenter] postNotificationName:BackNotification
                                                        object:self userInfo:dic];

    [self dismissViewControllerAnimated:YES completion:NULL];

}
时间: 2024-12-10 02:11:53

通知---视图间数据的传递:标签显示输入的内容【多个视图中】的相关文章

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

RootViewController.m #import "ModalViewController.h" @interface RootViewController () @end @implementation RootViewController { ModalViewController *modalCtrl; } - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {

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

RootViewController.h #import "ModalViewController.h" @interface RootViewController : UIViewController<ModalViewDelegate> RootViewController.m @interface RootViewController () @end @implementation RootViewController { ModalViewController *m

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

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 = [

ASP.NET MVC5中View-Controller间数据的传递

使用ASP.NET MVC做开发时,经常需要在页面(View)和控制器(Controller)之间传递数据,那么都有哪些数据传递的方式呢? 本文对于View向Controller中传值共列举了以下几种方式: QueryString RouteData Model Binding Form 使用和Action参数同名的变量进行传递 Cookie 对于Controller向View中传值则列举了以下几种方式: 单个值的传递 Json 匿名类型 ExpandoObject ViewBag.ViewDa

浅析:Acitivity间数据的传递

使用startActivityForResult方法进行数据传递. MainActivity.java: 1 public class MainActivity extends Activity { 2     Button  btn; 3     @Override 4     protected void onCreate(Bundle savedInstanceState) { 5         super.onCreate(savedInstanceState); 6        

Acitivity间数据的传递

使用startActivityForResult方法进行数据传递.    MainActivity.java: 1 public class MainActivity extends Activity { 2 Button btn; 3 @Override 4 protected void onCreate(Bundle savedInstanceState) { 5 super.onCreate(savedInstanceState); 6 setContentView(R.layout.ac

select 标签 未选择任何内容 在 vue.js 中使用遇到的坑

select 标签使用时,经常会遇到不进行任何的选择,即 value=‘’ 的情况 这时如果配合  vue.js 的 v-model 使用,会使得 v-model 标签失效,也就是说,v-model 标签配合 select 不能存在  <option value="" ></option>的情况 但是如果,我们必须有什么也不选择的选项的话,我们可以 <option value="%" >点击选择...</option>

excel限制数据的输入选项如何限制输入的内容

在某些字段中可能只要求输入特定的几个数据,此时如何限制输入的内容呢?(常见问题)excel表格里对数据输入的限制是如何实现的excel2013下拉栏怎样能禁止输入,只能选择下拉框里的选项excel如何只能从下拉式菜单中选择输入项而不能手动输入[wangqiang博客推荐视频教程点击下方链接进入] 1045432700博客推荐视频教程EXCEL从入门到精通`` 原文地址:http://blog.51cto.com/13172026/2287788

WPF,Silverlight与XAML读书笔记第十五 - 页间导航 页间数据传递

?说明:本系列基本上是<WPF揭秘>的读书笔记.在结构安排与文章内容上参照<WPF揭秘>的编排,对内容进行了总结并加入一些个人理解. 导航 有关导航的话题在介绍NavigationWindow与Page等元素时有提及.这篇文章将详细分析导航相关话题.同其它话题,针对WPF,Silverlight与WP 7,导航特性大致相似又有着些许不同.在介绍此特性时相同的特性将合在一起,每个框架独有的特性也将独立介绍并有明显标识. 导航的功能及目的就是从一个页面转向另一个页面,可能是前进亦或是后