SwiftUI ගැටලු සහ විසදුම්

tarson

Well-known member
  • Feb 25, 2009
    8,660
    5,852
    113
    මේ කෝඩ් එක වැඩ කරන්නෙ නැත්තෙ මොකෝ කියල වැඩ්ඩෙක් කියපන්කො. extension එක ඇතුලෙ තියෙන ෆන්ශන් එකෙන් stateObject එකට අන්ක "8" සෙට් කරනව, ඊට පස්සෙ රිසල්ට් එක environmentObject එකක් හරහා අනිත් වීව්ස් වලට ගන්නයි ඕනෙ, ටෙක්ස්ට්බොක්ස් එකේ "8" ගන්න විදිහ. උබලට මේ ස්විෆ්ට්යූඅයි එක සෙට් ද බන්?

    Swift:
    import SwiftUI
    
    @main
    struct temp3App: App {
       
        @StateObject var thisClassLocal = ThisClass(thisIn: 7)
       
        var body: some Scene {
            WindowGroup {
                ContentView()
                    .environmentObject(ThisClass(thisIn: 7))
            }
        }
    }
    
    struct ContentView: View {
       
       
       
        var body: some View {
            @StateObject var thisClassLocal = ThisClass(thisIn: 7)
           
            VStack {
               
                Text(temp3App().thisClassLocal.thisVariableOut.description)
            }
            .padding()
                }
    }
    
    extension temp3App {
       
       
       
        func ThisFunction() {
            @StateObject var thisClassLocal2 = ThisClass(thisIn: 7)
            thisClassLocal2.thisVariableOut = 8
           
        }
    }
    
    
    class ThisClass: ObservableObject {
     
        @Published var thisVariableOut: Int
       
        init(thisIn: Int) {
            self.thisVariableOut = thisIn
           
        }
       
    }

    අනිත් අයත් ප්‍රශ්න තියෙනවනම් දාන්න දන්න දේවල් වලින් හෙල්ප් කරන්නම්
     
    • Like
    Reactions: kinkon

    mahadana_muththa

    Well-known member
  • Jan 5, 2020
    2,088
    2,593
    113
    මන් මේකට බේසික් කන්සෙප්ට් එක එන විදියට මොඩිෆයි කලේ, study about parameter and flow controll in swift ui. happy coding.

    Swift:
    import Foundation
    import SwiftUI
    
    
    
    struct MyApp: View {
        @StateObject var thisClassLocal: ThisClass
        var body: some View {
            VStack {
                Text(thisClassLocal.thisVariableOut.description)
            }
            .padding()
        }
    }
    
    class ThisClass: ObservableObject {
        @Published var thisVariableOut: Int
    
        init(thisIn: Int) {
            self.thisVariableOut = thisIn
        }
    }
    
    struct Content_Previews: PreviewProvider{
       
        static var previews: some View{
            MyApp(thisClassLocal: ThisClass(thisIn: 7))
        }
    }
     

    chanzKolla

    Well-known member
  • Nov 5, 2015
    1,219
    1,055
    113
    මේ කෝඩ් එක වැඩ කරන්නෙ නැත්තෙ මොකෝ කියල වැඩ්ඩෙක් කියපන්කො. extension එක ඇතුලෙ තියෙන ෆන්ශන් එකෙන් stateObject එකට අන්ක "8" සෙට් කරනව, ඊට පස්සෙ රිසල්ට් එක environmentObject එකක් හරහා අනිත් වීව්ස් වලට ගන්නයි ඕනෙ, ටෙක්ස්ට්බොක්ස් එකේ "8" ගන්න විදිහ. උබලට මේ ස්විෆ්ට්යූඅයි එක සෙට් ද බන්?

    Swift:
    import SwiftUI
    
    @main
    struct temp3App: App {
     
        @StateObject var thisClassLocal = ThisClass(thisIn: 7)
     
        var body: some Scene {
            WindowGroup {
                ContentView()
                    .environmentObject(ThisClass(thisIn: 7))
            }
        }
    }
    
    struct ContentView: View {
     
     
     
        var body: some View {
            @StateObject var thisClassLocal = ThisClass(thisIn: 7)
        
            VStack {
            
                Text(temp3App().thisClassLocal.thisVariableOut.description)
            }
            .padding()
                }
    }
    
    extension temp3App {
     
     
     
        func ThisFunction() {
            @StateObject var thisClassLocal2 = ThisClass(thisIn: 7)
            thisClassLocal2.thisVariableOut = 8
        
        }
    }
    
    
    class ThisClass: ObservableObject {
     
        @Published var thisVariableOut: Int
     
        init(thisIn: Int) {
            self.thisVariableOut = thisIn
        
        }
     
    }

    අනිත් අයත් ප්‍රශ්න තියෙනවනම් දාන්න දන්න දේවල් වලින් හෙල්ප් කරන්නම්
    meken testApp() kiyala aluth struct ekak hadaddi, aluth instance ekak hadenawa atthe mchn, uba kiyala tiyena eka simple mehema kalaki ( I'm no expert, there may be better ways)

    https://www.codebin.cc/code/clhfbx6ip0001mm081tcoxjaz:5RPeq4FqXqkkpCT3sJ7LfVuhFM5jaKem7CpY47jUVgjf

    Othana extension ekak dammata therumak nati wei, uba aluth object ekakane values change karanne, main class eke state object eka enviornmentObject ekak widihata child view walata pass karanna, child view wala enviroment object eke value change karanna function daanna extension nathuwa

    Screenshot 2023-05-09 at 02.48.34.png
     
    Last edited:
    • Like
    Reactions: tarson

    tarson

    Well-known member
  • Feb 25, 2009
    8,660
    5,852
    113
    මන් මේකට බේසික් කන්සෙප්ට් එක එන විදියට මොඩිෆයි කලේ, study about parameter and flow controll in swift ui. happy coding.

    Swift:
    import Foundation
    import SwiftUI
    
    
    
    struct MyApp: View {
        @StateObject var thisClassLocal: ThisClass
        var body: some View {
            VStack {
                Text(thisClassLocal.thisVariableOut.description)
            }
            .padding()
        }
    }
    
    class ThisClass: ObservableObject {
        @Published var thisVariableOut: Int
    
        init(thisIn: Int) {
            self.thisVariableOut = thisIn
        }
    }
    
    struct Content_Previews: PreviewProvider{
      
        static var previews: some View{
            MyApp(thisClassLocal: ThisClass(thisIn: 7))
        }
    }

    Environment object එක හරහා ගන්න විදිහයි ඕනෙ 😅, මේකත් හොදයි. Thank you.

    meken testApp() kiyala aluth struct ekak hadaddi, aluth instance ekak hadenawa atthe mchn, uba kiyala tiyena eka simple mehema kalaki ( I'm no expert, there may be better ways)

    https://www.codebin.cc/code/clhfbx6ip0001mm081tcoxjaz:5RPeq4FqXqkkpCT3sJ7LfVuhFM5jaKem7CpY47jUVgjf

    Othana extension ekak dammata therumak nati wei, uba aluth object ekakane values change karanne, main class eke state object eka enviornmentObject ekak widihata child view walata pass karanna, child view wala enviroment object eke value change karanna function daanna extension nathuwa

    View attachment 204460

    Thank you. Extension එකක ඇතුලෙ තියෙන ෆන්ශන් එක දෙන රිසල් එකක් අනිත් වීව්ස් වලට EnvironmentObject එක හරහා ගන්න විදිහ තමයි හොයාගන්න ඕනෙ උනේ. ඒක නිසා Extension එකයි EnvironmentObject එකයි කෝඩ් එකේ තියෙන්න ඕනෙ. ඒත් Thank you රිප්ලයි එකට සහ ලින්ක් එකට.
    ------ Post added on May 9, 2023 at 8:24 AM

    touch eka ne brother me tike, full time flutter api dan
    heta balala kiyannan

    NEMESIS ta mathaka athi
    ආ අව්ලක් නෑ 🍻
    ------ Post added on May 9, 2023 at 8:26 AM
     

    uia9030

    Junior member
  • Sep 6, 2016
    38
    61
    18
    Try this


    Swift:
    import SwiftUI
    
    @main
    struct temp3App: App {
     
        @StateObject var thisClassLocal = ThisClass(thisIn: 7)
     
        var body: some Scene {
            WindowGroup {
                ContentView()
                    .environmentObject(thisClassLocal)
            }
        }
    }
    
    struct ContentView: View {
     
        @EnvironmentObject var thisClassLocal: ThisClass
     
        var body: some View {
            VStack {
                Text(thisClassLocal.thisVariableOut.description)
            }
            .padding()
        }
    }
    
    extension temp3App {
     
        func thisFunction() {
            thisClassLocal.thisVariableOut = 8
        }
    }
    
    class ThisClass: ObservableObject {
     
        @Published var thisVariableOut: Int
     
        init(thisIn: Int) {
            self.thisVariableOut = thisIn
        }
    }
    ------ Post added on May 9, 2023 at 9:13 AM
     
    • Like
    Reactions: tarson

    tarson

    Well-known member
  • Feb 25, 2009
    8,660
    5,852
    113
    Try this


    Swift:
    import SwiftUI
    
    @main
    struct temp3App: App {
     
        @StateObject var thisClassLocal = ThisClass(thisIn: 7)
     
        var body: some Scene {
            WindowGroup {
                ContentView()
                    .environmentObject(thisClassLocal)
            }
        }
    }
    
    struct ContentView: View {
     
        @EnvironmentObject var thisClassLocal: ThisClass
     
        var body: some View {
            VStack {
                Text(thisClassLocal.thisVariableOut.description)
            }
            .padding()
        }
    }
    
    extension temp3App {
     
        func thisFunction() {
            thisClassLocal.thisVariableOut = 8
        }
    }
    
    class ThisClass: ObservableObject {
     
        @Published var thisVariableOut: Int
     
        init(thisIn: Int) {
            self.thisVariableOut = thisIn
        }
    }
    ------ Post added on May 9, 2023 at 9:13 AM

    මේක වැඩ. thank you. 🍻🍻


    @uia9030
    @Solo Rider

    තව එකක් ගෙනාව, මේකත් පුලුවන් කෙනෙක් මොකක්ද අව්ල කියල කියන්න. මේක මවුස් එකෙන් ස්ක්‍රොල් කරනකොට මැද තියෙන rectangle එක සූම් වෙන එකක්.
    මේකෙ කෝඩ් එක රන් වෙනව එරර්ස් මුකුත් නෑ, ඒත් මේකෙ එක පාරක් ස්ක්‍රොල් කරාම දිගටම සූම් වෙනව නවත්තන්න බෑ, සූම් අවුට් කලොත් දිගටම සූම් අවුට් වෙනව. මේකත් පුලුවන් තරම් ට්‍රයි කලා හදන්න, ඒත් මොන රෙද්දක් කරලවත් දිගටම ඔටෝ සූම් වෙන්නෙ මොකෝ කියල හිතාගන්න බෑ.

    Swift:
    import SwiftUI
    import Combine
    
    @main
    struct This4App: App {
     
      @State var subs = Set<AnyCancellable>()
      @StateObject var thisClassLocal = ThisClass(thisIn: 7, thisUpRefreshState: true, thisDownRefreshState: true, thisScaleBy: 1)
    
        var body: some Scene {
        WindowGroup {
         ThisView1()
          .onAppear {ThisFunction()}
        .environmentObject(thisClassLocal)
       } }}
    
    struct ThisView1: View {
    
        @State var scaleby: CGFloat = 2
        @EnvironmentObject var thisClassLocal2 : ThisClass
    
        var body: some View {
        VStack {
             Rectangle()
               .fill(.gray)
              .frame(width: 100, height: 100)
       }
      .padding()
          .scaleEffect(scaleby)
          .onChange(of: thisClassLocal2.thisDownRefreshState) { newValue in
             withAnimation { scaleby -= 2 }
         }
    
            .onChange(of: thisClassLocal2.thisUpRefreshState) { newValue in
             withAnimation { scaleby += 2
    
            } }}
    
    extension This4App {
    
    func ThisFunction() {
    
        NSApp.publisher(for: \.currentEvent)
                    .filter { event in event?.type == .scrollWheel }
                    .throttle(for: .milliseconds(200),
                              scheduler: DispatchQueue.main,
                              latest: true)
                    .sink {
    
                        if let event = $0 {
                            if event.deltaY > 0 {
                               thisClassLocal.thisUpRefreshState.toggle()
                            }
    
                            if event.deltaY < 0 {
                             thisClassLocal.thisDownRefreshState.toggle()
    
                            } } }
             .store(in: &subs)
     }}
    
    class ThisClass: ObservableObject {
    
        @Published var thisUpRefreshState: Bool
        @Published var thisDownRefreshState: Bool
    
        init(thisIn: CGFloat, thisUpRefreshState: Bool, thisDownRefreshState: Bool, thisScaleBy: CGFloat) {
    
        self.thisUpRefreshState = thisUpRefreshState
        self.thisDownRefreshState = thisDownRefreshState
        }
    
    }
    ------ Post added on May 9, 2023 at 4:31 PM
     
    Last edited:

    david345

    Active member
  • May 20, 2020
    251
    209
    43
    මාත් SwiftUI ඉගෙන ගන්නවා තාම. මං කරන්නේ chatGPT දාලා error fix කරනවා. මේ code එක chatGPT copy paste කරලා issue එක ගැනයි ඒක හැෙදන්න ඕන විදියයි කියන්න. අනිවා මේක හදාගන්න පුලුවන්.
     

    Nidarshana_k

    Well-known member
  • Feb 19, 2022
    29,633
    1
    51,359
    113
    ප*යාගල
    sorry.com
    මාත් SwiftUI ඉගෙන ගන්නවා තාම. මං කරන්නේ chatGPT දාලා error fix කරනවා. මේ code එක chatGPT copy paste කරලා issue එක ගැනයි ඒක හැෙදන්න ඕන විදියයි කියන්න. අනිවා මේක හදාගන්න පුලුවන්.
    Jobs thiyenwada bn lankwe???