도움이 필요합니다. popover에서 선택한 사진을 mainController의 UIImageView
으로 전달할 때마다 항상 이 아닌이 표시됩니다. 그 문제를 해결하는 방법? 저를 도와주세요, 나는 그것을 해결하기 위해 하루를 보내고 실패했습니다. 여기왜 내 UIImageView는 항상 nil입니까?
import Foundation
import UIKit
class AddAdvertise: UIViewController {
var data = SkelbimasModel.init()
var nuotraukuKiekis = [UIImage]()
//pazymetu nuotrauku placeholderiai
@IBOutlet weak var ImagePlaceHolder00: UIImageView!
@IBOutlet weak var ImagePlaceHolder01: UIImageView!
@IBOutlet weak var ImagePlaceHolder02: UIImageView!
@IBOutlet weak var ImagePlaceHolder03: UIImageView!
@IBOutlet weak var ImagePlaceHolder04: UIImageView!
@IBOutlet weak var ImagePlaceHolder05: UIImageView!
@IBOutlet weak var NavBar: UINavigationBar!
override func viewDidLoad() {
super.viewDidLoad()
ImagePlaceHolder00.image = UIImage(named: "ok.png")
NavBar.topItem?.title = data.kategorija
self.view.backgroundColor = data.Color
}
@IBAction func ClickToPhotos(_ sender: Any) {
let popOverVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Pictures") as! PicturesCollectionViewController
self.addChildViewController(popOverVC)
popOverVC.view.frame = self.view.frame
self.view.addSubview(popOverVC.view)
popOverVC.didMove(toParentViewController: self)
}
func NuotraukuKiekis(pazymetuNuotraukuKiekis: [UIImage]){
for nuotrauka in pazymetuNuotraukuKiekis {
nuotraukuKiekis.append(nuotrauka)
}
ImagePlaceHolder00.image = nuotraukuKiekis[0] // <<<------ Here i get NIL
// ImagePlaceHolder01.image = pazymetuNuotraukuKiekis[1]
// ImagePlaceHolder02.image = pazymetuNuotraukuKiekis[2]
// ImagePlaceHolder03.image = pazymetuNuotraukuKiekis[3]
// ImagePlaceHolder04.image = pazymetuNuotraukuKiekis[4]
//ImagePlaceHolder05.image = pazymetuNuotraukuKiekis[5]
}
}
그리고 내 팝 오버 코드입니다 :
import UIKit
import Photos
class PicturesCollectionViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource{
@IBOutlet weak var ColView: UICollectionView!
private let reuseIdentifier = "Cell"
var imageArray = [UIImage]()
var selectedImages = [UIImage]()
func grabPhotos() {
let imgManager = PHImageManager.default()
let requestOption = PHImageRequestOptions()
requestOption.isSynchronous = true
requestOption.deliveryMode = .highQualityFormat
let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]
let fetchResult : PHFetchResult = PHAsset.fetchAssets(with: .image, options: nil)
if(fetchResult.count > 0) {
for i in 0..<fetchResult.count {
imgManager.requestImage(for: fetchResult.object(at: i) , targetSize: CGSize.init(width: 105, height: 105), contentMode: .aspectFill, options: requestOption, resultHandler: {(image, error) in
self.imageArray.append(image!)
})
}
print(imageArray.count)
print("Second : \(imageArray[0])")
} else {
ColView.reloadData()
}
}
override func viewDidLoad() {
super.viewDidLoad()
//isvalom pazymetas nuotraukas.
selectedImages.removeAll()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Register cell classei
let nib = UINib(nibName: "Cell", bundle: nil)
ColView.register(nib, forCellWithReuseIdentifier: reuseIdentifier)
grabPhotos()
ColView.allowsMultipleSelection = true
ColView.allowsSelection = true
}
@IBAction func IkeltiNuotraukas(_ sender: Any) {
let vc = AddAdvertise()
vc.NuotraukuKiekis(pazymetuNuotraukuKiekis: selectedImages)
self.view.removeFromSuperview()
}
//Paspaudus mygtuka, siunciam pazymetas nuotraukas atgal i AddAdvertise controleri
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return imageArray.count
}
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)
as! Cell
cell.PlaceHolder.image = imageArray[indexPath.row]
cell.backgroundColor = UIColor.black
return cell
}
// MARK: UICollectionViewDelegate
/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
override func collectionView(_ collectionView: UICollectionView, shouldHighlightItemAt indexPath: IndexPath) -> Bool {
return true
}
*/
public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath) as! Cell
if(selectedImages.count < 6){
cell.OK.isHidden = false
selectedImages.append(imageArray[indexPath.row])
print(selectedImages.count)
}
}
public func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath) as! Cell
cell.OK.isHidden = true
let selectedImage = cell.PlaceHolder.image
if(selectedImages.contains(selectedImage!)){
selectedImages.remove(at: selectedImages.index(of: selectedImage!)!)
}
}
// Uncomment this method to specify if the specified item should be selected
public func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
return true
}
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
public func collectionView(_ collectionView: UICollectionView, shouldShowMenuForItemAt indexPath: IndexPath) -> Bool {
return false
}
public func collectionView(_ collectionView: UICollectionView, canPerformAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) -> Bool {
return false
}
public func collectionView(_ collectionView: UICollectionView, performAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) {
print("Clicked")
}
}
When i call this function from button in Popover:
@IBAction func IkeltiNuotraukas(_ sender: Any) {
let vc = AddAdvertise()
vc.NuotraukuKiekis(pazymetuNuotraukuKiekis: selectedImages)
self.view.removeFromSuperview()
}
그것은 AddAdvertise 이미지 뷰에 이미지를 전달하는 가정이
여기 내 AddAdvertise.swift입니다.
마지막 함수 인 Ikelt가 ........ vc.loadViewIfNeeded()를 호출합니다. – zombie
코드에서 'self.selectedImages'를 사용하십시오. – Torongo
안녕하세요, 좀비가 당신을 이해하지 못합니다 :) –