Ali kazemi
Ali kazemi

Reputation: 84

how pass data on prepareForSegue function differently? swift 5

Hi everyOne actually I want to add Data of the cell in tableView that when I click on it , data should pass on showOfferController(another view controller) but I dont why my data is nil when passed on showOfferController:

this is my prepareForSegue function :

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
       if segue.identifier == "cellToShow" {
          
               let prePag = segue.destination as! ShowOfferController
            //print("title : \(title)")
            if WelcomeJson.CodingKeys.title.rawValue == titleItem {
                prePag.celanderItem = WelcomeJson.CodingKeys.dateOfExpire.rawValue
                prePag.itemImage[0] = imageString
                prePag.itemAddress = WelcomeJson.CodingKeys.address.rawValue
                prePag.itemOffer = WelcomeJson.CodingKeys.offer.rawValue
                prePag.itemDefine = WelcomeJson.CodingKeys.about.rawValue
           }
        
       }
   }

And also this is my DidSelectRow TableView function :

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    
    
    performSegue(withIdentifier: "cellToShow", sender: nil)
    
}

this is my ShowOfferController on main.storyBoard and also my viewController or VC1 that tabelViewCell point to ShowOfferController:

enter image description here

And here my ShowOfferController.swift code :

class ShowOfferController: UIViewController, UIScrollViewDelegate {


@IBOutlet weak var offerScroll: UIScrollView!

@IBOutlet weak var offerPageController: UIPageControl!

@IBOutlet weak var getLabel: UILabel!

@IBOutlet weak var percentLbl: UILabel!
@IBOutlet weak var calendar: UILabel!
@IBOutlet weak var address: UILabel!
let locations = NSTextAttachment()
let calendars = NSTextAttachment()
let precents = NSTextAttachment()
var itemAddress : String = ""
var itemOffer : String = ""
var celanderItem : String = ""
var itemImage : [String] = []
var itemDefine : String = ""

var frame: CGRect = CGRect(x:0, y:0, width:0, height:0)

override func viewDidLoad() {
    super.viewDidLoad()
   print("item of images is : \(itemImage)")
    // Do any additional setup after loading the view.
    
    offerPageController.numberOfPages = itemImage.count
    
    setupImages()
    offerScroll.delegate = self
    getLabel.text = itemDefine
    locations.image = UIImage(systemName: "location.fill")
    let locationVaAdress = NSMutableAttributedString(string: ". ")
    locationVaAdress.append(NSAttributedString(attachment: locations))
    locationVaAdress.append(NSAttributedString(string: "   \(itemAddress)"))
    address.attributedText = locationVaAdress
    calendars.image = UIImage(systemName: "calendar")
    let calendarss = NSMutableAttributedString(string: ". ")
    calendarss.append(NSAttributedString(attachment: calendars))
    calendarss.append(NSAttributedString(string: "   \(celanderItem)"))
    calendar.attributedText = calendarss
    precents.image = UIImage(systemName: "percent")
    let percentAndDarsad = NSMutableAttributedString(string: ". ")
    percentAndDarsad.append(NSAttributedString(attachment: precents))
    percentAndDarsad.append(NSAttributedString(string: "   \(itemOffer)"))
    percentLbl.attributedText = percentAndDarsad
    
}

func setupImages() {
    
    for index in 0..<itemImage.count {
        // 1.
        frame.origin.x = offerScroll.frame.size.width * CGFloat(index)
        frame.size = offerScroll.frame.size
        
            let correctScope = itemImage[index].addingPercentEncoding(withAllowedCharacters: .urlPathAllowed)
            let unoptionalScope = correctScope!
            let imgUrl = URL(string: "http://5.63.13.16:8080/\(unoptionalScope)")!
            if let data = try? Data(contentsOf: imgUrl) {
                let imgOffer = UIImageView(frame: frame)
                imgOffer.image = UIImage(data: data)
                self.offerScroll.addSubview(imgOffer)
            }
        
        offerScroll.contentSize = CGSize(width: (offerScroll.frame.size.width * CGFloat(itemImage.count)), height: offerScroll.frame.size.height)
           offerScroll.delegate = self
    }
    
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
    let pageNumber = offerScroll.contentOffset.x / offerScroll.frame.size.width
    offerPageController.currentPage = Int(pageNumber)
} 

so what's your idea ?

Upvotes: -1

Views: 56

Answers (1)

Alhomaidhi
Alhomaidhi

Reputation: 666

You should make (drag) the segue from on top of the table view controller in the storyboard instead of from the row

Upvotes: 0

Related Questions