I'm new to coding and Swift. For my first attempt at an app, I'm attempting to make a mobile music reminder application, in which I can type in the Artist, Album and Release Date and it'll notify me on the release date that the album came out today.
I'm working on the edit button, where if I misspelled the Artist or Album or even got the Release Date wrong, I could go in and edit the already saved information and have that new data saved over the original.
Currently, I'm trying to pass information from one ViewController to another ViewController but I'm having some difficulties.
I need the Artist, Album and Release Date information from the AddfreshreleaseViewController to go the EditfreshreleaseViewController
So far I've only attempted to get the Artist information to pass but I haven't had any luck. I've watched videos and read numerous articles and books but I can't seem to get it to work.
Below is the AddfreshreleaseViewController code:
import UIKit
import CoreData
import UserNotifications
class AddfreshreleaseViewController: UIViewController {
@IBOutlet weak var Artist: UITextField!
/* @IBOutlet weak var textfield = UITextField? (artisttextfield!)
let EditfreshreleaseViewController = segue.destination as! EditfreshreleaseViewController
EditfreshreleaseViewController.receivedString = artisttextfield.text!
}
override func prepare (for segue: UIStoryboardSegue, sender: Any?) {*/
@IBOutlet var artisttextfield: UITextField!
@IBOutlet var albumtextfield: UITextField!
@IBOutlet var releasedatePicker: UIDatePicker!
override func viewDidLoad() {
super.viewDidLoad()
releasedatePicker.minimumDate = Date()
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){
var destinationAddfreshreleaseViewController : EditfreshreleaseViewController = segue.destinationAddfreshreleaseViewController as EditfreshreleaseViewController
destinationAddfreshreleaseViewController.ArtistText = Artist.text!
}
// Do any additional setup after loading the view, typically from a nib.
}
@IBAction func saveTapped( _ sender: UIBarButtonItem) {
let artist = artisttextfield.text ?? ""
let album = albumtextfield.text ?? ""
let releasedate = releasedatePicker.date
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let newRelease = Release_Date(context: context)
newRelease.artist = artist
newRelease.album = album
newRelease.release_date = releasedate as NSDate?
newRelease.release_dateId = UUID().uuidString
if let uniqueId = newRelease.release_dateId {
print("The freshreleaseId is \(uniqueId)")
}
do {
try context.save()
let message = "\(artist)'s new album \(album) releases Today!"
let content = UNMutableNotificationContent()
content.body = message
content.sound = UNNotificationSound.default()
var dateComponents = Calendar.current.dateComponents([.month, .day],
from: releasedate)
dateComponents.hour = 09
dateComponents.minute = 00
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents,
repeats: true)
if let identifier = newRelease.release_dateId {
let request = UNNotificationRequest(identifier: identifier,
content: content, trigger: trigger)
let center = UNUserNotificationCenter.current()
center.add(request, withCompletionHandler: nil)
}
} catch let error {
print("Could not save because of \(error).")
}
dismiss(animated: true, completion: nil)
print("Added a Release Date!")
print("Artist: \(newRelease.artist)")
print("Album: \(newRelease.album)")
print("Release Date: \(newRelease.release_date)")
}
@IBAction func cancelTapped(_ sender: UIBarButtonItem) {
dismiss(animated: true, completion: nil)
}
}
Below is the EditfreshreleaseViewController code:
import UIKit
import CoreData
import UserNotifications
class EditfreshreleaseViewController: UIViewController {
@IBOutlet var Artist: UITextField!
var ArtistText = String()
@IBOutlet var artisttextfield: UITextField!
@IBOutlet var albumtextfield: UITextField!
@IBOutlet var releasedatePicker: UIDatePicker!
/*@IBOutlet weak var artist: UILabel! (ArtistTextField!)
var receivedString = ""*/
override func viewDidLoad() {
Artist.text = ArtistText
super.viewDidLoad()
/*artist.text = receivedString*/
releasedatePicker.minimumDate = Date()
// Do any additional setup after loading the view, typically from a nib.
}
@IBAction func saveTapped( _ sender: UIBarButtonItem) {
let artist = artisttextfield.text ?? ""
let album = albumtextfield.text ?? ""
let releasedate = releasedatePicker.date
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let newRelease = Release_Date(context: context)
newRelease.artist = artist
newRelease.album = album
newRelease.release_date = releasedate as NSDate?
newRelease.release_dateId = UUID().uuidString
if let uniqueId = newRelease.release_dateId {
print("The freshreleaseId is \(uniqueId)")
}
do {
try context.save()
let message = "\(artist)'s new album \(album) releases Today!"
let content = UNMutableNotificationContent()
content.body = message
content.sound = UNNotificationSound.default()
var dateComponents = Calendar.current.dateComponents([.month, .day],
from: releasedate)
dateComponents.hour = 09
dateComponents.minute = 00
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents,
repeats: true)
if let identifier = newRelease.release_dateId {
let request = UNNotificationRequest(identifier: identifier,
content: content, trigger: trigger)
let center = UNUserNotificationCenter.current()
center.add(request, withCompletionHandler: nil)
}
} catch let error {
print("Could not save because of \(error).")
}
dismiss(animated: true, completion: nil)
print("Added a Release Date!")
print("Artist: \(newRelease.artist)")
print("Album: \(newRelease.album)")
print("Release Date: \(newRelease.release_date)")
}
@IBAction func cancelTapped(_ sender: UIBarButtonItem) {
dismiss(animated: true, completion: nil)
}
}
Any help would be greatly appreciated.
The prepareForSegue
function is a UIViewController
method that must be overriden by subclasses, in order to pass data between other ViewControllers through a segue.
Try editing your code in AddfreshreleaseViewController to the following. (I've basically removed some code from your viewDidLoad
function and added a new one just below it)
override func viewDidLoad() {
super.viewDidLoad()
releasedatePicker.minimumDate = Date()
}
override func prepareForSegue(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? EditfreshreleaseViewController{
destination.ArtistText = Artist.text!
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.