2017-10-18 49 views
0

我有一個headerView其中我有一個浮動按鈕。由於浮動按鈕中的所有子項都是以編程方式生成的,並且它是headerView的子視圖,所以我無法調用performSegue。我試圖創建一個委託方法,但我完全搞砸了。我知道協議可能是解決方案,但我很不確定如何正確解決問題。 我附上下面PerformSegue從UICollectionReusableView(報頭)的自定義按鈕夫特

headerView代碼headerView的圖像:

class ProfileHeader: UICollectionReusableView, FloatyDelegate { 

@IBOutlet weak var pname: UILabel! 
@IBOutlet weak var pusername: UILabel! 
@IBOutlet weak var profilePic: UIImageView! 
@IBOutlet weak var userDesc: UILabel! 
@IBOutlet weak var allviews: UILabel! 
@IBOutlet weak var allphotos: UILabel! 



var user : User! 
var userposts = [String]() 
var currentViewUser: String! 
var floaty = Floaty() 

let uid = KeychainWrapper.standard.string(forKey: KEY_UID) 

override func awakeFromNib() { 
    super.awakeFromNib() 

    user = User() 

    fetchCurrentUser() 
    profilePic.addBlackGradientLayer(frame: profilePic.bounds) 
    layoutFAB() 
} 




func layoutFAB() { 

    floaty.openAnimationType = .slideDown 
    floaty.hasShadow = false 
    floaty.addItem("Edit Profile", icon: UIImage(named: "")) { item in 
     // here is where the segue is to be performed. 
    } 

    floaty.paddingY = (frame.height - 50) - floaty.frame.height/2 
    floaty.fabDelegate = self 
    floaty.buttonColor = UIColor.white 
    floaty.hasShadow = true 
    floaty.size = 45 

    addSubview(floaty) 

} 


func fetchCurrentUser(){ 
    if uid != nil { 
     FBDataservice.ds.REF_CURR_USER.observeSingleEvent(of: .value, with: { (snapshot) in 
      if let allData = snapshot.value as? Dictionary<String, Any> { 
       if let cred = allData["credentials"] as? Dictionary<String, Any> { 
        let user = User(userid: snapshot.key, userData: cred) 
        self.pusername.text = user.username 
        self.pname.text = user.name 
        self.userDesc.text = user.aboutme 
        self.allphotos.text = String(user.photos) 
        self.allviews.text = String(user.views) 
        if user.userPic != nil { 
         let request = URL(string: user.userPic) 
         Nuke.loadImage(with: request!, into: self.profilePic) 
        } else { 
         return 
        } 
       } 
      } 

     }) 
    } 
} 
} 

CollectionViewController代碼:

class ProfileVC: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { 


@IBOutlet weak var collectionView: UICollectionView! 

var user : User! 
var userposts = [String]() 
var post = [Posts]() 
var currentViewUser: String! 
var imagePicker: UIImagePickerController! 
var fetcher: Fetcher! 
var imageFromImagePicker = UIImage() 


let uid = KeychainWrapper.standard.string(forKey: KEY_UID) 

override func viewDidLoad() { 
    super.viewDidLoad() 

    user = User() 
    fetcher = Fetcher() 
    imagePicker = UIImagePickerController() 
    imagePicker.delegate = self 

    collectionView.delegate = self 
    collectionView.dataSource = self 



    initializeUserPost() 



    let nib = UINib(nibName: "ProfileCell", bundle: nil) 
    collectionView.register(nib, forCellWithReuseIdentifier: "ProfileCell") 





} 


func editProfileTapped() { 
    performSegue(withIdentifier: "manageconnections", sender: nil) 
} 

@IBAction func manageConnections(_ sender: Any) { 
    performSegue(withIdentifier: "manageconnections", sender: nil) 
} 


override func viewWillAppear(_ animated: Bool) { 
    super.viewWillAppear(true) 
    collectionView.reloadData() 
} 


@IBAction func gotoSettings(_ sender: Any) { 
    performSegue(withIdentifier: "openSettings", sender: nil) 
} 



@IBAction func newPost(_ sender: Any) { 
    uploadNewPost() 
} 

@IBAction func backToFeed(_ sender: Any) { 
    performSegue(withIdentifier: "feedroute", sender: nil) 
} 

@IBAction func searchUsers(_ sender: Any) { 
    performSegue(withIdentifier: "searchNow", sender: nil) 
} 


func initializeUserPost() { 

     FBDataservice.ds.REF_POSTS.observe(.value, with: { (snapshot) in 
      if let snap = snapshot.value as? Dictionary<String, Any> { 
       for snapy in snap { 
        if let userimages = snapy.value as? Dictionary<String, Any> { 
         let author = userimages["author"] as? String 
         if author! == self.uid! { 
          let images = userimages["imageurl"] as? String 
          self.userposts.append(images!) 

         } 
        } 
       } 
      } 
      self.collectionView.reloadData() 
     }) 


} 

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let userImages = userposts[indexPath.row] 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ProfileCell", for: indexPath) as! ProfileCell 
    cell.fillCells(uid: uid!, userPost: userImages) 
    return cell 
} 


func numberOfSections(in collectionView: UICollectionView) -> Int { 
    return 1 
} 


