加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift开发笔记9 - 正向和反向页面传参

发布时间:2020-12-14 01:46:52 所属栏目:百科 来源:网络整理
导读:现有a、b两个页面,a页面中有两个按钮到可以跳转到b页,不同按钮点入后b页的输入框显示内容不同,b页面还要实现输入框文字回传 实际场景: 我有个打卡用的考勤页面和填写迟到或早退理由的备注页面,在考勤页面,点击“迟到理由”按钮,进入备注填写页面,输

现有a、b两个页面,a页面中有两个按钮到可以跳转到b页,不同按钮点入后b页的输入框显示内容不同,b页面还要实现输入框文字回传

实际场景:

我有个打卡用的考勤页面和填写迟到或早退理由的备注页面,在考勤页面,点击“迟到理由”按钮,进入备注填写页面,输入框默认文字是“我昨晚加班了,所以今天上班晚了”,修改完理由后,回退到考勤页面,修改后迟到理由会显示到打卡按钮旁边;同时还有个早退理由按钮,处理过程类似。

在storyboa里segue是这样的:


首先看考勤页面(主页面)如何给备注页面(目标页面)传参:

在考勤页面(主页面)的viewcontroller中找到prepareForSegue方法,这个方法由xcode自动生成,用于在使用segue跳转前,做一些处理动作:

    //记录通过点击哪个按钮进入的备注页面
    var backToButtonInd:Int=1
    override func prepareForSegue(segue: UIStoryboardSegue,sender: AnyObject?) {
  
         print("KaoqinTableViewController ( segue.identifier!)")
         let destinationController = segue.destinationViewController as! UINavigationController
        let nextview:AddCommentViewController = destinationController.childViewControllers[0] as! AddCommentViewController
      
        if(segue.identifier=="onworkReasion"){
             nextview.tempText="我昨晚加班了,所以今天上班晚了"
             backToButtonInd=1
        }else if(segue.identifier=="offworkReasion"){
             nextview.tempText="我一会要去拜访客户,所以先走了"
             backToButtonInd=2
        }
    }


实际上是通过修改segue的目标页面的某个类属性,从而达到传参的目的。

下面是备注页面(目标页面)如何回传参数:

在备注页面(目标页面)使用回调函数来传参,考勤页面把一个回调函数赋值给备注页面的一个函数类型的类属性,然后点击提交按钮后,提交方法中调用该回调函数:

在考勤页面(主页面)的viewcontroller中增加函数commComplet,并在prepareForSegue中给增加一行代码

   //备注页面回传传参
    func commComplet(theData:String){
        print("commComplet接收回传数据")
        if(backToButtonInd==1){
            self.lateReasonLabel.text=theData
        }else{
            self.ofWorkReasonLabel.text=theData
        }
        
    }
    //记录通过点击哪个按钮进入的备注页面
    var backToButtonInd:Int=1
    override func prepareForSegue(segue: UIStoryboardSegue,sender: AnyObject?) {
  
         print("KaoqinTableViewController ( segue.identifier!)")
         let destinationController = segue.destinationViewController as! UINavigationController
        let nextview:AddCommentViewController = destinationController.childViewControllers[0] as! AddCommentViewController 
       //备注页面的回调函数
        nextview.myClosure=commComplet
        if(segue.identifier=="onworkReasion"){
             nextview.tempText="我昨晚加班了,所以今天上班晚了"
             backToButtonInd=1
        }else if(segue.identifier=="offworkReasion"){
             nextview.tempText="我一会要去拜访客户,所以先走了"
             backToButtonInd=2
        }
    }

在备注页面viewcontrollor中:

import UIKit
//回调函数
typealias sendValueClosure=(string:String)->Void

class AddCommentViewController:UIViewController,UITextFieldDelegate {
     
    @IBOutlet weak var beizhuTextView: UITextField!
    //正向传值
    var tempText:String="填写备注"
    //回调考勤页面的方法
    var myClosure:sendValueClosure?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //当页面初始化后,设置输入框默认值 接收考勤页面的正向传值
        beizhuTextView.text=tempText

    }
    @IBAction func addComment(sender: AnyObject) {
        self.beizhuTextView.resignFirstResponder()
        self.dismissViewControllerAnimated(true ){
         if let cellcount=self.beizhuTextView.text {
                 //回调函数 实现反向传值
                  self.myClosure!(string:cellcount )
         }
        self.dismissViewControllerAnimated(true,completion:  {
            self.beizhuTextView.resignFirstResponder()
        })
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读