我有两个ViewControllers。 Viewcontroller1管理IOS设备。 ViewController2管理外部显示器。在ViewController1中,我有一个显示在标签上的计时器。在ViewController2中,我有一个函数,它接收参数字符串时间以在控制台中打印参数并在另一个标签中显示它。此函数正确接收参数并将其打印到控制台,但不更新标签。感谢
ViewController1.swift
import UIKit
class ViewController_1: UIViewController
{
var count = 90
var minutos = 0
var segundos = 0
var tiempo_string = "00:00"
var miTimer = Timer()
@IBOutlet weak var etiquetaPrincipal: UILabel!
@IBAction func empezarCuenta(_ sender: UIButton)
{
miTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(counter), userInfo: nil, repeats: true)
}
override func viewDidLoad()
{
super.viewDidLoad()
let notificationCentre = NotificationCenter.default
notificationCentre.addObserver(self, selector: #selector(ViewController_1.pantallaConectada), name:
NSNotification.Name.UIScreenDidConnect, object: nil)
if UIScreen.screens.count > 1
{
pantallaConectada()
}
}
var segundaVentana : UIWindow?
func pantallaConectada()
{
prepareNuevaPantalla()
}
func prepareNuevaPantalla()
{
let secondScreen = UIScreen.screens[1]
segundaVentana = UIWindow(frame: secondScreen.bounds)
segundaVentana?.screen = secondScreen
let myStoryboard = UIStoryboard(name: "Main", bundle: nil) "ViewController_2_ID" que se encuentra dentro de myStoryboard
let SecondViewController = myStoryboard.instantiateViewController(withIdentifier: "ViewController_2_ID")
segundaVentana?.rootViewController = SecondViewController
segundaVentana?.isHidden = false
}
func counter() {
count -= 1
minutos = count / 60
segundos = count % 60
if minutos >= 10 && segundos < 10
{
tiempo_string = "\(minutos):0\(segundos)"
}
else if minutos < 10 && segundos >= 10
{
tiempo_string = "0\(minutos):\(segundos)"
}
else if minutos < 10 && segundos < 10
{
tiempo_string = "0\(minutos):0\(segundos)"
}
else
{
tiempo_string = "\(minutos):\(segundos)"
}
//Edit Text of the Label
self.etiquetaPrincipal.text = self.tiempo_string
let vistaExterna = ViewController_2()
vistaExterna.mostrarTiempo(tiempo_string: self.tiempo_string)
if count == 0
{
miTimer.invalidate() //paramos el Timer
}
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
ViewController2.swift
import UIKit
class ViewController_2: UIViewController
{
@IBOutlet weak var etiquetaSecundaria: UILabel?
override func viewDidLoad()
{
super.viewDidLoad()
let vistaPrincipal = ViewController_1()
let tiempo_string = vistaPrincipal.tiempo_string
mostrarTiempo(tiempo_string: tiempo_string)
}
func mostrarTiempo(tiempo_string: String)
{
print(tiempo_string)
self.etiquetaSecundaria?.text = tiempo_string
}