I have this script:
List {
//code
}.presenation($displayAlert) {
Alert(title: Text("Start1"), message: Text("other...."), dismissButton: .default(Text("Go!")))
}
I receive error:
"Protocol type 'Any' cannot conform to 'View' because only concrete types can conform to protocols"
I think that the .presentation is deprecate on Version 11.0 (11A420a)
How can I fix this error?
Thank you!
To show an alert you need to use the .alert modifier as the .presentation modifier was deprecated in Beta 4.
Here is a quick example showing how to use it.
struct ContentView: View {
#State var showAlert = false
var body: some View {
List {
Button(action: {
self.showAlert.toggle()
}) {
Text("press me")
}
}.alert(isPresented: $showAlert) {
Alert(title: Text("Title"), message: Text("Message"), dismissButton: .default(Text("OK!")))
}
}
}
You may also want to consider updating your version of Xcode as 11.2 was released today
Related
In the new version of iOS and Xcode
NavigationLink(isActive: , destination: , label: )
is deprecated.
How do we control the status of NavigationLink then?
Old/Deprecated way to navigate:
#State private var readyToNavigate : Bool = false
var body: some View {
NavigationView {
VStack {
NavigationLink(destination: MyTargetView(), isActive: $readyToNavigate, label: {Text("Navigate Link")})
Button {
//Code here before changing the bool value
readyToNavigate = true
} label: {
Text("Navigate Button")
}
}
.navigationTitle("Navigation")
}
}
New way to navigate
#State private var readyToNavigate : Bool = false
var body: some View {
NavigationStack {
VStack {
Button {
//Code here before changing the bool value
readyToNavigate = true
} label: {
Text("Navigate Button")
}
}
.navigationTitle("Navigation")
.navigationDestination(isPresented: $readyToNavigate) {
MyTargetView()
}
}
}
Use new NavigationLink(value:label:) as specified. By putting corresponding value into NavigationPath you activate a link.
See more for example in this topic
Check out Apple's migration docs:
https://developer.apple.com/documentation/swiftui/migrating-to-new-navigation-types
The isActive implementation of NavigationLink is deprecated in iOS 16. Instead, you should use the new NavigationLink(value:) along with .navigationDestination with NavigationStack.
I'm trying the iOS 16 beta, MacOS 13 beta, and Xcode 14 beta. I can't get TextField to edit a property within a #Binding struct. It works with a #State struct variable, but not binding. Anyone else seeing this problem?
I filed a Feedback with Apple a couple of days ago, but no response yet. I imagine they are swamped the week after WWDC.
MacOS Version 13.0 Beta (22A5266r)
Xcode Version 14.0 beta (14A5228q)
Here's a code example of the problem.
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationStack {
List {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundColor(.accentColor)
Text("Hello, world!")
}
/// Click on Category List
NavigationLink {
CatList()
} label: {
Text("Category List")
}
}
}
}
}
struct CatList: View {
/// Click on bar
#State var catArray: [Categories] = [ Categories(id: UUID(), myName: "foo"), Categories(id: UUID(), myName: "bar")]
var body: some View {
List {
ForEach($catArray) { $eachCat in
NavigationLink {
CatDetails(thisCategory: $eachCat)
} label: {
Text(eachCat.myName)
}
}
}
}
}
struct CatDetails: View {
#Binding var thisCategory: Categories
var body: some View {
Form {
/// Cannot edit bar.
TextField("Name:", text: $thisCategory.myName)
}
}
}
struct Categories: Identifiable, Hashable {
var id: UUID
var myName: String
}
I came across some issues with my SwiftUI code.
I made a simple example.
Just a Button that opens a Sheet.
struct ContentView: View {
#State private var showSheet = false
var body: some View {
Button(action: {
showSheet.toggle()
}, label: {
Text("Button")
})
.sheet(isPresented: $showSheet) {
SheetView(show: $showSheet, selectedDate: .constant(nil))
}
}
}
The Sheet has an optional Binding.
You can close it via the mark button.
However, as soon as I use an #Environment wrapper, the xmark stops working.
struct SheetView: View {
#Binding var show: Bool
#Environment(\.colorScheme) var colorScheme
#Binding var selectedDate: Date?
var body: some View {
NavigationView {
Text("Hello, \(selectedDate?.description ?? "Welt")!")
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
Button(action: {
self.show = false
}) {
Image(systemName: "xmark")
.renderingMode(.original)
.accessibilityLabel(Text("Save"))
}
}
}
}
}
}
(This seems to be due to the view constantly refreshing itself?)
In addition: This only happens on my iPhone 12 Pro Max.
In the simulator or an iPhone 11 Pro Max it is working fine.
(Don't have any other devices to test on.)
If I don't use .constant(nil) but give it a $Date, it works just fine.
If I remove the #Environment, it also works.
Am I doing something wrong here?
What is my mistake?
I am trying to define the following View in SwiftUI but it is not working:
struct ContentView: View {
var body: some View {
Text("Placeholder")
Button(action: {
// Do something
}) {
Text("Button")
}
}
}
The error is :
Function declares an opaque return type, but has no return statements in its body from which to infer an underlying type
There are also two warnings:
Result of 'Text' initializer is unused
and
Result of 'Button<Label>' initializer is unused
I am trying to code with XCode11 on Mac with OS Catalina. Does anybody know what the problem is?
You forgot to add mentioned VStack
var body: some View {
VStack { // << here !!
Text("Placeholder")
Button(action: {
// Do something
}) {
Text("Button")
}
}
}
You have missed the VStack :
var body: some View {
VStack {
Text("Placeholder")
Button("Button"){ }
}
}
I am making Tic Tac Toe using swiftUI and in order to give a alert who wins or there is a tie and with tapping on 'Ok' alert will dismiss
Anyone up there please help me to resolve this ??
This is the actual error
use .alert instead of .presentation
struct ContentView: View {
#State private var showingAlert = false
var body: some View {
Button(action: {
self.showingAlert = true
}) {
Text("Show Alert")
}
.alert(isPresented: $showingAlert) {
Alert(title: Text("Important message"), message: Text("Wear sunscreen"), dismissButton: .default(Text("Got it!")))
}
}
}