Post

Replies

Boosts

Views

Created

Why Do We Need to Specify Schedule?
Hola, I have the following simple lines of code. import UIKit import Combine class ViewController: UIViewController { // MARK: - Variables var cancellable: AnyCancellable? @Published var labelValue: String? // MARK: - IBOutlet @IBOutlet weak var textLabel: UILabel! // MARK: - IBAction @IBAction func actionTapped(_ sender: UIButton) { labelValue = "Jim is missing" } // MARK: - Life cycle override func viewDidLoad() { super.viewDidLoad() cancellable = $labelValue .receive(on: DispatchQueue.main) .assign(to: \.text, on: textLabel) } } I just wonder what is the point of specifying the main thread with .receive? If I comment out the receive line, the app will still run without a problem. Muchos thankos
1
0
649
Oct ’21
Updating @State Variable Depending ForEach Row Selection
import SwiftUI struct ContentView: View { @State var numbers = [2021, 9, 30] var body: some View { //let firstLocalYear = 2021 let firstLocalMonth = 9 let firstLocalDay = 24 let firstLastDay = 30 NavigationView { List { Section(header: Text("Current month")) { ForEach(firstLocalDay ..< firstLastDay) { i in HStack { Text("\(firstLocalMonth)-\(i + 1)") Spacer() NavigationLink( destination: TimeView(numbers: $numbers), label: { Text("") }) } } } } } } } struct TimeView: View { @Binding var numbers: [Int] var body: some View { HStack { Text(String(numbers[0])) Text(String(numbers[1])) Text(String(numbers[2])) } } } I have the lines of code above to list some rows of text. For now, numbers is a state variable that is pre-determined. This state variable is passed on to TimeView. Actually, I want to change this array depending on which row the user selects like numbers = [firstLocalYear, firstLocalMonth, i] where i comes from the ForEach thing. How can I change this array? Muchos thankos.
1
0
445
Sep ’21
SwiftUI - What is Identifiable?
I have the following simple lines of code. import SwiftUI struct ContentView: View { var users = ["Susan", "Kate", "Natalie", "Kimberly", "Taylor", "Sarah", "Nancy", "Katherine", "Nicole", "Linda", "Jane", "Mary", "Olivia", "Barbara"] var body: some View { List { ForEach(users, id: \.self) { user in Text(user) } } } } So I'm just listing names. What I want to ask is what is id and what .self means. If I look up the doc under ForEach, it says the following. Either the collection’s elements must conform to Identifiable or you need to provide an id parameter to the ForEachinitializer. Does the compiler automatically generate a unique string like UUID for each element in the array or something? Can I somehow print the raw value of each id? Muchos thankos.
1
0
3.4k
Sep ’21
Changing a Status With @EnvironmentObject in Another View
I'm playing with @EnvironmentObject to see how it works in SwiftUI. I have the main view (ContentView) where it says the user has not logged in yet. By letting the user tap a link, I want to make it such that they can log in by tapping a button. class LoginMe: ObservableObject { @Published var loggedIn = false } struct ContentView: View { @StateObject var loginMe = LoginMe() var body: some View { if loginMe.loggedIn { Text("Yes, I'm logged in") } else { NavigationView { VStack { Text("No, not logged in yet") .padding(.vertical) NavigationLink(destination: LoginView()) { Text("Tap to log in") } } .navigationBarTitle("User") } .environmentObject(loginMe) } } } struct LoginView: View { @EnvironmentObject var loginMe: LoginMe var body: some View { /* Toggle(isOn: $loginMe.loggedIn) { Text("Log in") }.padding(.horizontal) */ Button("Login") { loginMe.loggedIn.toggle() } } } So far, when the user taps a button in the LoginView view, the screen goes back to ContentView and the navigation simply disappears. How can I change my code so that the status will change back and forth in in the LoginView view by tapping a button and then so that they can return to ContentView the navigation return button? I think the problem is that I need to use @State var in ContentView and @Binding var in LoginView. Things are kind of confusing. Muchos thankos.
1
0
557
Sep ’21
Toggling Values on EnvironmentValue (EditMode)
I have the following lines of code for showing a list of friends. import SwiftUI struct ContentView: View { @State var users = ["Susan", "Kate", "Natalie", "Kimberly", "Taylor", "Sarah", "Nancy", "Katherine", "Nicole", "Linda", "Jane", "Mary", "Olivia", "Barbara"] @State var editMode = EditMode.inactive var body: some View { NavigationView { List { ForEach(users, id: \.self) { user in Text(user) } } .navigationBarTitle("Friends") .environment(\.editMode, $editMode) .navigationBarItems(leading: Button("Edit", action: { if self.editMode == .active { self.editMode = .inactive } else { self.editMode = .active } })) } } } If you see the code at the bottom, I have four lines just in order to change the value of editMode. Does SwiftUI have something like showDetails.toggle() where showDetails is a Boolean variable? Muchos thankos.
3
0
585
Sep ’21
How to Use a Button in navigationBarItems to Work with List
I have the following lines of code to work with a list of strings. import SwiftUI struct ContentView: View { @State private var users = ["George", "Kenny", "Susan", "Natalie"] var body: some View { NavigationView { List { ForEach(users, id: \.self) { user in Text(user) } .onDelete(perform: delete) } .navigationBarTitle("My family") .toolbar { EditButton() } } } func delete(at offsets: IndexSet) { users.remove(atOffsets: offsets) } } Now, I'm doing the following out of curiosity. Now, I have a button in naviationBarItems. And I wonder if I can turn on and off the edit feature of the list with the button? struct ContentView: View { @State private var users = ["George", "Kenny", "Susan", "Natalie"] var body: some View { NavigationView { List { ForEach(users, id: \.self) { user in Text(user) } } .navigationBarTitle("My family") .navigationBarItems(trailing: Button(action: { print("Edit button pressed...") }) { Text("Edit") } ) } } } Muchos thankos.
1
0
545
Sep ’21
Navigation title with LayoutConstraints Warnings in Console
I just want to show a simple navigation title like the following. import SwiftUI struct ContentView: View { var body: some View { NavigationView { ZStack { Color.red.edgesIgnoringSafeArea(.all) Text("Hello") } .navigationTitle("GGG") .navigationBarTitleDisplayMode(.inline) .navigationBarHidden(false) } } } And I get a bunch of mumbo jumbo auto-layout warnings (Unable to simultaneously satisfy constraints...) in Console. If I comment out the navigationTitle line, I won't get them. I have never seen those messages in showing a navigation title when writing code with UIKit. What am I doing wrong? Muchos thankos
1
0
562
Sep ’21
Practical Use of Combine's Subject
I'm trying to understand how Combine works. The following is my sample code. import UIKit import Combine class ViewController: UIViewController { // MARK: - Variables var cancellable: AnyCancellable? // MARK: - IBAction @IBAction func buttonTapped(_ sender: UIButton) { currentValueSubject.send(20) } // MARK: - Life cycle var currentValueSubject = CurrentValueSubject<Int, Never>(1) override func viewDidLoad() { super.viewDidLoad() let cancellable = currentValueSubject .sink { value in print("New value: \(value)") } currentValueSubject.send(5) currentValueSubject.send(10) //currentValueSubject.send(completion: .finished) currentValueSubject.send(15) //cancellable.cancel() } } If I run it with the iPhone simulator, I get New value: 1 New value: 5 New value: 10 New value: 15 If I tap the button, the app won't get a new value. I suppose that's because the subscription is cancelled at the end of viewDidLoad? If so, why does it get cancelled? I don't quite see a practical side of Combine's Subject. When is it useful? Thanks.
2
0
1.2k
Aug ’21
Value of type 'UIView?' has no member 'isEnabled'
I have the following lines of code in practicing Combine. import UIKit import Combine class ViewController: UIViewController { // MARK: - Variables var cancellable: AnyCancellable? @Published var segmentNumber: Int = 0 // MARK: - IBOutlet @IBOutlet weak var actionButton: UIButton! // MARK: - IBAction @IBAction func segmentChanged(_ sender: UISegmentedControl) { segmentNumber = sender.selectedSegmentIndex } // MARK: - Life cycle override func viewDidLoad() { super.viewDidLoad() cancellable = $segmentNumber.receive(on: DispatchQueue.main) .assign(to: \.isEnabled, on: actionButton) } } I get an error at .assign that says Value of type 'UIView?' has no member 'isEnabled' What am I doing wrong? Thank you.
3
0
2.5k
Aug ’21
Using TestFlight Before Submission?
Hello. I'm a little bit confused about how TestFlight works. If I have an iOS app under development that has not been in the store and that has not been submitted for a review yet, can I use TestFlight to have it tested by my development team? I know that there are two types of tests, internal tests and external tests. It seems that you can use TestFlight for internal tests even if the app has not been submitted for a review. Thanks.
1
0
721
Jan ’21
Why Do We Need to Specify Schedule?
Hola, I have the following simple lines of code. import UIKit import Combine class ViewController: UIViewController { // MARK: - Variables var cancellable: AnyCancellable? @Published var labelValue: String? // MARK: - IBOutlet @IBOutlet weak var textLabel: UILabel! // MARK: - IBAction @IBAction func actionTapped(_ sender: UIButton) { labelValue = "Jim is missing" } // MARK: - Life cycle override func viewDidLoad() { super.viewDidLoad() cancellable = $labelValue .receive(on: DispatchQueue.main) .assign(to: \.text, on: textLabel) } } I just wonder what is the point of specifying the main thread with .receive? If I comment out the receive line, the app will still run without a problem. Muchos thankos
Replies
1
Boosts
0
Views
649
Activity
Oct ’21
Updating @State Variable Depending ForEach Row Selection
import SwiftUI struct ContentView: View { @State var numbers = [2021, 9, 30] var body: some View { //let firstLocalYear = 2021 let firstLocalMonth = 9 let firstLocalDay = 24 let firstLastDay = 30 NavigationView { List { Section(header: Text("Current month")) { ForEach(firstLocalDay ..< firstLastDay) { i in HStack { Text("\(firstLocalMonth)-\(i + 1)") Spacer() NavigationLink( destination: TimeView(numbers: $numbers), label: { Text("") }) } } } } } } } struct TimeView: View { @Binding var numbers: [Int] var body: some View { HStack { Text(String(numbers[0])) Text(String(numbers[1])) Text(String(numbers[2])) } } } I have the lines of code above to list some rows of text. For now, numbers is a state variable that is pre-determined. This state variable is passed on to TimeView. Actually, I want to change this array depending on which row the user selects like numbers = [firstLocalYear, firstLocalMonth, i] where i comes from the ForEach thing. How can I change this array? Muchos thankos.
Replies
1
Boosts
0
Views
445
Activity
Sep ’21
SwiftUI - What is Identifiable?
I have the following simple lines of code. import SwiftUI struct ContentView: View { var users = ["Susan", "Kate", "Natalie", "Kimberly", "Taylor", "Sarah", "Nancy", "Katherine", "Nicole", "Linda", "Jane", "Mary", "Olivia", "Barbara"] var body: some View { List { ForEach(users, id: \.self) { user in Text(user) } } } } So I'm just listing names. What I want to ask is what is id and what .self means. If I look up the doc under ForEach, it says the following. Either the collection’s elements must conform to Identifiable or you need to provide an id parameter to the ForEachinitializer. Does the compiler automatically generate a unique string like UUID for each element in the array or something? Can I somehow print the raw value of each id? Muchos thankos.
Replies
1
Boosts
0
Views
3.4k
Activity
Sep ’21
Changing a Status With @EnvironmentObject in Another View
I'm playing with @EnvironmentObject to see how it works in SwiftUI. I have the main view (ContentView) where it says the user has not logged in yet. By letting the user tap a link, I want to make it such that they can log in by tapping a button. class LoginMe: ObservableObject { @Published var loggedIn = false } struct ContentView: View { @StateObject var loginMe = LoginMe() var body: some View { if loginMe.loggedIn { Text("Yes, I'm logged in") } else { NavigationView { VStack { Text("No, not logged in yet") .padding(.vertical) NavigationLink(destination: LoginView()) { Text("Tap to log in") } } .navigationBarTitle("User") } .environmentObject(loginMe) } } } struct LoginView: View { @EnvironmentObject var loginMe: LoginMe var body: some View { /* Toggle(isOn: $loginMe.loggedIn) { Text("Log in") }.padding(.horizontal) */ Button("Login") { loginMe.loggedIn.toggle() } } } So far, when the user taps a button in the LoginView view, the screen goes back to ContentView and the navigation simply disappears. How can I change my code so that the status will change back and forth in in the LoginView view by tapping a button and then so that they can return to ContentView the navigation return button? I think the problem is that I need to use @State var in ContentView and @Binding var in LoginView. Things are kind of confusing. Muchos thankos.
Replies
1
Boosts
0
Views
557
Activity
Sep ’21
Toggling Values on EnvironmentValue (EditMode)
I have the following lines of code for showing a list of friends. import SwiftUI struct ContentView: View { @State var users = ["Susan", "Kate", "Natalie", "Kimberly", "Taylor", "Sarah", "Nancy", "Katherine", "Nicole", "Linda", "Jane", "Mary", "Olivia", "Barbara"] @State var editMode = EditMode.inactive var body: some View { NavigationView { List { ForEach(users, id: \.self) { user in Text(user) } } .navigationBarTitle("Friends") .environment(\.editMode, $editMode) .navigationBarItems(leading: Button("Edit", action: { if self.editMode == .active { self.editMode = .inactive } else { self.editMode = .active } })) } } } If you see the code at the bottom, I have four lines just in order to change the value of editMode. Does SwiftUI have something like showDetails.toggle() where showDetails is a Boolean variable? Muchos thankos.
Replies
3
Boosts
0
Views
585
Activity
Sep ’21
How to Use a Button in navigationBarItems to Work with List
I have the following lines of code to work with a list of strings. import SwiftUI struct ContentView: View { @State private var users = ["George", "Kenny", "Susan", "Natalie"] var body: some View { NavigationView { List { ForEach(users, id: \.self) { user in Text(user) } .onDelete(perform: delete) } .navigationBarTitle("My family") .toolbar { EditButton() } } } func delete(at offsets: IndexSet) { users.remove(atOffsets: offsets) } } Now, I'm doing the following out of curiosity. Now, I have a button in naviationBarItems. And I wonder if I can turn on and off the edit feature of the list with the button? struct ContentView: View { @State private var users = ["George", "Kenny", "Susan", "Natalie"] var body: some View { NavigationView { List { ForEach(users, id: \.self) { user in Text(user) } } .navigationBarTitle("My family") .navigationBarItems(trailing: Button(action: { print("Edit button pressed...") }) { Text("Edit") } ) } } } Muchos thankos.
Replies
1
Boosts
0
Views
545
Activity
Sep ’21
Navigation title with LayoutConstraints Warnings in Console
I just want to show a simple navigation title like the following. import SwiftUI struct ContentView: View { var body: some View { NavigationView { ZStack { Color.red.edgesIgnoringSafeArea(.all) Text("Hello") } .navigationTitle("GGG") .navigationBarTitleDisplayMode(.inline) .navigationBarHidden(false) } } } And I get a bunch of mumbo jumbo auto-layout warnings (Unable to simultaneously satisfy constraints...) in Console. If I comment out the navigationTitle line, I won't get them. I have never seen those messages in showing a navigation title when writing code with UIKit. What am I doing wrong? Muchos thankos
Replies
1
Boosts
0
Views
562
Activity
Sep ’21
Practical Use of Combine's Subject
I'm trying to understand how Combine works. The following is my sample code. import UIKit import Combine class ViewController: UIViewController { // MARK: - Variables var cancellable: AnyCancellable? // MARK: - IBAction @IBAction func buttonTapped(_ sender: UIButton) { currentValueSubject.send(20) } // MARK: - Life cycle var currentValueSubject = CurrentValueSubject<Int, Never>(1) override func viewDidLoad() { super.viewDidLoad() let cancellable = currentValueSubject .sink { value in print("New value: \(value)") } currentValueSubject.send(5) currentValueSubject.send(10) //currentValueSubject.send(completion: .finished) currentValueSubject.send(15) //cancellable.cancel() } } If I run it with the iPhone simulator, I get New value: 1 New value: 5 New value: 10 New value: 15 If I tap the button, the app won't get a new value. I suppose that's because the subscription is cancelled at the end of viewDidLoad? If so, why does it get cancelled? I don't quite see a practical side of Combine's Subject. When is it useful? Thanks.
Replies
2
Boosts
0
Views
1.2k
Activity
Aug ’21
Value of type 'UIView?' has no member 'isEnabled'
I have the following lines of code in practicing Combine. import UIKit import Combine class ViewController: UIViewController { // MARK: - Variables var cancellable: AnyCancellable? @Published var segmentNumber: Int = 0 // MARK: - IBOutlet @IBOutlet weak var actionButton: UIButton! // MARK: - IBAction @IBAction func segmentChanged(_ sender: UISegmentedControl) { segmentNumber = sender.selectedSegmentIndex } // MARK: - Life cycle override func viewDidLoad() { super.viewDidLoad() cancellable = $segmentNumber.receive(on: DispatchQueue.main) .assign(to: \.isEnabled, on: actionButton) } } I get an error at .assign that says Value of type 'UIView?' has no member 'isEnabled' What am I doing wrong? Thank you.
Replies
3
Boosts
0
Views
2.5k
Activity
Aug ’21
Using TestFlight Before Submission?
Hello. I'm a little bit confused about how TestFlight works. If I have an iOS app under development that has not been in the store and that has not been submitted for a review yet, can I use TestFlight to have it tested by my development team? I know that there are two types of tests, internal tests and external tests. It seems that you can use TestFlight for internal tests even if the app has not been submitted for a review. Thanks.
Replies
1
Boosts
0
Views
721
Activity
Jan ’21