私は、誰かの好きなことを表示するスウィフトコードを開発しました。だから私は1つのコードを1つの合計に好きな量を追加するwhileループに基づいています。次に、そのループの結果を含む短いフレーズを表示する関数を作成しました。今、私のビューコントローラでは、私は最終的に画面上のすべての時間のお気に入りを表示するために作成したラベルにその表示機能を実行したい。しかし、私はラベル上でdisplay関数をどのように実行し、最初にボタンをクリックしても表示されないようにする方法を理解できないようです。これはJavaの基本構文の経験があるだけで、コーディングの2日目です。ここでこの関数を配列に基づいて使用して、その配列の合計を別のクラスに表示するラベルを変更するにはどうすればよいですか?
は、最初のコード
import UIKit
class likesObject: NSObject {
// This is where I created the loop to solve for the total amount of likes
func complimentsLikeTotal() -> Void {
let monthlyLikes = [20, 15, 13, 32, 14, 38]
var totalLikes = 0
var runTotal = 0
while runTotal < monthlyLikes.count {
totalLikes += monthlyLikes[runTotal]
print(totalLikes)
runTotal += 1
}
}
/*
This is where I created the function to return how many all time likes there are in a phrase
I want to use this function in the viewcontroller code on the label
*/
func display() -> String {
return "\(complimentsLikeTotal) all time likes"
}
}
ここで、ビューコントローラのコード
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var inputLikesLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Here is where I don't know what to do with display and how to link it with the label
self.inputLikesLabel.text = display
}
}
問題はなんですか? "self.inputLikesLabel.text = display"はラベルのテキストを更新していませんか? – TNguyen