这是我的代码我不知道我做错了什么。所有的问题都在功能史上。 LebelText是一个定时器计时器标签,由另一个视图控制器进行调整。所以我想把lebetText转换为int来从中减去1,然后将其重新转换回字符串以显示数字。
这是视图控制器a。被塞住的短信是lebelText。
import UIKit
class testViewController: UIViewController {
@IBOutlet var lazel: UILabel!
@IBOutlet var plax: UIButton!
@IBOutlet var stopx: UIButton!
var timer = Timer()
var counter = 0.0
var isRunning = false
override func viewDidLoad() {
super.viewDidLoad()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let DestViewController : restultViewController = segue.destination as! restultViewController
DestViewController.LebelText = lazel.text!
}
@IBAction func play(_ sender: Any) {
if !isRunning{
timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(testViewController.update), userInfo: nil, repeats: true)
}
plax.isEnabled = false
stopx.isEnabled = true
}
@IBAction func stop(_ sender: Any) {
plax.isEnabled = true
stopx.isEnabled = false
timer.invalidate()
isRunning = false
}
func update(){
counter += 0.1
lazel.text = String(format: "1%f", counter)
lazel.text = "\(counter)"
}}
这是视图控制器b。目标是将lebelText转换为int,从中减去1。然后将其转换回字符串,以便显示。
import UIKit
class restultViewController: UIViewController {
@IBOutlet var dxe: UILabel!
var LebelText = String()
let myInt = Int()
override func viewDidLoad() {
super.viewDidLoad()
steve()
}
func steve(){
var eq = LebelText
var intValue = Int(eq)
let vx = intValue! - 1
let ramit = String(vx)
dxe.text = ramit
}
答案 0 :(得分:1)
好的,以便摆脱可选项,并确保它应该像
一样if let intValue = Int(eq) {
vx = intValue - 1
dxe.text = String(vx)
} else {
//do some stuff if you cannot convert eq to Int
}
但我建议你从一些更简单的任务开始,看起来你没有完全学习基础知识。
import UIKit
class restultViewController: UIViewController {
@IBOutlet var someLabel: UILabel!
public var myText: String?
override func viewDidLoad() {
super.viewDidLoad()
self.parseData()
}
private func parseData(){
guard let unwrapedText = self.myText else {
//you didn't pass string
return
}
if let myInt = Int(unwrapedText) {
myInt = myInt - 1
self.someLabel.text = String(myInt)
} else {
//you string is not convertable to int
}
}
}
答案 1 :(得分:-1)
你可以这样做。
import UIKit
class restultViewController: UIViewController {
@IBOutlet var dxe: UILabel!
var LebelText = String()
let myInt = Int()
override func viewDidLoad() {
super.viewDidLoad()
steve()
}
func steve(){
var eq = Int(LebelText.text)
eq = eq - 1
dxe.text = String(eq)
}