欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

Swift实现页面跳转与参数传递的方法

最编程 2024-08-08 10:05:14
...

本文介绍了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)!
        }
    }

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