1

我正在使用 SwiftUI,我使用的是 NavigationView,然后是 NavigationLink,它采用 CNContactViewController 的 UIViewControllerRepresentable。

UIViewController 可表示:

import SwiftUI
import ContactsUI

struct ContactView: UIViewControllerRepresentable {
    let contact: CNContact
    
    class Coordinator: NSObject, CNContactViewControllerDelegate {
        
        let contacts: Contacts
        
        init(contacts: Contacts) {
            self.contacts = contacts
        }
        
        func contactViewController(_ viewController: CNContactViewController, didCompleteWith contact: CNContact?) {
        }
    }
    
    init (_ contact: CNContact) {
        self.contact = contact
    }

    func makeUIViewController(context: Context) -> CNContactViewController {
        let controller = CNContactViewController(for: contact)
        controller.allowsEditing = true
        controller.contactStore = contacts.contactStore
        controller.message = ""
        controller.delegate = context.coordinator
        
        return controller
    }
    
    func makeCoordinator() -> Coordinator {
        return Coordinator(contacts: contacts)
    }

    
    func updateUIViewController(_ uiViewController: CNContactViewController, context: Context) {
        
    }
} 

导航链接:

NavigationView {
   List {
      let contact = ... // get an existing contact from the contact store

      NavigationLink(destination: ContactView(contact)) {
         Text("Show Contact")
      }
   }
}

按钮消失并重新出现:

按钮消失并重新出现

如果您想轻松运行,这里有一个 GitHub 上的演示项目

4

0 回答 0