我刚刚完成了我制作的一个简单Android应用程序的SwiftUI端口(感谢这里的所有人的帮助),但是它不会在画布中预览。它内置在模拟器中,但在我的手机上还不错。
这与在.onAppear中调用我的数据库选择功能有关,因为当我注释掉这部分代码时,它可以正常工作。
import SwiftUI
import AVFoundation
struct SoundPageView: View {
var category: String
var pageTitle: String
var buttonRows = [ButtonData]()
@State var btns: [[String]] = [[]]
@State var translation: String = "Bahasa Indonesia"
@State var gotValues: Bool = false
var btn1: String = "First Button"
var btn2: String = "Second Button"
var btn3: String = "Third Button"
var btn4: String = "Fourth Button"
var btn5: String = "Fifth Button"
var btn6: String = "Sixth Button"
var btn7: String = "Seventh Button"
var btn8: String = "Eighth Button"
var btn9: String = "Ninth Button"
var btn10: String = "Tenth Button"
var btn11: String = "Eleventh Button"
var btn12: String = "Twelfth Button"
var btn13: String = "Thirteenth Button"
var btn14: String = "Fourteenth Button"
var btn15: String = "Fifteenth Button"
var btn16: String = "Sixteenth Button"
var body: some View {
VStack {
Text("\(self.translation)")
// .italic()
.frame(maxWidth: .infinity)
.multilineTextAlignment(.center)
.frame(height: 50)
.padding(.horizontal, 4)
HStack(spacing:0) {
VStack {
Button(action: {
playSound(sound: "\(self.category)_audio1", type: "mp3")
self.translation = self.btns[1][1]
}) {
if(gotValues==true) {
Text("\(self.btns[1][0])")
} else {
Text("\(self.btn1)")
}
}
.buttonStyle(SoundButton())
Button(action: {
playSound(sound: "\(self.category)_audio3", type: "mp3")
self.translation = self.btns[3][1]
}) {
if(gotValues==true) {
Text("\(self.btns[3][0])")
} else {
Text("\(self.btn3)")
}
}
.buttonStyle(SoundButton())
Button(action: {
playSound(sound: "\(self.category)_audio5", type: "mp3")
self.translation = self.btns[5][1]
}) {
if(gotValues==true) {
Text("\(self.btns[5][0])")
} else {
Text("\(self.btn5)")
}
}
.buttonStyle(SoundButton())
Button(action: {
playSound(sound: "\(self.category)_audio7", type: "mp3")
self.translation = self.btns[7][1]
}) {
if(gotValues==true) {
Text("\(self.btns[7][0])")
} else {
Text("\(self.btn7)")
}
}
.buttonStyle(SoundButton())
Button(action: {
playSound(sound: "\(self.category)_audio9", type: "mp3")
self.translation = self.btns[9][1]
}) { if(gotValues==true) {
Text("\(self.btns[9][0])")
} else {
Text("\(self.btn9)")
}
}
.buttonStyle(SoundButton())
Button(action: {
playSound(sound: "\(self.category)_audio11", type: "mp3")
self.translation = self.btns[11][1]
}) {
if(gotValues==true) {
Text("\(self.btns[11][0])")
} else {
Text("\(self.btn11)")
}
}
.buttonStyle(SoundButton())
Button(action: {
playSound(sound: "\(self.category)_audio13", type: "mp3")
self.translation = self.btns[13][1]
}) {
if(gotValues==true) {
Text("\(self.btns[13][0])")
} else {
Text("\(self.btn13)")
}
}
.buttonStyle(SoundButton())
Button(action: {
playSound(sound: "\(self.category)_audio15", type: "mp3")
self.translation = self.btns[15][1]
}) {
if(gotValues==true) {
Text("\(self.btns[15][0])")
} else {
Text("\(self.btn15)")
}
}
.buttonStyle(SoundButton())
}
VStack {
Button(action: {
playSound(sound: "\(self.category)_audio2", type: "mp3")
self.translation = self.btns[2][1]
}) {
if(gotValues==true) {
Text("\(self.btns[2][0])")
} else {
Text("\(self.btn2)")
}
}
.buttonStyle(SoundButton())
Button(action: {
playSound(sound: "\(self.category)_audio4", type: "mp3")
self.translation = self.btns[4][1]
}) {
if(gotValues==true) {
Text("\(self.btns[4][0])")
} else {
Text("\(self.btn4)")
}
}
.buttonStyle(SoundButton())
Button(action: {
playSound(sound: "\(self.category)_audio6", type: "mp3")
self.translation = self.btns[6][1]
}) {
if(gotValues==true) {
Text("\(self.btns[6][0])")
} else {
Text("\(self.btn6)")
}
}
.buttonStyle(SoundButton())
Button(action: {
playSound(sound: "\(self.category)_audio8", type: "mp3")
self.translation = self.btns[8][1]
}) {
if(gotValues==true) {
Text("\(self.btns[8][0])")
} else {
Text("\(self.btn8)")
}
}
.buttonStyle(SoundButton())
Button(action: {
playSound(sound: "\(self.category)_audio10", type: "mp3")
self.translation = self.btns[10][1]
}) {
if(gotValues==true) {
Text("\(self.btns[10][0])")
} else {
Text("\(self.btn10)")
}
}
.buttonStyle(SoundButton())
Button(action: {
playSound(sound: "\(self.category)_audio12", type: "mp3")
self.translation = self.btns[12][1]
}) {
if(gotValues==true) {
Text("\(self.btns[12][0])")
} else {
Text("\(self.btn12)")
}
}
.buttonStyle(SoundButton())
Button(action: {
playSound(sound: "\(self.category)_audio14", type: "mp3")
self.translation = self.btns[14][1]
}) {
if(gotValues==true) {
Text("\(self.btns[14][0])")
} else {
Text("\(self.btn14)")
}
}
.buttonStyle(SoundButton())
Button(action: {
playSound(sound: "\(self.category)_audio16", type: "mp3")
self.translation = self.btns[16][1]
}) {
if(gotValues==true) {
Text("\(self.btns[16][0])")
} else {
Text("\(self.btn16)")
}
}
.buttonStyle(SoundButton())
}
}
}
.onAppear {
let buttonRows = DatabaseHelper().queryDatabase(category: self.category)
for each in buttonRows {
self.btns.append([each.english,each.indonesian])
}
self.gotValues = true
}
.navigationBarTitle(Text(self.pageTitle), displayMode: .inline)
}
}
struct SoundPageView_Previews: PreviewProvider {
static var previews: some View {
SoundPageView(category: "", pageTitle: "")
}
}
struct SoundButton: ButtonStyle {
func makeBody(configuration: Self.Configuration) -> some View {
configuration.label
.font(.custom("arial", size: 20))
.padding(.horizontal, 4).minimumScaleFactor(0.9)
.foregroundColor(Color.black)
.multilineTextAlignment(.center)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(configuration.isPressed ? LinearGradient(gradient: Gradient(colors: [Color("DarkestGreen"), Color("LightGreen")]), startPoint: .top, endPoint: .bottom):LinearGradient(gradient: Gradient(colors: [Color("DarkGreen"), Color("LightGreen")]), startPoint: .top, endPoint: .bottom))
.cornerRadius(10)
.shadow(radius: 3.0)
.overlay(
RoundedRectangle(cornerRadius: 10)
.stroke(Color.white, lineWidth:2))
.padding(2)
.scaleEffect(configuration.isPressed ? 0.975 : 1.0)
}
}
这是我在画布中收到的错误消息。
理想情况下,我想在.onAppear之前调用数据库函数,但不确定将其放在何处。当我创建一个init()时,会引发很多错误。就像我说的那样,应用程序中的所有内容都可以正常运行,我只是不希望崩溃错误成为我的应用程序被Apple拒绝的原因。对于我的学生来说,这只是一个免费的应用程序,非常简单。