func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return userposts.count 


} 

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 


     let selecteditem : String! 
     selecteditem = userposts[indexPath.row] 
     performSegue(withIdentifier: "lol", sender: selecteditem) 

} 

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    if segue.identifier == "lol" { 
     if let detailvc = segue.destination as? PhotoDetailVC { 
      if let bro = sender as? String { 
       detailvc.image = bro 
      } 
     } 
    } 
} 


func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { 

    let view = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "ProfileHeader", for: indexPath) as! ProfileHeader 
    return view 
} 




func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 

     let width = (collectionView.bounds.size.width/3) - 1 
     print(width) 
     let size = CGSize(width: width, height: width) 
     return size 

} 

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

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

你的代碼在哪裏? –

+0

你在問哪個碼?處理浮子按鈕? –

+0

您headerView將有助於我們 –

回答

3

解決你的問題,你應該使用一個名爲委託模式。

代表意味着您需要做一些工作來執行HeaderView的繼續工作,而這可以實現,在這種情況下您的工作是ProfileVC

1 - 創建HeaderView或其他文件中的協議,這是你的電話。

2 - 在HeaderView添加的協議變量類型以及與從的viewController通過。

​​3210

}

3 - 最後,編輯您ProfileVC實施HeaderViewDelegate協議,並把它傳遞給你的HeaderView

class ProfileVC: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, HeaderViewDelegate { 

@IBOutlet weak var collectionView: UICollectionView! 

var user : User! 
var userposts = [String]() 
var post = [Posts]() 
var currentViewUser: String! 
var imagePicker: UIImagePickerController! 
var fetcher: Fetcher! 
var imageFromImagePicker = UIImage() 

let uid = KeychainWrapper.standard.string(forKey: KEY_UID) 

override func viewDidLoad() { 
    super.viewDidLoad() 

    user = User() 
    fetcher = Fetcher() 
    imagePicker = UIImagePickerController() 
    imagePicker.delegate = self 

    collectionView.delegate = self 
    collectionView.dataSource = self 

    initializeUserPost() 


    let nib = UINib(nibName: "ProfileCell", bundle: nil) 
    collectionView.register(nib, forCellWithReuseIdentifier: "ProfileCell") 

} 

//Method from the new protocol 
func fabItemClicked(){ 
//Perform your segue here. 
} 

func editProfileTapped() { 
    performSegue(withIdentifier: "manageconnections", sender: nil) 
} 

@IBAction func manageConnections(_ sender: Any) { 
    performSegue(withIdentifier: "manageconnections", sender: nil) 
} 


override func viewWillAppear(_ animated: Bool) { 
    super.viewWillAppear(true) 
    collectionView.reloadData() 
} 


@IBAction func gotoSettings(_ sender: Any) { 
    performSegue(withIdentifier: "openSettings", sender: nil) 
} 



@IBAction func newPost(_ sender: Any) { 
    uploadNewPost() 
} 

@IBAction func backToFeed(_ sender: Any) { 
    performSegue(withIdentifier: "feedroute", sender: nil) 
} 

@IBAction func searchUsers(_ sender: Any) { 
    performSegue(withIdentifier: "searchNow", sender: nil) 
} 


func initializeUserPost() { 

     FBDataservice.ds.REF_POSTS.observe(.value, with: { (snapshot) in 
      if let snap = snapshot.value as? Dictionary<String, Any> { 
       for snapy in snap { 
        if let userimages = snapy.value as? Dictionary<String, Any> { 
         let author = userimages["author"] as? String 
         if author! == self.uid! { 
          let images = userimages["imageurl"] as? String 
          self.userposts.append(images!) 

         } 
        } 
       } 
      } 
      self.collectionView.reloadData() 
     }) 


} 

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let userImages = userposts[indexPath.row] 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ProfileCell", for: indexPath) as! ProfileCell 
    cell.fillCells(uid: uid!, userPost: userImages) 
    return cell 
} 


func numberOfSections(in collectionView: UICollectionView) -> Int { 
    return 1 
} 


func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return userposts.count 


} 

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 


     let selecteditem : String! 
     selecteditem = userposts[indexPath.row] 
     performSegue(withIdentifier: "lol", sender: selecteditem) 

} 

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    if segue.identifier == "lol" { 
     if let detailvc = segue.destination as? PhotoDetailVC { 
      if let bro = sender as? String { 
       detailvc.image = bro 
      } 
     } 
    } 
} 


func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { 

    let view = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "ProfileHeader", for: indexPath) as! ProfileHeader 
    view.delegate = self 
    return view 
} 




func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 

     let width = (collectionView.bounds.size.width/3) - 1 
     print(width) 
     let size = CGSize(width: width, height: width) 
     return size 

} 

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

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

不要忘記編輯此方法並傳入selfHeaderView

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { 

    let view = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "ProfileHeader", for: indexPath) as! ProfileHeader 
    view.delegate = self 
    return view 
} 
+0

非常感謝您的答覆! 我試着實現這一點,它顯示我錯誤'意外地發現零,而解包可選值。 –

+0

可能是你的代表是nill,儘量使用FUNC setDelegate()方法 –

+0

是的,我試過了。但它顯示了'self.delegate.fabItemClicked()'上的同樣的錯誤。在添加頭類後,是否需要在ProfileVC中設置'delegate = self'。 –