0%

SwiftUIスタート

开始。

  1. mutating 特别 SwiftUI
    1. @State @Binding
    2. @propertyWrapper ??
    3. @ObservedObject
    4. @Environment
    5. @Published
    6. @StateObject @ObservedObjec
  2. 问题
  3. 参考

mutating 特别 SwiftUI

@State @Binding

mvc model-view-controller 转变为 view-state 形式。
@State 内部有 getter setter,它的值改变会触发 view 的刷新
@Binding 把值属性转变成引用属性,这样改变就能向外传递

问题

  1. 只能在单个View及其子层级用
  2. 只能在body里用
  3. 不能在外部改变 state 的值
class a {
@state x:Int
demo(first:$x)
}

class b {
@Binding y:Int
func demo(first:Binding<Int>) {}
}

@propertyWrapper ??

@ObservedObject

鉴于 state 的局限性,可选操作就是这里。

  1. class 实现这个协议,只有一个需要实现的属性 objectWillChange
  2. 这个属性有个 send 方法可以通知事件发生,即可以刷新页面。比如 var b = some(0) { willSet { objectWillChange.send() }}
  3. 可以用 @Published 来替代繁琐的 objectWillChange 属性实现的相关功能

@Environment

@Published

@StateObject @ObservedObjec

问题

2021-05-01
添加 GeometryReader 前后,表现出现不一致
添加前,居中
添加后,左上角

var body: some View {
GeometryReader { geometry in
let sizeW = geometry.size.width //* 0.5
let paddingW = sizeW //* 0.5
Image("\(item.name)")
.resizable()
.frame(width: 50, height: 50, alignment: .center)
.clipShape(Circle())
.scaledToFit()
.background(Color.purple)
// .padding(paddingW)
}
}

2021-03-26
navigationview 内嵌在 tabview 时候,用 navigationlink 跳转后,tabbar 无法隐藏;
必须反过来,tabview 内嵌在 navigationview 才行,但这个时候会出现 Trying to pop to a missing destination at

2021-03-24
FetchRequst 竟然没有分页的限制,还需要 NSFetchRequest 来构造

2021-03-21
修改var的值 Cannot assign to property: 'self' is immutable
Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols

2021-03-19
core data fetch 还不能方便修改 predicate
navigationview 还没有 stack management,导致不能自己构建 nav 的 stack
Picker 的 onchange 不能正常行为, 用了onrecive来实现

参考

swiftui apple tutorials
swift language
Heads up: a SwiftUI navigation destination is not lazy.
Implementing complex navigation stack in SwiftUI and The Composable Architecture
Using iOS 14’s Menu as a Picker in SwiftUI
Dynamic Predicates with Core Data in SwiftUI
SwiftUI Picker onChange or equivalent?
How to migrate from SwiftUI to UIKit App Delegate Life Cycle in Xcode
Build a Custom iOS Segmented Control With SwiftUI
TabsSwitUIExample
Using CoreData with SwiftUI like mvvm
Fetching objects from Core Data in a SwiftUI project
SwiftUI pagination for List object
Reusable & Generic Database Layer
Network-and-DB-layer-Swift
Preventing unwanted fetches when using NSFetchedResultsController and fetchBatchSize
Implementing an infinite scrolling list with SwiftUI and Combine
Build an Endless Scrolling List With SwiftUI, Combine, and URLSession

edit menus
Adding Speech to custom UIMenuController
Commands in SwiftUI
From Old to New: How To Use Existing UIKit Views in SwiftUI
TextField and UIViewRepresentable

UIStackView: Distribution vs. Alignment