I am having some trouble understanding how I am supposed to create a view from an IBaction function in Xcode.
Does anyone know how I could open the RKViewController from my home page by clicking on a button I have created 'Select Dates'? The IBaction function relating to this button in my code block is 'calendarDisplay'.
The code block for my 'Select Dates' Button:
import UIKit
import SwiftUI
class HomeViewController: UIViewController {
@State var startIsPresented = false
var rkManager = RKManager(calendar: Calendar.current, minimumDate: Date(), maximumDate: Date().
addingTimeInterval(60*60*24*730), mode: 1)
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
func getTextFromDate(date: Date!) -> String {
let formatter = DateFormatter()
formatter.locale = .current
formatter.dateFormat = "EEEE, MMMM d, yyyy"
return date == nil ? "" : formatter.string(from: date)
}
}
@IBAction func calendarDisplay(_ sender: Any) {
**//Not sure what to put here - have tried many times to call the RKViewController but have failed.**
}
}
This is the code block which works in another project - I have no idea how to make this work in an @IBaction function, any help would be much appreciated.
import SwiftUI
struct ContentView : View {
@State var startIsPresented = false
var rkManager = RKManager(calendar: Calendar.current, minimumDate: Date(), maximumDate: Date().
addingTimeInterval(60*60*24*365), mode: 1) //
var body: some View {
VStack (spacing: 25) {
Button(action: { self.startIsPresented.toggle() }) {
VStack {
Text("Example 2 - Range of Dates Selection").foregroundColor(.blue)
Text("(end date > start date)").foregroundColor(.blue)
}
}
.sheet(isPresented: self.$startIsPresented, content: {
RKViewController(isPresented: self.$startIsPresented, rkManager: self.rkManager)})
VStack {
Text(self.getTextFromDate(date: self.rkManager.startDate))
Text(self.getTextFromDate(date: self.rkManager.endDate))
}
}
}
func getTextFromDate(date: Date!) -> String {
let formatter = DateFormatter()
formatter.locale = .current
formatter.dateFormat = "EEEE, MMMM d, yyyy"
return date == nil ? "" : formatter.string(from: date)
}
}