gpt4 book ai didi

ios - 使用 Swift 将信息从 ViewController 传递到 ViewController

转载 作者:行者123 更新时间:2023-11-30 11:04:49 26 4
gpt4 key购买 nike

我是编码和 Swift 新手。对于我第一次尝试应用程序,我正在尝试制作一个移动音乐提醒应用程序,我可以在其中输入艺术家、专辑和发行日期,它会通知我专辑今天发布的发行日期。

我正在开发编辑按钮,如果我拼错了艺术家或专辑,甚至弄错了发行日期,我可以进去编辑已保存的信息,并将新数据保存在原始数据上。

目前,我正在尝试将信息从一个 ViewController 传递到另一个 ViewController,但遇到了一些困难。

我需要从 AddfreshreleaseViewController 获取艺术家、专辑和发布日期信息才能转到 EditfreshreleaseViewController

到目前为止,我只是尝试让艺术家信息通过,但我没有任何运气。我观看了视频并阅读了大量文章和书籍,但我似乎无法让它发挥作用。

下面是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)
}



}

任何帮助将不胜感激。

最佳答案

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!
}
}

关于ios - 使用 Swift 将信息从 ViewController 传递到 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52841260/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com