UIKit Framework - Snippets: Unterschied zwischen den Versionen
Aus Wikizone
| Zeile 2: | Zeile 2: | ||
[[UIKit Framework]] | [[UIKit Framework]] | ||
| − | == | + | == View programmatisch erstellen == |
| + | So kann man einen Screen nur mit Code erstellen und diesen vom Hauptview aus aufrufen. | ||
| + | |||
| + | SecondViewController.swift | ||
<syntaxhighlight lang="Swift"> | <syntaxhighlight lang="Swift"> | ||
import UIKit | import UIKit | ||
| Zeile 21: | Zeile 24: | ||
} | } | ||
} | } | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | ViewController.swift | ||
| + | <syntaxhighlight lang="Swift"> | ||
| + | //... | ||
| + | @IBAction func calculatePressed(_ sender: UIButton) { | ||
| + | let height = round(100 * heightSlider.value)/100 | ||
| + | let weight = round(weightSlider.value) | ||
| + | let bmi = Int( weight / pow(height,2) ) | ||
| + | |||
| + | // show result on SecondViewController | ||
| + | let secondVC = SecondViewController() | ||
| + | secondVC.bmiValue = String(format: "%d", bmi) | ||
| + | self.present(secondVC, animated: true, completion: nil) | ||
| + | } | ||
| + | //... | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Version vom 4. Januar 2023, 11:38 Uhr
Links
UIKit Framework
View programmatisch erstellen
So kann man einen Screen nur mit Code erstellen und diesen vom Hauptview aus aufrufen.
SecondViewController.swift
import UIKit
class SecondViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .red //Shortcut for UIColor.red
let label = UILabel()
label.text = "Hello"
label.frame = CGRect(x: 0, y: 0, width: 100, height: 50)
// add the label to the view. addSubview needs a view as param. As UILabel inherits from UIView it's possible
view.addSubview(label)
}
}
ViewController.swift
//...
@IBAction func calculatePressed(_ sender: UIButton) {
let height = round(100 * heightSlider.value)/100
let weight = round(weightSlider.value)
let bmi = Int( weight / pow(height,2) )
// show result on SecondViewController
let secondVC = SecondViewController()
secondVC.bmiValue = String(format: "%d", bmi)
self.present(secondVC, animated: true, completion: nil)
}
//...