ios - 将相同的 View Controller 插入堆栈会产生奇怪的行为

标签 ios swift swift3

我在导航 Controller 中嵌入了一个 AccountViewController。

这就像一个带有关注者列表的帐户配置文件屏幕

当我点击关注者时,我将同一个 ViewController 推到顶部,并填充点击的用户的详细信息。

当我返回导航栏时,我希望返回到以前的用户个人资料屏幕,但是它在放弃顶部后显示新点击的用户屏幕。

这就是我正在做的事情

let userId = self.followerUsers[pickedCellIndexPathRow].userId!
let newUserVC = self.storyboard?.instantiateViewController(withIdentifier: "AccountVC") as! AccountViewController
newUserVC.userId = userId
self.navigationController?.pushViewController(newUserVC, animated: true)

这很好,但正如前面提到的,返回会丢弃屏幕,但旧屏幕上有新用户的详细信息

我该如何修复它 - 本质上我应该能够堆叠配置文件并返回它们

AccountViewController:

import UIKit
import SwiftEventBus

class AccountViewController: UIViewController,UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, SegmentClickedDelegate {

func userClickedSegment(segmentNumber: Int){

    segmentIndex = segmentNumber

}

var segmentIndex = 0
    {
    didSet{
             collectionView.reloadData()
    }
}



@IBOutlet weak var collectionView: UICollectionView!
override func viewDidLoad() {
    super.viewDidLoad()

    collectionView.dataSource = self
    collectionView.delegate = self


    let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout
    layout?.sectionHeadersPinToVisibleBounds = true

}

 var model = UserProfileViewModel()

var user: UserResponse?{
    didSet{
        containerCell.user = self.user
        mapUserPhotos()
        mapFollowers()
        mapFollowing()
        if collectionView != nil {
            collectionView.reloadData()
        }
    }
}

public var userId : String="" {
    didSet{
        model.getUserInfo(userId: userId)
        SwiftEventBus.onMainThread(self, name:EventBus.GetUserEvent) { result in
            let user = result.object as! UserResponse
            self.user = user
        }

    }
}

var reviews = [UserReviews](){
    didSet{

        if collectionView != nil {
            //collectionView.reloadData()
        }
    }
}

var followingUsers = [UserFollowing](){
    didSet{
        if collectionView != nil {
           // collectionView.reloadData()
        }

    }
}

var followerUsers = [UserFollowers](){
    didSet{
        if collectionView != nil {
            //collectionView.reloadData()
        }

    }
}

func mapUserPhotos(){
    if let result = user{
        self.reviews = result .reviews!
    }

}

func mapFollowers(){

    if let result = user{
        self.followerUsers = result .followers!
    }
}

func mapFollowing(){

    if let result = user{
        self.followingUsers = result .following!

    }
}






// number of sections is 2. Section above search and below search
func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 2
}

//number of items for each section. Section above search will have only one and below search will be dynamic as per images we have
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    if section == 0 {
        return 1
    } else {
        switch segmentIndex{//segmentedControl.selectedSegmentIndex {
        case 0 :
            return self.reviews.count
        case 1:
            return self.followerUsers.count
        case 2:
            return self.followingUsers.count

        default://break
            return self.reviews.count
        }

    }
}

private struct UserPostsBoard{
    static let PhotoCellIdentifier = "UserPhotoCell"
    static let FollowerCellIdentifier = "FollowerCell"
    static let FollowingCellIdentifier = "FollowingCell"

}

