将相同的viewcontroller推送到堆栈上会有奇怪的行为

时间:2017-01-29 11:20:16

标签: ios swift swift3

我在导航控制器中嵌入了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)
}

}

1 个答案:

答案 0 :(得分:0)

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

愚蠢的是,我忘了将它包括在课堂中 - 所以所有实例都会在没有它的情况下进行更新

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

        SwiftEventBus.unregister(self)
    }