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

RootViewController.h

#import "ModalViewController.h"

@interface RootViewController : UIViewController<ModalViewDelegate>

RootViewController.m

@interface RootViewController ()

@end

@implementation RootViewController {

    ModalViewController *modalCtrl;

}

- (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];

    UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 200, 20)];
    textLabel.tag = 100;
    textLabel.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:textLabel];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(50, 150, 100, 30);
    [button setTitle:@"打开" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];

    modalCtrl = [[ModalViewController alloc] init];
    //设置代理对象
    modalCtrl.delegate = self;

}

- (void)buttonAction
{
    [self presentViewController:modalCtrl animated:YES completion:NULL];

}

//实现协议方法
- (void)responseData:(NSString *)text {

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

    label.text = text;

}

ModalViewController.h

//返回文本数据
- (void)responseData:(NSString *)text;

@end

@interface ModalViewController : UIViewController <UITextFieldDelegate>

@property(nonatomic, assign)id<ModalViewDelegate> delegate;

ModalViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor greenColor];

    UITextField *textFiled = [[UITextField alloc] initWithFrame:CGRectMake(50, 60, 160, 30)];
    textFiled.tag = 100;
    textFiled.delegate = self;
    textFiled.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:textFiled];
    //显示键盘
    [textFiled becomeFirstResponder];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(50, 150, 100, 30);
    [button setTitle:@"返回" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];

}

//按钮点击事件
- (void)buttonAction
{
    [self dismissViewControllerAnimated:YES completion:NULL];

    UITextField *field = (UITextField *)[self.view viewWithTag:100];
    NSString *text = field.text;

    //判断代理是否实现了协议方法
    if ([self.delegate respondsToSelector:@selector(responseData:)]) {
        //调用协议方法
        [self.delegate responseData:text];
    }

}

//点击return调用的协议方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField {

    //收起键盘
    [textField resignFirstResponder];

    return YES;

}

时间: 2024-10-10 16:32:10

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

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

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

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

RootViewController.m - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { //注冊通知 [[NSNotificationCenter defaultCenter] addObserver:self selec

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

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

关于界面的按钮的显隐 还有jsp页面数据的传递 把页面的标签变成只读

//界面按钮的显示隐藏 界面input的锁定    function change(){       document.getElementById('first1').style.display="inline";       document.getElementById('first2').style.display="inline";       document.getElementById('first3').style.display="in