我是swift的新手,当我尝试构建应用程序时,我遇到了一些问题。 当我尝试将视图A中的值传递给视图B时(但应用程序将首先访问视图B),B中的标签将不会显示。但是,如果我从视图B转到视图A并更新视图A中的值,然后返回视图B,则将更新视图B中的值。为什么会这样?
还有一个问题,在退出/返回到另一个视图后,将不会保留从视图A和B更改的值。即使我注销/切换到另一个视图,我怎样才能保持更改?
谢谢大家!这是我的代码。
班级视图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)
}
课程视图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)
}
答案 0 :(得分:0)
视图中的行b;
num_1.text = labeltext
每次实例化一个新字符串。
更改行
var labeltext = String()
到
var labelText : String? = nil
要在注销后保留数据,您需要在应用中开发“持久性”。基本上,您需要在应用程序中嵌入持久的数据存储。
这样做的流行方式是;