gpt4 book ai didi

SwiftUI 如何在选项卡单击时刷新出现

转载 作者:行者123 更新时间:2023-12-04 08:04:50 24 4
gpt4 key购买 nike

我有 5 个选项卡,每个选项卡都是不同的 View ,并且加载出现。一旦标签被加载 snapper 我怎样才能让它再次重新加载。我是 SwiftUI 的新手,无法完成。所以基本上我有一个带有 5 个选项卡的 TabView View ,其中一个选项卡称为 首页 单击 home 时,它​​会调用另一个名为 TimeLineView 的 View ,该 View 从数据库中获取 OnAppear 数据。由于它是 OnAppear,它第一次工作,但在随后的 taps 中不会再次被调用。我想改变它,以便在每次点击时我都可以调用 OnAppear 或调用 OnAppear 中的函数。任何建议都会很棒

struct TabView : View {
@State var model: [MainModel]
@State var selectedTab = "Home"
var edges = UIApplication.shared.windows.first?.safeAreaInsets
@Namespace var animation
@StateObject var modelData = MainViewModel.ModelView()
@State var IsMainView: MainViewEnum = MainViewEnum.MainView
@Binding var mainModelValue: MainModelValue

var body: some View {

VStack(spacing: 0){

GeometryReader{_ in

ZStack{

TimeLineView(model: model, isMainView: $IsMainView, newValue: mainModelValue, UserID: 0) // I would like to refresh this on every tap click
.opacity(selectedTab == "Home" ? 1 : 0)

RankingView()
.opacity(selectedTab == "Ranking" ? 1 : 0)

TypeCategoryView()
.opacity(selectedTab == "Post" ? 1 : 0)

MenuView()
.opacity(selectedTab == "Menu" ? 1 : 0)

InboxView()
.opacity(selectedTab == "Inbox" ? 1 : 0)

}
}
.onChange(of: selectedTab) { (_) in

switch(selectedTab){

case "Ranking": if !modelData.isRankingView{modelData.GetRankingView()}
case "Post": if !modelData.isOrderLoad{modelData.loadOrders()}
case "Menu": if !modelData.isRewardLoad{modelData.loadReward()}
case "Inbox": if !modelData.InboxView{modelData.InboxLoad()}
default: ()
}
}
HStack(spacing: 0){

ForEach(tabs,id: \.self){ tab in

TabButton(title: tab, selectedTab: $selectedTab,animation: animation)

if tab != tabs.last{
Spacer(minLength: 0)
}
}
}

}
}
}



struct TimeLineView: View {
@State var model: [MainModel]
@Binding var isMainView: MainViewEnum
@State var shareSheet = false
@State var PostIDState = 0
@State var sharePost: String?
@State var ProfileID: String?
@State var FullNameClicked = false
@State var FullName = ""
@State var newValue = MainModelValue()
@State var UserID: Int

var body: some View {
ZStack
{
Color(UIColor.white)
.ignoresSafeArea()

ScrollView(showsIndicators: false) {
LazyVStack {

if isMainView == MainViewEnum.MainView {
TestView(model: $model)
}
let result = model.sorted {
$0.id! > $1.id!
}

ForEach(result) { value in

Spacer()
VStack(alignment: .leading) {
HStack {

Text("\(value.initials!)")
.fontWeight(.bold)
.frame(width: 35, height: 35, alignment: .center)
.foregroundColor(Color(SystemColorPicked(picked: value.color)))
.padding()
.overlay(
Circle()
.stroke(Color.black, lineWidth: 1)
.padding(6)
)

VStack(alignment: .leading, spacing: 1) {
HStack {
Text("\(value.fullname)")
.font(.system(size: 15))
.bold()
.onTapGesture {
FullNameClicked.toggle()
FullName = value.fullname
ProfileID = String(value.profile_id)
}
Spacer()

}.padding(.leading, 13)

}
}

VStack(alignment: .leading, spacing: 1) {
HStack {

if value.post.count >= 100 {


PostShowView(text: value.post)

} else {
Text("\(value.post)")
.fixedSize(horizontal: false, vertical: true)
.font(.system(size: 15))
}

}.padding(.leading, 90)

Spacer()

}.padding(.leading, 90)

VStack {
Spacer()
Divider()
}.padding(.leading, 90)

}

}
}.padding(25.0)
}.padding(.top, 2.0)

}
.listRowInsets(.none)
.onAppear(){
// I would like this to execute every time this View is called
MainViewModel().MainViewRequests(46, model: newValue) { data in
self.model = data!

}

}
}
}

最佳答案

试试 DeferView (取自我在 https://stackoverflow.com/a/61242931/12299030 中的回答),例如

DeferView  {
TimeLineView(model: model,
isMainView: $IsMainView,
newValue: mainModelValue,
UserID: 0)
}.opacity(selectedTab == "Home" ? 1 : 0)

关于SwiftUI 如何在选项卡单击时刷新出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66272063/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com