美文网首页swift
Swift-页面跳转+传值

Swift-页面跳转+传值

作者: Harlan_Zhang | 来源:发表于2019-01-29 10:37 被阅读0次

本文介绍了IOS开发中的几种基础常用的页面跳转方式以及传值的方式。

  1. present + dismiss

场景准备,主页面以及要跳转的目的页面,对应MainViewController.swift 和DestinationViewController.swift。

MainViewController页面初始化一个跳转按钮,并绑定一个点击跳转事件,跳转的代码写在跳转函数pageJump中。
代码如下:

import UIKit

class MainViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        initBtn()
    }
    //初始化按钮,点击按钮跳转页面
    func initBtn() {
        let screenSize = UIScreen.main.bounds.size
        let jumpBtn = UIButton(type: .system)
        jumpBtn.setTitle("跳转", for: .normal)
        jumpBtn.frame = CGRect(x: screenSize.width / 2 - 50, y: screenSize.height - 50, width: 100, height: 30)
        jumpBtn.backgroundColor = UIColor(red: 50 / 255, green: 123 / 255, blue:  255 / 255, alpha: 1)
        jumpBtn.setTitleColor(UIColor.white, for: .normal)
        //按钮绑定事件,点击时执行
        jumpBtn.addTarget(self, action: #selector(pageJump), for: .touchDown)
        self.view.addSubview(jumpBtn)

    }
    
    @objc func pageJump() {
        //创建一个页面
        let destination = DestinationViewController()
        //取目标页面的一个变量进行赋值,以属性的方式进行传值。
        destination.message = "传递的信息"
        //跳转
        self.present(destination, animated: true, completion: nil)
    }
}

Destination页面初始化一个返回按钮,并绑定返回事件。
代码如下:

import UIKit

class DestinationViewController: UIViewController {

    var message: String?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        initBtn()
        print(message!)
    }
    //初始化返回按钮,点击按钮返回主页面。
    func initBtn() {
        let screenSize = UIScreen.main.bounds.size
        let jumpBtn = UIButton(type: .system)
        jumpBtn.setTitle("返回", for: .normal)
        jumpBtn.frame = CGRect(x: screenSize.width / 2 - 50, y: screenSize.height - 50, width: 100, height: 30)
        jumpBtn.backgroundColor = UIColor(red: 50 / 255, green: 123 / 255, blue:  255 / 255, alpha: 1)
        jumpBtn.setTitleColor(UIColor.white, for: .normal)
        //按钮绑定事件
        jumpBtn.addTarget(self, action: #selector(pageReturn), for: .touchDown)
        self.view.addSubview(jumpBtn)
    }
    
    @objc func pageReturn() {
        //返回主页面
        self.dismiss(animated: true, completion: nil)
    }
    
}

注意!如果你的页面是在storyboard上面设计出来的,那么跳转时的代码要进行相应变化。

  @objc func pageJump() {
        //Main是storyboard的名字
        let sb = UIStoryboard(name: "Main", bundle: nil)
        //withIdentifier参数的值destinationPage是目的页面的Storyboard ID, 这个值在Storyboard里边进行设置。
        let destination = sb.instantiateViewController(withIdentifier: "destnationPage") as! DestinationViewController
        destination.message = "传递的信息"
        self.present(destination, animated: true, completion: nil)
    }

返回的代码没有变化。

2.push + pop

如果使用带导航栏的NavigationController可以使用push + pop 方法来跳转页面并返回。

场景准备,MainViewController.swift 和DestinationViewController.swift,并把MainViewController设为NavigationController的rootViewController,可以在Storyboard中直接设置。

MainViewController其它代码不变,只改变跳转函数。
代码如下:

    @objc func pageJump() {
        let sb = UIStoryboard(name: "Main", bundle: nil)
        let destination = sb.instantiateViewController(withIdentifier: "destnationPage") as! DestinationViewController
        destination.message = "传递的信息"
        //跳转
        self.navigationController?.pushViewController(destination, animated: true)
    }

DestinationViewController页面此时在导航栏上自带返回按钮,如果要使用自定义的按钮返回,代码如下:

   @objc func pageReturn() {
        //返回上一页面
        self.navigationController?.popViewController(animated: true)
    }

在跳转多个页面后返回rootViewController。

   @objc func pageReturn() {
        //返回主页面rootViewController
        self.navigationController?.popToRootViewController(animated: true)
    }

3.segue跳转

使用segue进行跳转第一步必须在storyboard中创建segue。首先在页面上拖拽一个按钮,然后在按钮上按住右键拖到要跳转的页面上,松开后在Action segue中选择show,然后一个segue就建立了,使用这种方法不需要任何额外的操作与代码,此时只需要点击按钮就会自动跳转。

如果使用tableViewCell建立的segue,直接点击相应的cell不会跳转,此时需要在代码中处理。

   //该方法是重写了tableView方法,当点击tableViewCell时该方法就会被触发。
   override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let message =  "信息"
        //withIdentifier的值是在storyboard中创建的segue的Identifier值,需要在storyboard中设置
        self.performSegue(withIdentifier: "MainToDestination", sender: message)
    }

传值也需要单独处理

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        //判断是通过哪个segue进行的跳转,然后处理传值
        if segue.identifier == "MainToDestination" {
            let controller = segue.destination as! DestinationViewController
            controller.message= (sender as? String)!
        }
    }

以上就是本次文章的内容,欢迎大家留言。

相关文章

  • Swift-页面跳转+传值

    本文介绍了IOS开发中的几种基础常用的页面跳转方式以及传值的方式。 present + dismiss 场景准备,...

  • ionic关于跳转那些事

    简单介绍一下跳转页面,和页面传值 引入 页面跳转并传值 返回上一页 请求接口失败或者成功返回页面 如果需要返回是传入值

  • swift 闭包传值

    场景:A页面跳转到B页面,B页面返回到A页面,(B页面给A页面传值) B页面逻辑:创建block,声明变量,传值 ...

  • 微信小程序几种常用的跳转方式并传值

    第一种:通过链接传值(跳转页面传值) 第二种:跳转到 tabBar 页面,并关闭其他所有非 tabBar 页面 第...

  • swift 代理传值

    场景:A页面跳转到B页面,B页面返回到A页面,(B页面给A页面传值) B页面逻辑:创建协议,声明变量,传值 A页面...

  • 金蝶kingdee mbos移动轻应用开发脚本参考

    前端事件脚本 1.手动调用提交校验页面字段必填项方法: 2.mbos内页面跳转以及传值: 3.跳转后页面接收传值:...

  • query与params的页面传值

    先简单记录页面传值, 还没完全弄懂其中的原理先配置路径跳转 params data传参 params传参 跳转页面...

  • iOS五种页面传值方式

    方式一:属性传值 A页面的值跳转到B页面之后把值传给B页面,只需要在.h文件定义属性,A页面再跳转之前赋值给B页面...

  • React Native学习笔记(七)-页面跳转、传值、回传

    React Native页面跳转、传值、回传 Navigator --- 在React Native中的不同页面我...

  • 4、Mpvue页面跳转传值、V-html解析等

    一、页面跳转传值 绑定点击事件 页面跳转传参 页面效果页面参数 二、VUE之V-html解析(使用:简单的文本类型...

网友评论

    本文标题:Swift-页面跳转+传值

    本文链接:https://www.haomeiwen.com/subject/lpwojqtx.html