我有這樣的代碼在我ViewController1從屏幕傳遞數據到屏幕xcode?
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提前和對不起我的英語不好:$
EDIT2: 這與segue的種類有關嗎? 因爲我用 '出席模態'
EDIT3: 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]'對象還是隻傳遞'totalCalendarios' var? – nayem
only totalCalendarios –