我是编码和Swift的新手。对于我第一次尝试使用某个应用程序的过程,我尝试制作一个移动音乐提醒应用程序,在该应用程序中,我可以键入Artist,专辑和发行日期,并且它将在发行日期通知我该专辑今天发行。
我正在使用“编辑”按钮,如果我拼错了Artist或Album甚至误解了Release Date,我可以进入并编辑已经保存的信息,并将新数据保存在原始文件上。
当前,我正在尝试将信息从一个ViewController传递到另一个ViewController,但是遇到了一些困难。
我需要AddfreshreleaseViewController中的Artist,Album和Release Date信息才能进入EditfreshreleaseViewController
到目前为止,我仅尝试传递Artist信息,但我没有任何运气。我看过视频,看过许多文章和书籍,但似乎无法正常工作。
下面是AddfreshreleaseViewController代码:
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)
}
}
下面是EditfreshreleaseViewController代码:
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)
}
}
任何帮助将不胜感激。
答案 0 :(得分:0)
prepareForSegue
函数是一个UIViewController
方法,必须通过子类覆盖它,以便通过segue在其他ViewController之间传递数据。
尝试在AddfreshreleaseViewController中将代码编辑为以下内容。 (我基本上已经从您的viewDidLoad
函数中删除了一些代码,并在其下面添加了一个新代码)
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!
}
}