2016-06-21 10 views
-3

私は短時間の勉強をしていて、xcodeで簡単なios swiftアプリケーションを設計してアラートを開き、タップするとラベルの中に開発者の名前を表示します。コードは以下に掲載されています。何らかの理由でlabel.hidden = trueに「期待される宣言」というエラーが表示されますが、それ以外は何も表示されません。なぜこのエラーメッセージが表示されるのですか?それはどういう意味ですか?このスウィフトコードが正しく機能しないのはなぜですか?

import UIKit 

class ViewController: UIViewController { 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

//declares the label 
let label = UILabel(frame: CGRectMake(0, 0, 200, 21)) 
//should hide the label 
label.hidden = true 
//next 2 lines center the label 
label.center = CGPointMake(160, 284) 

label.textAlignment = NSTextAlignment.Center 
//puts text into the label 
label.text = "This app was developed by me!" 

//declares the function 
@IBAction func function() { 
    // declares variable that stores the alert and it's properties 
    let alertController = UIAlertController(title: "Welcome!", message: "Here is some information about the developer!", preferredStyle: .Alert) 
    alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)) 
    self.presentViewController(alertController, animated: true, completion: nil) 
    label.hidden = false 


    } 
} 

答えて

2

機能でカプセル化する必要があります。

class ViewController: UIViewController { 

    let label = UILabel(frame: CGRectMake(0, 0, 200, 21)) 

    override func viewDidLoad() { 
    super.viewDidLoad() 

    configureViews() 
    } 

    func configureViews() { 
    //declares the label 
    //should hide the label 
    label.hidden = true 
    //next 2 lines center the label 
    label.center = CGPointMake(160, 284) 

    label.textAlignment = NSTextAlignment.Center 
    //puts text into the label 
    label.text = "This app was developed by me!" 
    } 


    //declares the function 
    @IBAction func function() { 
    // declares variable that stores the alert and it's properties 
    let alertController = UIAlertController(title: "Welcome!", message: "Here is some information about the developer!", preferredStyle: .Alert) 
    alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)) 
    self.presentViewController(alertController, animated: true, completion: nil) 
    label.hidden = false 
    } 
} 
関連する問題