var containerCell = AboveSearchCollectionViewCell()
// cell for item at given indexpath: if section is 0 then return cell above search, if section is 1 then return cell below search
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    if indexPath.section == 0 {
        // above search cell
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "aboveSearch", for: indexPath)

        containerCell = (cell as? AboveSearchCollectionViewCell)!
        containerCell.user = self.user

        return cell
    } else {


        // below search cell

        print("self.segmentIndex\(self.segmentIndex)")

        //switch item{//self.segmentIndex{//segmentedControl.selectedSegmentIndex {
        switch self.segmentIndex{//segmentedControl.selectedSegmentIndex {
        case 0 :
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: UserPostsBoard.PhotoCellIdentifier, for: indexPath)
            let item = self.reviews[indexPath.row]

            if let itemCell = cell as? UserPhotoCollectionViewCell{
                itemCell.photo = item
            }
            return cell
        case 1:
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: UserPostsBoard.FollowerCellIdentifier, for: indexPath)
            let item = self.followerUsers[indexPath.row]

            if let itemCell = cell as? UserFollowersCollectionViewCell{
                itemCell.user = item
            }
            return cell
        case 2:
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: UserPostsBoard.FollowingCellIdentifier, for: indexPath)
            let item = self.followingUsers[indexPath.row]

            if let itemCell = cell as? UserFollowingCollectionViewCell{
                itemCell.user = item
            }
            return cell

        default://break
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: UserPostsBoard.PhotoCellIdentifier, for: indexPath)
            let photoItem = self.self.reviews[indexPath.row]

            if let photoCell = cell as? UserPhotoCollectionViewCell{
                photoCell.photo = photoItem
            }
            return cell
        }








    }
}

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if collectionView == self.collectionView {
            //pickedCell = collectionView.cellForItem(at: indexPath)
            //pickedCellIndexPathRow = indexPath.row
            //self.performSegue(withIdentifier: "showUserRating", sender: self)

        pickedCellIndexPathRow = indexPath.row

        switch segmentIndex{//segmentedControl.selectedSegmentIndex {
            case 0 : self.performSegue(withIdentifier: "showUserRating", sender: self)
            case 1 : //self.performSegue(withIdentifier: "showFollower", sender: self)
                let userId = self.followerUsers[pickedCellIndexPathRow].userId!
                let newUserVC = self.storyboard?.instantiateViewController(withIdentifier: "AccountVC") as! AccountViewController
                newUserVC.userId = userId
                self.navigationController?.pushViewController(newUserVC, animated: true)//push(avc, animated: true, completion: nil)
                //self.navigationController?.popViewController(animated: false)

            //case 2 : self.performSegue(withIdentifier: "showFollower", sender: self)
            default:  break

        }
        }

                }
var pickedCellIndexPathRow = 0



func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    if indexPath.section == 0 {
        return CGSize(width: collectionView.frame.width, height: 200)
    }else{

        switch segmentIndex{//segmentedControl.selectedSegmentIndex {
        case 0 :
            let width = (collectionView.frame.width / 3) - 1
            return CGSize(width: width, height: width)
        default:
            return CGSize(width: collectionView.frame.width, height: 80)
        }

    }
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {

    return 1.0
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return 1.0
}




// implementation of function viewForSupplementaryElementOfKind, for section header of collectionView
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    // returning the search bar for header
    let segmentView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "search", for: indexPath) as! TopBarCollectionReusableView
    segmentView.delegate = self
    return segmentView
}

// size for header in section: since we have 2 sections, collectionView will ask size for header for both sections so we make section header of first section with height 0 and width 0 so it remains like invisible.
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
    // if section is above search bar we need to make its height 0
    if section == 0 {
        return CGSize(width: 0, height: 0)
    }
    // for section header i.e. actual search bar
    return CGSize(width: collectionView.frame.width, height: 50)
}

}

最佳答案

好的,我正在使用 SwiftEventBus,它会在网络调用返回响应时填充字段。

愚蠢的是,我忘记将其包含在类中 - 因此所有实例都将在没有它的情况下更新

override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)

        SwiftEventBus.unregister(self)
    }

关于ios - 将相同的 View Controller 插入堆栈会产生奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41920309/

相关文章:

iOS Firebase 动态链接未在冷启动 iOS 14 上处理

ios - 如何搜索经过身份验证的用户 Firebase iOS Swift

ios - 尝试实现我的 Xcode 项目时看到错误(Firebase 问题不 react native )

ios - 重新运行应用程序后在 UIDatePicker 中显示保存的时间

Swift - 有人能帮我理解 .sorted(by :) works in this example?

ios - OS X 或 iOS 上的 SNI(服务器名称指示)- NSURLSession CFNetwork

ios - 如何以编程方式快速设置自动布局中的 UIView 大小?

ios - 如何在 swift 3 中恢复标签可见性状态

ios - 转换为Swift 3.0语法后,如何将类型“CBManagerState”的值转换为预期类型“CBCentralManagerState”?

ios - 我可以获得取消自动更新订阅的收据吗?