当前位置:网站首页>[SwiftUI 开发] @State @Binding @ObservedObject @EnvironmentObject
[SwiftUI 开发] @State @Binding @ObservedObject @EnvironmentObject
2022-07-29 11:03:00 【文件夹_IOS】
@State
@State属性包装器, struct的属性是不可以改变的,在SwiftUI中如果想改变属性,需要加上@State。而且当 @State 装饰过的属性发生了变化,SwiftUI 会根据新的属性值重新创建视图
struct ContentView: View {
@State var title = 0
var body: some View {
Button {
title = title + 1
} label: {
Text("\(title)")
}
.font(.system(size: 38))
.foregroundColor(.white)
.frame(width: 88, height: 88)
.background(Color.yellow)
.cornerRadius(44)
}
}
@Binding
@Binding属性修饰器, 在 Swift 中值的传递形式是值类型传递方式。但是通过 @Binding 修饰器修饰后,属性变成了一个引用类型,传递变成了引用传递,这样父子视图的状态就能关联起来了。
struct ContentView: View {
@State var title = 0
var body: some View {
UserButton(title: $title) {
title = title + 1
}
}
}
struct UserButton: View {
@Binding var title: Int
let action: () -> Void
var body: some View {
Button(action: action) {
Text("\(title)")
.font(.system(size: 38))
.foregroundColor(.white)
.frame(width: 88, height: 88)
.background(Color.yellow)
.cornerRadius(44)
}
}
}效果和上图一样
@ObservedObject
@Published
@ObservedObject 和@Published 相当于KVO注册监听,@ObservedObject修饰一个对象,那就是注册了监听, @Published修饰一个对象,表示这个对象可以被监听。这俩是一对。
想要使用@ObservedObject,必须实现ObservableObject 协议。
struct ContentView: View {
@ObservedObject var person: Person
var body: some View {
VStack{
Text(person.name)
Button{
person.name = "韩梅梅"
} label: {
Text(person.name)
.font(.system(size: 38))
.foregroundColor(.white)
.frame(width: 188, height: 88)
.background(Color.yellow)
.cornerRadius(10)
}
}
}
}
class Person: ObservableObject {
@Published var name: String = "小明"
}
@EnvironmentObject
@EnvironmentObject 环境变量,创建对象时,通过.environmentObject(person())传值。在它所有的子界面都可以用@EnvironmentObject来接收person对象。它是通过Environment查找person对象的
* 值得注意的是,push到另一个界面是不行的,必须要在同一个界面,因为push到的这个新View,不属于它的子View
let person = Person()
ContentView().environmentObject(person)
struct ContentView: View {
@EnvironmentObject var person: Person
var body: some View {
VStack{
ContentView2()
Button{
person.name = "韩梅梅"
} label: {
Text(person.name)
.font(.system(size: 38))
.foregroundColor(.white)
.frame(width: 188, height: 88)
.background(Color.yellow)
.cornerRadius(10)
}
}
}
}
struct ContentView2: View {
@EnvironmentObject var person: Person
var body: some View {
VStack{
Button{
person.name = "李雷"
} label: {
Text(person.name)
.font(.system(size: 38))
.foregroundColor(.white)
.frame(width: 188, height: 88)
.background(Color.yellow)
.cornerRadius(10)
}
}
}
}
@Environment
根据@Environment我们不仅可以拿到自定义的属性,还可以拿到系统属性
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
// 比如我们需要dismiss时,就需要拿到系统presentationMode对象
presentationMode.wrappedValue.dismiss()
// 可以拿到系统ColorScheme对象
@Environment(\.colorScheme) var colorScheme: ColorScheme边栏推荐
- Gbase8s core data backup
- Steps of project explanation in interview
- Pyqt5 rapid development and practice 6.6 qformlayout & 6.7 nested layout & 6.8 qsplitter
- Hugo NexT V4 介绍
- ES6-箭头函数this指向
- matplotlib中文问题
- VMware: use commands to update or upgrade VMware esxi hosts
- Niuke net brush questions
- 多线程顺序运行的 4 种方法,面试随便问!
- Spark efficient data analysis 01. Establishment of idea development environment
猜你喜欢

【图像检测】基于灰度图像的积累加权边缘检测方法研究附matlab代码

How to use grep to find pattern matching across multiple lines

Pytorch 入门

How to use grep to display file names and line numbers before matching lines

PHP basics uses arrays to save data

Meituan and hungry were interviewed by Hangzhou supervisors to implement the responsibility of food safety management and prohibit malicious competition

开源峰会抢先看 | 7 月 29 日分论坛 & 活动议程速览
Xiaoxiao authorization system V5.0 happy version

Conference OA project - my approval

Site data collection -scrapy usage notes
随机推荐
如何开始为您的 Kubernetes 应用程序编写 Helm 图表
开放原子开源基金会秘书长孙文龙 | 凝心聚力,共拓开源
Function comparison between report control FastReport and stimulus soft
Starrocks technology insider: how to have both real-time update and fast query
Meeting OA project (V) -- meeting notice and feedback details
LeetCode_278_第一个错误的版本
How to realize the function of adding watermark
2022 latest WiFi master applet independent version 3.0.8
基于flask写的一个小商城mall项目
深入理解C# 进入快速通道的委托
matplotlib中文问题
2022最新 wifi大师小程序独立版3.0.8
为什么应该在开发环境中使用 Kubernetes
Basic construction of QT project
开源峰会抢先看 | 7 月 29 日分论坛 & 活动议程速览
The heavyweight foundation awarded platinum, gold and silver donors
leetcode-位运算
聊聊性能测试环境搭建
Software testing dry goods
阿里P8爆出的这份大厂面试指南,看完工资暴涨30k!