1.更改根控制器页面:
Button.init("返回") {
let screnDelegate: UIWindowSceneDelegate? = {
var uiScreen: UIScene?
UIApplication.shared.connectedScenes.forEach { (screen) in
uiScreen = screen
}
return (uiScreen?.delegate as? UIWindowSceneDelegate)
}()
screnDelegate?.window!?.rootViewController = UIHostingController(rootView: HomeView());
或者
UIApplication.shared.windows.first?.rootViewController = UIHostingController(rootView: HomeView())
}
2.push跳转
import SwiftUI
struct HomeView:View {
var body: some View{
return List{
ForEach(0..<3, id: \.self) { index in
if index==0{
NavigationLink(destination: one.init(name: "", age: 1)){
Text("\(index)")
}
}
if index==1{
NavigationLink(destination: Two()){
Text("\(index)")
}
}
}
}
}
}
push/pop:跳转的时候带参数,控制pop返回
import SwiftUI
struct HomeView:View {
@State var isShow = false
var body: some View{
//isActive是控制是否自动跳转,false不自动跳转,跳转到下一个页面后isShow就变成了true
NavigationLink(destination: one(showing:$isShow), isActive: self.$isShow) {
Text("one")
}
}
}
pop返回:
mport SwiftUI
struct one: View {
@Binding var showing :Bool
var body: some View {
Button("点击返回") {
self.showing = false//这个控制pop返回
}
}
}
pop到根视图:在SwiftUI下实现popToRootViewController()返回根视图 - 知乎
present:(fullScreenCover)
import SwiftUI
struct HomeView:View {
@State var isPresented = false
var body: some View{
Button("present"){
self.isPresented = true
}.fullScreenCover(isPresented: $isPresented) {
print("消失")
} content: {
Two()
}
}
}
import SwiftUI
struct Two: View {
@Environment(\.presentationMode) var presentationModess
var body: some View {
Text("Hello,two")
Button("返回"){
self.presentationModess.wrappedValue.dismiss()//返回的方法
}
}
}
sheet:(和present类似,只需改变fullScreenCover为sheet)
import SwiftUI
struct HomeView:View {
@State var isPresented = false
var body: some View{
Button("sheet"){
self.isPresented = true
}.sheet(isPresented: $isPresented) {//这个是页面消失后的回调
print("消失")
} content: {
Two()
}
}
}
present返回
import SwiftUI
struct Two: View {
//该视图将创建一个名为presentationMode的属性,该属性附加到存储在应用程序环境中的演示模式变量中
@Environment(\.presentationMode) var presentationModess
var body: some View {
Text("Hello,two")
Button("返回"){
//需要在其中添加wrapdValue,因为presentationMode实际上是一个绑定,因此它可以由系统自动更新——我们需要在其中进行解包以检索实际的呈现方式,以关闭视图。
self.presentationModess.wrappedValue.dismiss()//返回的方法
}
}
}
alert:提示框,最多两个按钮
import SwiftUI
struct HomeView:View {
@State var showAlert = false
var body: some View{
Button("alert"){
self.showAlert = true
}.alert(isPresented: $showAlert){
// Alert(title: Text("one"), message: Text("two"), dismissButton: .default(Text("ok")))
return Alert(title: Text("one"), message: Text("one"), primaryButton: .cancel(Text("取消"), action: {
print("取消")
}), secondaryButton:.default(Text("确定"), action: {
print("确定")
}))
}
}
}
actionsheet:提示框,可以有多个按钮
import SwiftUI
struct HomeView:View {
@State var isPresented = false
var body: some View{
Button("present"){
self.isPresented = true
}.actionSheet(isPresented: $isPresented) {
ActionSheet(title: Text("Change background"), message: Text("Select a new color"), buttons: [
.default(Text("Red")) { print("Red") },
.default(Text("Green")) { print("green") },
.default(Text("Blue")) {print("blue") },
.cancel()
])
}
}
}
跳转到safari浏览器:必须要加上http
Link(destination: URL(string: "https://www.baidu.com")!) {
Text("百度搜索")
}