最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

swiftui - How do I fix Screen Time API Error: Value of type 'Button<Text>' has no member '

programmeradmin0浏览0评论

I have been following the tutorial from WWDC 21 for how to use the Screen Time API. The tutorial presents the following code:

import FamilyControls
import SwiftUI

@StateObject var model = MyModel()
@State var isPresented = false

var body: some View {
    Button("Select Apps to Discourage") {
        isPresented = true
    }
    .familyActivityPicker(isPresented: $isPresented, 
                          selection: $model.selectionToDiscourage)
}

I tried using this in my application as such:

import SwiftUI
import SwiftData
import FamilyControls

struct AppSelectionButton: View {
    @Binding var isPresented: Bool
    @ObservedObject var model: MyModel

    var body: some View {
        Button("Select Apps to Discourage") {
            isPresented = true
        }
        .familyActivityPicker(isPresented: $isPresented,
                              selection: $model.selectionToDiscourage)
    }
}

class MyModel: ObservableObject {
    @Published var selectionToDiscourage: FamilyActivitySelection

    init() {
        self.selectionToDiscourage = FamilyActivitySelection()
    }
}

But when I do so I get the error "Value of type 'Button' has no member 'familyActivityPicker'".

I also tried:

struct ExampleView: View {
    @@Published var selection = FamilyActivitySelection()

    var body: some View {
        VStack {
            Image(systemName: "eye")
                .font(.system(size: 76.0))
                .padding()

            FamilyActivityPicker(selection: $selection)

            Image(systemName: "hourglass")
                .font(.system(size: 76.0))
                .padding()
        }
        .onChange(of: selection) { newSelection in
            let applications = selection.applications
            let categories = selection.categories
            let webDomains = selection.webDomains
        }
    }
}

But it still didn't work.

I'm targetting iOS 18.1.

What could be causing this error?

I have been following the tutorial from WWDC 21 for how to use the Screen Time API. The tutorial presents the following code:

import FamilyControls
import SwiftUI

@StateObject var model = MyModel()
@State var isPresented = false

var body: some View {
    Button("Select Apps to Discourage") {
        isPresented = true
    }
    .familyActivityPicker(isPresented: $isPresented, 
                          selection: $model.selectionToDiscourage)
}

I tried using this in my application as such:

import SwiftUI
import SwiftData
import FamilyControls

struct AppSelectionButton: View {
    @Binding var isPresented: Bool
    @ObservedObject var model: MyModel

    var body: some View {
        Button("Select Apps to Discourage") {
            isPresented = true
        }
        .familyActivityPicker(isPresented: $isPresented,
                              selection: $model.selectionToDiscourage)
    }
}

class MyModel: ObservableObject {
    @Published var selectionToDiscourage: FamilyActivitySelection

    init() {
        self.selectionToDiscourage = FamilyActivitySelection()
    }
}

But when I do so I get the error "Value of type 'Button' has no member 'familyActivityPicker'".

I also tried:

struct ExampleView: View {
    @@Published var selection = FamilyActivitySelection()

    var body: some View {
        VStack {
            Image(systemName: "eye")
                .font(.system(size: 76.0))
                .padding()

            FamilyActivityPicker(selection: $selection)

            Image(systemName: "hourglass")
                .font(.system(size: 76.0))
                .padding()
        }
        .onChange(of: selection) { newSelection in
            let applications = selection.applications
            let categories = selection.categories
            let webDomains = selection.webDomains
        }
    }
}

But it still didn't work.

I'm targetting iOS 18.1.

What could be causing this error?

Share Improve this question edited Feb 2 at 8:37 Joakim Danielson 52.1k5 gold badges33 silver badges71 bronze badges asked Feb 1 at 22:55 Ameen IzhacAmeen Izhac 1251 silver badge8 bronze badges 2
  • stackoverflow/questions/71698728/… – lorem ipsum Commented Feb 1 at 23:43
  • In your ExampleView, you should not have @@Published var selection = FamilyActivitySelection(), it should be @State var selection = FamilyActivitySelection(). See the docs FamilyActivityPicker – workingdog support Ukraine Commented Feb 2 at 1:44
Add a comment  | 

1 Answer 1

Reset to default 1

Try this example code. See also FamilyActivityPicker

class MyModel: ObservableObject {
    @Published var selectionToDiscourage = FamilyActivitySelection()
}

Here is my full test code:

import FamilyControls
import SwiftUI

struct ContentView: View {
    @StateObject private var model = MyModel()
    @State private var isPresented = false
    
    var body: some View {
        Button("Select Apps to Discourage") {
            isPresented = true
        }
        .familyActivityPicker(isPresented: $isPresented,
                              selection: $model.selectionToDiscourage)
        
     //   AppSelectionButton(isPresented: $isPresented, model: model)
    }
}

struct AppSelectionButton: View {
    @Binding var isPresented: Bool
    @ObservedObject var model: MyModel
    
    var body: some View {
        Button("Select Apps to Discourage") {
            isPresented = true
        }
        .familyActivityPicker(isPresented: $isPresented,
                              selection: $model.selectionToDiscourage)
    }
}

class MyModel: ObservableObject {
    @Published var selectionToDiscourage = FamilyActivitySelection()
}

MacOS 15.3, using Xcode 16.2, target iOS-18, tested on real iOS device.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论