私は迅速に新しいです、私はアプリケーションを構築しようとするといくつかの問題があります。 ビューAからビューBに値を渡そうとすると(ただし、アプリケーションはビューBにアクセスします)、Bのラベルは表示されません。ただし、ビューBからビューAに移動してビューAの値を更新し、ビューBに戻ると、ビューBの値が更新されます。なぜこれが起こるのでしょうか?別のページの値を渡す前にUILableが表示されないのはなぜですか?
さらにもう1つ質問すると、ビュー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)
}
こんにちは、お返事ありがとうございます。ビューAの値を渡す前でもビューBのUILabel(num_1)が表示されます。 –
さらに、以前の更新値(num_1、num_2、voteNum1、voteNum2)を保存して、別のページに切り替えてもAからBへの切り替えのように、A内のvoteNum1は変更されず、毎回0を返します。)またはログアウトします。 –
だから、基本的にここに2つのシナリオの値を格納したいですか? 1.しかし、長期的には、アプリを殺すか? 2.アプリ全体で一時的に実行中 –