gpt4 book ai didi

ios - 为什么在从另一个页面传递值之前,UILable 不会显示?

转载 作者:行者123 更新时间:2023-11-30 12:19:11 24 4
gpt4 key购买 nike

我是 swift 新手,当我尝试构建应用程序时遇到了一些问题。当我尝试将值从 View A 传递到 View B(但应用程序将首先访问 View B)时,B 的标签将不会显示。但是,如果我从 View B 转到 View A 并更新 View A 中的值,然后返回 View B,则 View B 中的值将会更新。为什么会出现这种情况?

还有一个问题,从 View A 和 B 更改的值在注销/返回到另一个 View 后将不会保留。即使我注销/切换到另一个 View ,如何保留更改?

谢谢大家!这是我的代码。

类 View B

@IBOutlet weak var num_1: UILabel!
@IBOutlet weak var num_2: UILabel!

var labeltext = String()

override func viewDidLoad() {
super.viewDidLoad()
num_1.text = labeltext
}

@IBAction func topic1(sender: AnyObject) {
self.performSegueWithIdentifier("contentView", sender: self)
}

类 View A

override func viewDidAppear(animated: Bool)
{

let isUserLoggedIn = NSUserDefaults.standardUserDefaults().boolForKey("isUserLoggedIn");

if(!isUserLoggedIn)
{
self.performSegueWithIdentifier("loginView", sender: self);
}


}


@IBAction func logoutButtonTapped(sender: AnyObject) {

NSUserDefaults.standardUserDefaults().setBool(false,forKey:"isUserLoggedIn");
NSUserDefaults.standardUserDefaults().synchronize();

let loginViewController = self.storyboard!.instantiateViewControllerWithIdentifier("loginView") as! LoginViewController
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

appDelegate.window?.rootViewController = loginViewController
appDelegate.window?.makeKeyAndVisible()

// self.performSegueWithIdentifier("loginView", sender: self);


}

@IBOutlet weak var voteNum1: UILabel!
@IBOutlet weak var voteNum2: UILabel!
@IBOutlet weak var centerCircle: UIImageView!

var num1 = 0.0
var num2 = 0.0
var sum = 0.0
var a = 0.0
var b = 0.0
let circleShape = CAShapeLayer()

@IBAction func vote1(sender: AnyObject) {
num1 = Double(voteNum1.text!)!
self.voteNum1.text = String(num1 + 1)
a = num1 + 1

}

@IBAction func vote2(sender: AnyObject) {
num2 = Double(voteNum2.text!)!
self.voteNum2.text = String(num2 + 1)
b = num2 + 1
}

@IBAction func calculate(sender: AnyObject) {
sum = Double(a/(b+a))
circleShape.strokeEnd = CGFloat(sum)
}

override func viewDidLoad() {
super.viewDidLoad()
// round view
let roundView = UIView(frame: CGRectMake(85, 100, 150, 150))
roundView.backgroundColor = UIColor.whiteColor()
roundView.layer.cornerRadius = roundView.frame.size.width / 2

// bezier path
let circlePath = UIBezierPath(arcCenter: CGPoint (x: roundView.frame.size.width / 2, y: roundView.frame.size.height / 2),
radius: roundView.frame.size.width / 2.2 ,
startAngle: CGFloat(-0.5 * M_PI),
endAngle: CGFloat(1.5 * M_PI),
clockwise: true)
// circle shape
circleShape.path = circlePath.CGPath
circleShape.strokeColor = UIColor.blackColor().CGColor
circleShape.fillColor = UIColor.clearColor().CGColor
circleShape.lineWidth = 14
// set start and end values
circleShape.strokeStart = 0.0
circleShape.strokeEnd = CGFloat(sum)

// add sublayer
roundView.layer.addSublayer(circleShape)
// add subview
self.view.addSubview(roundView)
self.view.insertSubview(roundView, belowSubview: centerCircle)

}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var DestViewController : View B = segue.destinationViewController as! View B

DestViewController.labeltext = voteNum1.text!
}

@IBAction func back(sender: AnyObject) {
self.performSegueWithIdentifier("back", sender: self)
}

最佳答案

您 View 中的线b;

num_1.text = labeltext

每次都实例化一个新字符串。

换行

var labeltext = String()

var labelText : String? = nil

要在注销后保留数据,您需要在应用程序中开发“持久性”。本质上,您需要在应用程序中嵌入持久的数据存储。

流行的做法是;

  • 核心数据
  • 领域*

* https://realm.io

关于ios - 为什么在从另一个页面传递值之前,UILable 不会显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45009417/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com