Swift: 使用 NSNotification 更新 Button.setTitle 和当前 UIPageView

Swift: Update Button.setTitle and current UIPageView with NSNotification

Swift 4:我正在使用 NSNotification post 并观察两个视图控制器之间的对象。我的问题是更新 viewController 观察给定接收到的对象。

有两件事需要更新
1. 按钮的 setTitle,
2. UIPageViewController 上的当前页面。
如何更新这两个东西?

我今天和昨天都为此工作了一整天,但没有结果。

我试过使用 struct 来做更新和普通变量。

**YellowViewController - Observes -> Issue here**

let searchedReturnedKey = "Searched"

class YellowViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {

let pageView = PageView()
let searched = Notification.Name(rawValue: searchedReturnedKey)
private var isAnimating = false

struct structure { 
    static var SearchedIndex = Int()
}


override func viewDidLoad() {
    super.viewDidLoad()
    dataSource = self
    delegate = self
    setupUIPageView()
    setupNavigationBarItem()
    createObserver()
}

func createObserver(){
    NotificationCenter.default.addObserver(self, selector: #selector(YellowViewController.updateVerseView(notification:)), name: searched, object: nil)
}
@objc func updateVerseView(notification: NSNotification) {
    print("Observed")
    structure.SearchedIndex = notification.object as! Int
    let chapterIndexLabel = doneModel.ChapterIndex
    let verseList = doneModel.chapterVerses
    let pageView = PageView()
    // HERE IS MY ISSUE - booklabel.text and pageview.verses
    bookLabel.text = chapterIndexLabel[structure.SearchedIndex] 
    pageView.verses = verseList[structure.SearchedIndex]
    print(structure.SearchedIndex, "Index")
}


 func setupUIPageView() {
    let verseList = bibleModel.chapterVerses
    let chapterIndexLabel = bibleModel.ChapterIndex
    let pageViewControllers = [pageView] // Important! UIPageViewController sets ViewController in a list to enable swiping. - Understand this.
    pageView.verses = verseList.first! // Important! Setups verses for first view.
    bookLabel.text = chapterIndexLabel.first!
    setViewControllers(pageViewControllers, direction: .forward, animated: true, completion: nil)
}

var bookLabel = UILabel()
func setupLeftNavItems() {
    let bookButton = UIButton(type: .system)
    bookButton.setTitle(bookLabel.text! + " 1", for: .normal)
    bookButton.setTitleColor(goldColor, for: .normal)
    bookButton.titleLabel?.font = UIFont(name: "AvenirNext-DemiBold", size: 18)
    bookButton.addTarget(self, action: #selector(handleSearch), for: .touchUpInside)
    //        bookButton.frame = CGRect(x: 30, y: 0, width: 54, height: 34)
    self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: bookButton)
}


@objc func handleSearch() { // Handles openeing SearchViewController
    let searchViewController = SearchViewController()
    let navController = UINavigationController(rootViewController: searchViewController)
    present(navController, animated: true, completion: nil)
}

func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController]) {
    isAnimating = true
}



//    Handles swiping right - To next chapter.
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
    if isAnimating {
        return nil
    }
    let verseList = bibleModel.chapterVerses
    let currentVerseView = (viewController as! PageView).verses
    let currentIndex = verseList.index(of: currentVerseView)
    let chapterIndexLabel = doneModel.ChapterIndex

    if currentIndex! < verseList.count - 1 {
        let pageView = PageView()
        pageView.verses = verseList[currentIndex! + 1 ]
        bookLabel.text = chapterIndexLabel[currentIndex! + 1]
        print("Swiped right")
        setupLeftNavItems()
        createObserver()
        return pageView
    }
    return nil
}

//    Handles swiping left - To previous chapter
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
    if isAnimating {
        return nil
    }
    let verseList = bibleModel.chapterVerses
    let currentVerseView = (viewController as! PageView).verses
    let currentIndex = verseList.index(of: currentVerseView)
    let chapterIndexLabel = doneModel.ChapterIndex

    if currentIndex! > 0 {
        let pageView = PageView()
        pageView.verses = verseList[currentIndex! - 1]  
        bookLabel.text = chapterIndexLabel[currentIndex! - 1]
        setupLeftNavItems()
        print("Swiped left")
        createObserver()
        return pageView
    }
    return nil
}

}

SearchViewController - Post

class SearchViewController: UITableViewController, UISearchBarDelegate {

let cellId = "cellId"

override func viewDidLoad() {
    super.viewDidLoad()
    navigationItem.titleView = navSearchBar
    setupView()
}


func setupView() {
    tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
    tableView.delegate = self
    tableView.dataSource = self
    navSearchBar.delegate = self
}


override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
       return Books.count
}

**I post the data from this function**
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let cell = tableView.cellForRow(at: indexPath)
    let cellLabelContent = cell!.textLabel!.text 
    let cellLabelIndex = Books.firstIndex(of: cellLabelContent!)
    print("Book name:", cellLabelContent!, "index:", cellLabelIndex!)
    let notificationName = Notification.Name(rawValue: searchedReturnedKey)
    NotificationCenter.default.post(name: notificationName, object: cellLabelIndex)
    dismiss(animated: true, completion: nil)


}
}

实际结果 -> 无更新
预期结果 -> 更新给定对象

在这里你需要addObserver你的选择器#selector(YellowViewController.updateVerseView(notification:)

我想你设置错了ViewController名字

为了能够更新 UIPageViewController 的当前视图,您需要在 updateVerseView 中执行此操作。
setViewControllers 方法是更新 UIPageViewController 内部视图的正确且有效的方法。

let pageViewControllers = [pageView]
    pageView.verses = verseList[structure.searchedIndex]
    setViewControllers(pageViewControllers, direction: .forward, animated: true, completion: nil)

要更新您的 bookLabel.txt,您只需调用 setupLeftNavItems() 函数。