This comment was a mistake
Post
Replies
Boosts
Views
Activity
I found a solution to this issue.
It needs to right-click the Exit icon. Then, by the left-click, drag the connection from the circle of the unwind method to the button on the screen. And in another window that appears, click action.
And after clicking the button, a transition to the target view controller occurs, as well as data passing.
Thanks for your reply!
Do I understand correctly that if it were a structure, not a class, then there would be no such reference?
I have added the
myView.translatesAutoresizingMaskIntoConstraints = false
code line to my code.
import UIKit
class ViewController: UIViewController {
var myView = UIView()
override func viewDidLoad() {
super.viewDidLoad()
myView.frame.size.width = 100
myView.frame.size.height = 100
myView.backgroundColor = .green
view.addSubview(myView)
myView.translatesAutoresizingMaskIntoConstraints = false
let margins = view.layoutMarginsGuide
myView.trailingAnchor.constraint(equalTo: margins.trailingAnchor).isActive = true
}
}
But now. i get a blank screen.
Why is this happening?
Thank you so much guys! I appreciate your help!
Thank you very very much!
It works!
There are many good tutorials and sample codes written for the released version of SwiftUI. You should better find a good one unless you want to study how the beta version of SwiftUI was.
Yes, you are right! I just want to go to the end in this tutorial)) And then go to the released version. And the second reason that the basics are well shown here, the main ideas of SwiftUI. When I see the general idea, it is easier for me to understand the specific methods.
Thanks a lot again!
ok, thanks for helping. Below is the complete project code for files. At the very end, there is a link to the full project in the archive.
This is a RoomsApp.swift file code:
import SwiftUI
@main
struct RoomsApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
This is ContentView.swift code:
import SwiftUI
struct ContentView: View {
@ObservedObject var store = RoomStore()
var body: some View {
NavigationView {
List {
Button(action: addRoom) {
Text("Add Room")
}
ForEach(store.rooms) { room in
RoomCell(room: room)
}
}
.navigationBarTitle(Text("Rooms"))
}
}
func addRoom() {
store.rooms.append(Room(name: "Hall 2", capacity: 2000))
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView(store: RoomStore(rooms: testData))
}
}
struct RoomCell: View {
var room: Room
var body: some View {
NavigationLink(destination: RoomDetail(room: room)) {
Image(room.thumbnailName)
.cornerRadius(8)
VStack(alignment: .leading) {
Text(room.name)
Text("\(room.capacity) people")
.font(.subheadline)
.foregroundColor(.secondary)
}
}
}
}
RoomDetail.swift file code:
import SwiftUI
struct RoomDetail: View {
var room: Room
@State private var zoomed = false
var body: some View {
ZStack(alignment: .topLeading) {
Image(room.imageName)
.resizable()
.aspectRatio(contentMode: zoomed ? .fill : .fit)
.navigationBarTitle(room.name, displayMode: .inline )
.onTapGesture { // Instead of TapAction
withAnimation(.easeIn(duration: 1)) { zoomed.toggle() } // .easyIn is instead of basic(duration)
}
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
if room.hasVideo && !zoomed {
Image(systemName: "video.fill")
.font(.title)
.padding(.all)
.transition(.move(edge: .leading))
}
}
}
}
struct RoomDetail_Previews: PreviewProvider {
static var previews: some View {
Group {
NavigationView{RoomDetail(room: testData[0])}
NavigationView{RoomDetail(room: testData[1])}
}
}
}
Room
import SwiftUI
struct Room: Identifiable {
var id = UUID()
var name: String
var capacity: Int
var hasVideo: Bool = false
var imageName: String {
return name
}
var thumbnailName: String {
return name + "Thumb"
}
}
let testData = [
Room(name: "room-1", capacity: 6, hasVideo: true),
Room(name: "room-2", capacity: 8, hasVideo: false),
Room(name: "room-3", capacity: 16, hasVideo: true),
Room(name: "room-4", capacity: 10, hasVideo: true),
Room(name: "room-5", capacity: 12, hasVideo: false),
Room(name: "room-6", capacity: 8, hasVideo: false),
Room(name: "room-7", capacity: 10, hasVideo: true),
Room(name: "room-8", capacity: 7, hasVideo: false),
Room(name: "room-9", capacity: 11, hasVideo: false)
]
RoomStore
import SwiftUI
import Combine
class RoomStore: ObservableObject { // Instead of BindableObject
var rooms: [Room] {
didSet { didChange.send() }
}
init(rooms: [Room] = []) {
self.rooms = rooms
}
var didChange = PassthroughSubject<Void, Never>()
}
This is a link to the complete project in the archive.
https://drive.google.com/file/d/1Sis3r57S9n9Uf4Pd8dFSyMoM-Z0vsj9K/view?usp=sharing
Yes, I understand that the language has changed, and I even tried to find these changes, but it did not work out. That's why I asked a question on this forum. Perhaps there is someone here who has done this tutorial and can say what has changed?
Thanks a lot guys! I really appreciate your help!
Уes, thanks for the help. Now I realized that the whole point is in this line of code:
item.isSelected.toggle()
Here, the value is not checked, here it is changed.
Let me ask one last question on this topic: if foods were class, would that make a difference? In fact , a class is a reference type and if we write such code
var item = foods[indexPath.row]
then it turns out that we are passing the link and the above line should perform the change.
item.isSelected.toggle()
Is that right?
You wrote:
"Unless you call reloadRows(at:with:) or reloadData() (generally, reloadData() is too much just for updating a single cell), UITableView would not update the cell, meaning tableView(_:cellForRowAt:) would not be called."
I understand that. My question is about something else - why is the same operation performed differently?
Inside the cellForRowAt method, I can create a separate constant to hold the item from array and then check its isSelected property.
let food = foods[indexPath.row]
if food.isSelected {...
But inside the didSelectRowAt method, this code does not work and I need to retrieve an item from the original array on the fly to check its isSelected property.
if foods[indexPath].isSelected...
What could be the reason here? Any ideas please
P.S.
But why is this happening? After all, next to it, in the cellForRowAt method, the same previous code is used:
...
let food = foods[indexPath.row]
...
// Assign checkmark
if food.isSelected {
cell.accessoryType = .checkmark
} else {
cell.accessoryType = .none
}
...
And it works.
Any ideas on this point, please.
Thank you so much!
I made a following code:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// Create variable and check it.
guard let cell = tableView.cellForRow(at: indexPath) else { return }
//Clean cell format before select food
cell.accessoryType = .none
// Set checkmark
if foods[indexPath].isSelected == false {
cell.accessoryType = .checkmark
foods[indexPath].isSelected.toggle()
// Remove checkmark
} else {
cell.accessoryType = .none
foods[indexPath].isSelected.toggle()
}
LoadSaveData.saveToFile(foods: foods)
tableView.reloadData()
}
It works!
"How do you deselect?"
I use this if condition inside of didSelectRowAt method
// Set a checkmark
if item.isSelected == false {
cell.accessoryType = .checkmark
item.isSelected.toggle()//
// Remove a checkmark
} else {
cell.accessoryType = .none
item.isSelected.toggle()//
}
The fact is that in this condition only the first part is executed, and the second part (else) is never executed. And I cannot understand why?
"call func reloadROws..."
I call tableView.reloadData() but it doesn't work.
Create your own small applications. For example, currency converter, to-do list, etc.