【发布时间】:2017-04-19 01:35:23
【问题描述】:
我的 ViewController1 中有这段代码
var calendarios = [Calendario]()
var totalCalendarios1 = 0
(当屏幕加载 totalCalendarios 更改并正确显示总数时,我可以在调试中看到它) 我希望它传递给我的 ViewController2,我已经制作了这段代码:
let copiaCalendarios = ViewController1()
let totalCalendarios2 = copiaCalendarios.totalCalendarios1
我打印了第二个值,它总是说 0,之前没关系。
我也用过这段代码:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destino = segue.destination as? ViewController
destino?.calendariosCopia = calendarios
}
但不起作用。 我究竟做错了什么? 已搜索信息,但已过时
编辑: 这是我的原始代码: CalendarioViewController 是我的第二个 VC,CalendarioTableViewController 是我的第一个 VC 并且是发送者。
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destino = segue.destination as? CalendarioViewController
destino?.calendariosCopia = calendarios
}
这是我用过的代码,这个也是:
let copiaCalendarios = CalendarioTableViewController()
let totalCalendarios = copiaCalendarios.totalCalendarios
在两个VC中都有一个var totalCalendarios = 0
Thx ind提前并为我的英语不好感到抱歉:$
编辑2: segue的种类有关系吗? 因为我用的是'Present Modally'
编辑3: CalendarioTableViewController:
//
// CalendarioTableViewController.swift
// Vizion5
//
// Created by ROB on 16/04/17.
// Copyright © 2017 ROB. All rights reserved.
//
import UIKit
class CalendarioTableViewController: UITableViewController {
var calendarios = [Calendario]()
var totalCalendarios = 0
func cargarEjemplos() {
guard let calendario1 = Calendario(nombre: "2017-A", fin: "17/01/2017", inicio: "05/05/2017") else {
fatalError("Error en calendario table view controller")
}
guard let calendario2 = Calendario(nombre: "2016-A", fin: "17/01/2016", inicio: "05/05/2016") else {
fatalError("Error en calendario table view controller")
}
calendarios += [calendario1, calendario2]
}
func contarCalendarios() {
totalCalendarios = calendarios.count
}
//AÑADIDO AUTOMATICAMENTE
override func viewDidLoad() {
super.viewDidLoad()
//CARGA LOS EJEMPLOS
cargarEjemplos()
contarCalendarios()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return calendarios.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//let identificadorCelda = "tablaCalendario"
guard let cell = tableView.dequeueReusableCell(withIdentifier: "tablaCalendario", for: indexPath) as? CalendarioTableViewCell
else {
fatalError("error 1: calendario table controller")
}
let calendario = calendarios[indexPath.row]
// Configure the cell...
cell.nombreCalendario.text = calendario.nombre
cell.finCalendario.text = calendario.fin
cell.inicioCalendario.text = calendario.inicio
return cell
}
//MARK: ACCIONES
@IBAction func regresarATablaCalendario(sender: UIStoryboardSegue) {
if let viewControllerOrigen = sender.source as? CalendarioViewController, let calendario = viewControllerOrigen.calendario {
//AÑADE EL NUEVO CALENDARIO
let newIndexPath = IndexPath(row: calendarios.count, section: 0)
calendarios.append(calendario)
tableView.insertRows(at: [newIndexPath], with: .automatic)
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
print("Total calendarios es: " + String(totalCalendarios))
let destino = segue.destination as? CalendarioViewController // here you have to use the exact name of your Second View Controller instead of ViewController
destino?.totalCalendarios = totalCalendarios
}
}
我的 CalendarioViewController
//
// CalendarioViewController.swift
// Vizion5
//
// Created by ROB on 16/04/17.
// Copyright © 2017 ROB. All rights reserved.
//
import UIKit
class CalendarioViewController: UIViewController, UITextFieldDelegate, UINavigationControllerDelegate {
@IBOutlet weak var nombreCalendario: UITextField!
@IBOutlet weak var fechaInicioCalendario: UIDatePicker!
@IBOutlet weak var fechaFinCalendario: UIDatePicker!
@IBOutlet weak var botonGuardar: UIBarButtonItem!
var totalCalendarios = 0
//ESTE VALOR SERA PASADO POR 'CALENDARIOVIEWCONTROLLER' EN 'PREPARE(FOR: SENDER:)' O CONTRUIDO PARA AGREGAR UN NUEVO CALENDARIO
var calendario: Calendario?
override func viewDidLoad() {
super.viewDidLoad()
nombreCalendario.delegate = self //SE CONTROLA A SI MISMO
print("Total segue: " + String(totalCalendarios))
//CHECA SI SE PUEDE HABILITAR EL BOTON DE GAURDADO
//DESABILITA EL BOTON DE GUARDADO
botonGuardar.isEnabled = false
//actualizaEstadoBotonGuardar()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//MARK: FUNCIONES DE TECLADO Y DETERMINAR FECHA (QUE EL FIN SEA MAYOR QUE INICIO)
func textFieldDidBeginEditing(_ textField: UITextField) {
botonGuardar.isEnabled = false
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
//SE DESHABILITA EL BOTON DE GUARDAR CUANDO SE ESCRIBE
botonGuardar.isEnabled = false
//ESCONDE EL TECLADO AL PRESIONADO "HECHO" (DONE)
textField.resignFirstResponder()
return true
}
func textFieldDidEndEditing(_ textField: UITextField) {
actualizaEstadoBotonGuardar()
//navigationItem.title = textField.text
}
// MARK: - NAVEGACION (UNWIND SEGUE)
@IBAction func botonCancelar(_ sender: UIBarButtonItem) {
dismiss(animated: true, completion: nil)
}
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
//super.prepare(for: segue, sender: sender)
guard let button = sender as? UIBarButtonItem, button === botonGuardar else {
print("Error en prepare sender")
return
}
let setFormatoFecha = DateFormatter()
setFormatoFecha.dateFormat = "dd/MM/yyyy"
let nombre = nombreCalendario.text
let fin = setFormatoFecha.string(from: fechaFinCalendario.date)
let inicio = setFormatoFecha.string(from: fechaInicioCalendario.date)
//SE INSERTAN LOS DATOS LEIDOS.
calendario = Calendario(nombre: nombre!, fin: fin, inicio: inicio)
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
//MARK: METODOS PRIVADOS
private func actualizaEstadoBotonGuardar() {
//DESHABILITA EL BOTON DE GUARDAR CUANDO ESTA EN BLANCO *WIP*
let texto = nombreCalendario.text ?? ""
let tieneTexto = !texto.isEmpty
//WIP
if (!texto.isEmpty) {
if (fechaInicioCalendario.date >= fechaFinCalendario.date) {
botonGuardar.isEnabled = false
} else {
botonGuardar.isEnabled = true
}
}
}
}
【问题讨论】:
-
您似乎有一个错字。
ViewController而不是ViewController1。 -
您要传递
[Calendario]对象还是只传递totalCalendariosvar?? -
仅总日历
-
@MartinHernandezPerez 在下面看到我的编辑。
标签: ios swift viewcontroller