我正在设计一款应用程序,用于在手机上本地存储数据,并且我正在使用核心数据。我使用主详细的应用程序应用程序类型,因为该应用程序有点像笔记应用程序,但有很多UITextField用于强大的数据。
所以我有很多三个视图控制器
MasterViewController
(这个视图控制器只有一个典型的单元格视图,只有单元格的标题和顶部的添加huston,它将用户带到DetailedVIewController)
DetailedView控制器
(此视图控制器有5-6个UITextField,用于从用户输入,一个按钮用于添加来自相机胶卷的图像,一个UITextfield用于自动填充gps坐标。有一个保存按钮,用于保存所有输入的数据。用户和后退按钮返回MasterViewController)
StorageViewController
(此视图控制器将显示用户在DetailedViewController中输入的相应字段中的所有数据。每次用户保存时,都会在masterviewcontroller中创建一个新单元格,并通过单击用户可以访问该文件的单个单元格。)
我设计了所有应用,甚至可以使用核心数据保存数据。我遇到的问题是在StorageViewController中拨打数据。
我每次保存时都能创建新的单元格,但是当我单击单元格并转到Storageviewcontroller时,没有任何内容填充。我无法弄清楚这个代码。
以下是DetailedViewController
中的代码import UIKit
import MapKit
import CoreLocation
import CoreData
class DetailViewController: UIViewController, UITextFieldDelegate, UIPickerViewDelegate, UIPickerViewDataSource, CLLocationManagerDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate, UITextViewDelegate {
@IBOutlet weak var detailDescriptionLabel: UILabel!
//--------------Buttons and Labels for the project----------------
@IBOutlet weak var locationText: UITextField!
@IBOutlet weak var fileNameText: UITextField!
@IBOutlet weak var tagsText: UITextField!
@IBOutlet weak var Comments: UITextField!
@IBOutlet weak var lableMicrophones: UITextField!
@IBOutlet weak var LableRecorder: UITextField!
@IBAction func importImage(_ sender: Any) {
let image = UIImagePickerController()
image.delegate = self
image.sourceType = UIImagePickerControllerSourceType.photoLibrary
// image.sourceType = UIImagePickerControllerSourceType.camera
image.allowsEditing = false
self.present(image, animated: true)
{
//After it is complete
}
}
@IBOutlet weak var mapKIT: MKMapView!
@IBOutlet weak var forCoordiantes: UITextField!
//--------------Buttons and Labels for the project----------------
// ------------------Declation on containers--------------------------
let manager = CLLocationManager ()
let mircophone_arr = [ "M/S Sennheiser MKH30 & DPA4060", "M/S Sennheiser MKH30 $ MKH20", "Stereo DPA4060s", "Rode NTG2", "Built In Microphone"]
let Recorder_arr = ["Tascam DR-680", "Sound Device 702T", "Zoom H4n", "Zoom H2", "iPhone"]
let my_pickerview = UIPickerView()
var current_arr : [String] = []
var active_textfield: UITextField!
var context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
// ------------------Declation on containers--------------------------
func configureView() {
// Update the user interface for the detail item.
if let detail = detailItem {
if let label = detailDescriptionLabel {
label.text = detail.timestamp!.description
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
configureView()
lableMicrophones.delegate = self
LableRecorder.delegate = self
my_pickerview.delegate = self
my_pickerview.dataSource = self
lableMicrophones.inputView = my_pickerview
LableRecorder.inputView = my_pickerview
//For mapkit
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.requestWhenInUseAuthorization()
manager.startUpdatingLocation()
// Saving Data for location--------------------------------------------
locationText.delegate = self
fileNameText.delegate = self
tagsText.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
var detailItem: Event? {
didSet {
// Update the view.
configureView()
}
}
//---------------------------formapkit Co-ordinates---------------------------------
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations[0]
let span:MKCoordinateSpan = MKCoordinateSpanMake(0.01, 0.01)
let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)
mapKIT.setRegion(region, animated: true)
self.mapKIT.showsUserLocation = true
print(location.coordinate)
forCoordiantes.text? = "\(location.coordinate.latitude, location.coordinate.longitude)"
}
//---------------------------formapkit Co-ordinates---------------------------------
//----------------Hide keyboard when user touches outside------------------------------
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}
//----------------Hide keyboard when user touches outside------------------------------
//-----------------user presses return key------------------------------------------------
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return (true)
}
//----------------Hide keyboard when user touches outside------------------------------
//-----------------All 4 functions belong to picker view---------------------------------
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return current_arr.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return current_arr[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
print("Selected item is", current_arr[row])
active_textfield.text = current_arr[row]
}
//-----------------All 4 functions belong to picker view---------------------------------
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
active_textfield = textField
switch textField {
case lableMicrophones:
current_arr = mircophone_arr
case LableRecorder:
current_arr = Recorder_arr
default:
print("Default")
}
return true
}
//------------------ Save Button function ----------------------------------------------
@IBAction func saveFile(_ sender: Any) {
if locationText.text != "" && fileNameText.text != "" {
let newUser = NSEntityDescription.insertNewObject(forEntityName: "Event", into: context)
newUser.setValue(self.locationText.text, forKey: "locationText")
newUser.setValue(self.fileNameText.text, forKey: "fileNameText")
do {
try context.save()
}
catch {
print("error")
}
}
else {
print("error")
}
}
//------------------ Save Button function ----------------------------------------------
}
请帮忙