Post

Replies

Boosts

Views

Activity

Reply to I get the error "Unexpectedly found nil while implicitly unwrapping an Optional value" when calling a protocol method
Thanks for your updated code. Your code is still missing storeButtonIsSelected, but many important things got clarified and I filled the missing part by guess. So, your view hierarchy (transition structure) is something like this, OK? ViewContoller |-(startLivePressed)----Segue(id: segueToCamera)- LiveViewController | ↑ | No direct segue connection | ↓-(storeButtonPressed)--Segue(id: segueToStore)-- StoreViewController (I have renamed all the type names with Capitalized identifiers if not yet. If you have some specific reason that you cannot follow this very common Swift coding rule, please re-interpret the type names.) In this transition structure, you cannot use delegate pattern. With using segue, the target view controllers will be instantiated at each transition, so once a view controller is dismissed, it is sort of not alive. But the delegate needs to be alive while the view controller using it is alive. In other words, while StoreViewController is presented, there is no alive LiveViewController. If you want to keep this transition structure, you may need to give up using delegate pattern. Or else, you need to re-design the view hierarchy completely. When keeping your transition structure, shared object would be a better strategy. MyModel.swift: import Foundation class MyModel { static let shared = MyModel() var labelTitle: String = "" } StoreViewController class StoreViewController: UIViewController { // var labelDelegate: LabelMaker! @IBAction func viewersTapped(_ sender: UIButton) { MyModel.shared.labelTitle = sender.currentTitle ?? "" // labelDelegate.labelMaker(value: sender.currentTitle!) } //... } LiveViewController class LiveViewController: UIViewController { // let storeView = StoreViewController() @IBOutlet weak var stopButton: UIButton! @IBOutlet var onlineViewers: UILabel! override func viewDidLoad() { super.viewDidLoad() // storeView.labelDelegate = self } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) onlineViewers.text = MyModel.shared.labelTitle } } If you do not prefer this, you can utilize delegate pattern with making ViewController as a delegate, but it may need a little more complex code. (Please remember ViewController is still alive while StoreViewController is presented.) By the way, some of your protocols SegueDestination, SourceForB and SourceForC are making your code more complex than it should be. That is making it very hard to read your code.
Topic: Programming Languages SubTopic: Swift Tags:
Feb ’21
Reply to I get the error "Unexpectedly found nil while implicitly unwrapping an Optional value" when calling a protocol method
how do I make the value in onlineViewers stay when the user leaves the app and comes back?  That's a little bit too far from your original question of this thread. For a small amount of app state info like labelTitle, UserDefaults would be a good place. Also you should better learn about App State Restoration features of iOS: Preserving Your App's UI Across Launches - https://developer.apple.com/documentation/uikit/view_controllers/preserving_your_app_s_ui_across_launches. (There may be some other documents. Non-Apple articles might be more easy to read, as usual.) If you cannot make it work with your code, you should better start a new thread focused on it.
Topic: Programming Languages SubTopic: Swift Tags:
Feb ’21
Reply to View Initializes Twice
Any way to reduce the amount of times the view is initialized or api calls being made would be greatly appreciated! In SwiftUI, initializers of Views may be called unpredictable times. You should better not make API calls in the initializers of Views. Better try onAppear.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Feb ’21
Reply to Need suggestions on publisher autosave solutions
I should have put more context here. Please clarify: applies as soon as any change happens is the desired behavior but with using onChange, you find some undesired behavior Is that OK? You should better show all the view hierarchy and all the definitions of relevant types (Notes, SingleNote, or there may be more) to examine what is happening.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Feb ’21
Reply to Need suggestions on publisher autosave solutions
It should save as soon as anything changes, that is autosave I meant. Thanks. Then this description I tried to add a onChange modifier to the TextEditor but it applies as soon as any change happens, which is not desired. is sort of a mistake, OK? applies as soon as any change happens is the desired behavior.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Feb ’21
Reply to 3 code errors
You should better keep your code properly indented. Cmd-A Ctrl-I That will reveal some simple mistakes. Value of type 'String' has no member 'frame' : line 174 Cannot infer contextual base in reference to member 'all': line 176 Your line 170...180 will look like this when extra whitespaces and newlines removed: Text(isDecrementOn ? "-10" : "+10".frame(width: 50).padding(.all) ) You write .frame(width: 50) immediately after a String literal "+10". You may want to add .frame to Text, not a String. Closure containing a declaration cannot be used with function builder 'ViewBuilder' : line 186 The line struct ContentView_Previews: PreviewProvider { needs to be placed outside of the definition of struct ContentView. Meaning you need to write it after the closing brace } matching the opening brace struct ContentView: View {. You are writing struct ContentView_Previews: PreviewProvider { just after the closing brace for HStack(alignment: .center, spacing: 10) {. You need to be careful enough about matching parentheses.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Feb ’21