zoukankan      html  css  js  c++  java
  • StoryBoard 页面传值

    如图新建一个viewController和DetailViewController

    VC 和DetailVC 联线的Idetnifier 设置为:GoDetailVC

    ViewController主要代码
    - (IBAction)signInButtonTouched:(id)sender { [self performSegueWithIdentifier:@"GoDetailVC" sender:self]; } #pragma mark - prepare segue // A页面传值到B页面 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { [super prepareForSegue:segue sender:sender]; if ([segue.identifier isEqualToString:@"GoDetailVC"]) { DetailViewController *detailVC = segue.destinationViewController; detailVC.passValue = self.usernameTextField.text; } } // B页面传值到A页面 -(IBAction)done:(UIStoryboardSegue *)segue{ if ([segue.sourceViewController isKindOfClass:[DetailViewController class]]) { DetailViewController * svc = (DetailViewController *)segue.sourceViewController; self.passwordTextField.text = svc.textField.text; } }

    在StoryBoard中 DetailViewController的Done按钮 Action关联到 viewController的done方法 如图

    1.在VC页面中第一个textField中输入值 点击 Sing in 按钮就会把值传到DetailViewController 的textField中显示出来。

    2.在DetailViewController textField中修改值 点击Done 按钮就会把值回传到ViewController中

    如果 是Present 方式要怎么传值勒。如图

    用如下代码肯定传值行不通
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { [super prepareForSegue:segue sender:sender]; if ([segue.identifier isEqualToString:@"GoDetailVC"]) { DetailViewController *detailVC = segue.destinationViewController; // 因为这取出来的是NavigationController 不是DetailViewController
    detailVC.passValue = self.usernameTextField.text; 
    }
    }

    所以修改为如下:
    // A页面传值到B页面
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        [super prepareForSegue:segue sender:sender];
        if([[segue identifier] isEqualToString:@"GoDetailVC"])
        {
            NSArray *viewControllerArray = [segue.destinationViewController viewControllers];
            DetailViewController *detailVC = [viewControllerArray objectAtIndex:0];
            detailVC.passValue = self.usernameTextField.text;
        }
    }

     

  • 相关阅读:
    微服务简介
    docker跨主机通信扁平化网络的设计与实现
    docker学习ppt
    docker原理介绍
    我的博客搬家啦
    响应式之像素和viewport
    换行+省略号
    ITerms2在mac系统下的安装和配色,并和go2shell关联
    利其器之webstorm快捷键
    React入门 (2)—实现微博展示列表
  • 原文地址:https://www.cnblogs.com/joesen/p/4585354.html
Copyright © 2011-2022 走看看