将相同的viewcontroller

将相同的viewcontroller

我在导航控制器中嵌入了AccountViewController。

这就像一个带有关注者列表的AccountProfile屏幕

当我点击关注者时,将相同的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)
    }

10-04 19:33