简体   繁体   English

FSCalendar - 从选定的日期到另一个视图创建 segue controller

[英]FSCalendar - create segue from day selected to another view controller

I am a beginner in writing iOS apps and am currently working on a calendar app using FSCalendar.我是编写 iOS 应用程序的初学者,目前正在使用 FSCalendar 开发日历应用程序。 I want my app to go to the next view controller whenever I tap on the selected day on the FSCalendar (to show details about events on the selected day) but I couldn't find a way to create a segue that connects from the selected day to the view controller.每当我在 FSCalendar 上点击选定的日期(以显示有关选定日期的事件的详细信息)时,我希望我的应用程序从 go 到下一个视图 controller,但我找不到创建从选定日期连接的 segue 的方法查看 controller。

If segue is the way to go in this case, how do I make one?如果在这种情况下 segue 是通往 go 的方式,我该怎么做? But if segue isn't the way to do it, please give some suggestions of other possible solutions.但如果 segue 不是这样做的方法,请提供一些其他可能解决方案的建议。 Thank you in advance!先感谢您!

FSCalendar has a "did select date" delegate function. If you are looking at the Swift Storyboard Sample, it is called and prints a formatted date to the debug console. FSCalendar 有一个“did select date”委托 function。如果您正在查看 Swift Storyboard 示例,它会被调用并将格式化日期打印到调试控制台。 That's where you can trigger your segue.那就是你可以触发你的segue的地方。

Start by adding a "Detail" view controller class:首先添加一个“详细信息”视图 controller class:

//
//  DetailViewController.swift
//  FSCalendarSwiftExample
//
//  Created by Don Mag on 8/31/20.
//

import UIKit

class DetailViewController: UIViewController {

    var selectedDate: Date?
    
    @IBOutlet var myLabel: UILabel!
    
    fileprivate let formatter: DateFormatter = {
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy/MM/dd"
        return formatter
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        if let d = selectedDate {
            myLabel.text = formatter.string(from: d)
        }
    }

}

Add a view controller to the Storyboard with a couple labels, set its class to DetailViewController , and connect the "Detail Label" to the myLabel @IBOutlet :将视图 controller 添加到带有几个标签的 Storyboard,将其 class 设置为DetailViewController ,并将“Detail Label”连接到myLabel @IBOutlet

在此处输入图像描述

Ctrl-Drag from the view controller icon at the top of the FSCalendar view controller to your new Detail View Controller, and select Show from the popup menu: Ctrl-从 FSCalendar 视图 controller 顶部的视图 controller 图标拖动到新的详细视图 Controller,并从弹出菜单中Show select:

在此处输入图像描述

You'll see that a segue has been added.您会看到已添加一个segue Select that segue and give it an identifier of "gotoDetail": Select segue 并给它一个标识符“gotoDetail”:

在此处输入图像描述

In InterfaceBuilderViewController class, find this func:InterfaceBuilderViewController class 中,找到这个函数:

func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) {
    print("calendar did select date \(self.formatter.string(from: date))")
    if monthPosition == .previous || monthPosition == .next {
        calendar.setCurrentPage(date, animated: true)
    }
}

and add a performSegue line at the end:并在末尾添加一个performSegue行:

func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) {
    print("calendar did select date \(self.formatter.string(from: date))")
    if monthPosition == .previous || monthPosition == .next {
        calendar.setCurrentPage(date, animated: true)
    }
    // add this line
    self.performSegue(withIdentifier: "gotoDetail", sender: nil)
}

still in InterfaceBuilderViewController class, find the prepare(for segue... func:仍然在InterfaceBuilderViewController class 中,找到prepare(for segue... func:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let config = segue.destination as? CalendarConfigViewController {
        config.lunar = self.lunar
        config.theme = self.theme
        config.selectedDate = self.calendar.selectedDate
        config.firstWeekday = self.calendar.firstWeekday
        config.scrollDirection = self.calendar.scrollDirection
    }
}

and add this code at the end:并在末尾添加此代码:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let config = segue.destination as? CalendarConfigViewController {
        config.lunar = self.lunar
        config.theme = self.theme
        config.selectedDate = self.calendar.selectedDate
        config.firstWeekday = self.calendar.firstWeekday
        config.scrollDirection = self.calendar.scrollDirection
    }
    // add this code
    if let detail = segue.destination as? DetailViewController {
        detail.selectedDate = calendar.selectedDate
    }
}

Run the app, select Interface Builder from the table view, and select a date.从表视图运行应用程序,select Interface Builder ,select 一个日期。 Your new Detail controller should be pushed into view, and it should display your selected date.您的新详细信息 controller 应该被推入视图,并且它应该显示您选择的日期。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从SplitViewController中的MasterViewController发送到另一个View Controller - Segue from the MasterViewController in a SplitViewController to another View Controller 如何从视图控制器创建一个segue到自身 - how create a segue from view controller to itself 自定义单元格中的选定行不会将完整数据从segue传递到另一个视图控制器 - selected row in custom cell doesn't pass complete data from segue to another view controller iOS如何将选定的单元格值隔离到另一个视图控制器中 - iOS how to segue a selected cell value into another view controller 从一个视图控制器切换到另一个视图控制器的推荐方法是哪种? - Which is the recommended way to segue from one view controller to another? 尝试从一个视图 controller 切换到另一个视图时出错 - Error when trying to segue from one view controller to another 使用Segue Perform方法将JSON数据从一个视图控制器发送到另一个 - Send JSONdata from one view controller to another with segue perform method 从嵌入在 UINavigationController 中的一个视图控制器转移到另一个 UINavigationController - Segue from one view controller embedded within UINavigationController to another UINavigationController 无法从textField选择到另一个视图控制器 - Couldn't segue to another view controller from textField 是否可以从另一个情节提要中选择特定的视图控制器 - Is It possible to segue to a specific view controller from another storyboard
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM