0

I do news app in swift 2.3, xcode 7.3.1. I have Realm DB and data therein from server. I need move to ViewController when I press button and display selected data(news). The button over by my elements of news. How to pass data from selected elements of news to show details on another ViewController?

This is news elements. The button over by elements of with horizontal scrolling: enter image description here

This is code:

import UIKit
import RealmSwift

let realm4 = try! Realm()

class MainViewController: UIViewController, HorizontalScrollDelegate {

let newsObj = realm4.objects(News)
let nObj = News()

var imageURL: NSURL?

override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
    super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

override func viewDidLoad() {
    super.viewDidLoad()

    let hScroll = HorizontalScroll(frame: CGRectMake(0, 0, 380, 160))
    hScroll.delegate = self
    hScroll.backgroundColor = UIColor.whiteColor()
    self.view.addSubview(hScroll)
    view.reloadInputViews()

}
   func numberOfScrollViewElements() -> Int {
        return newsObj.count
    }

    func elementAtScrollViewIndex(index: Int) -> UIView {

        let indexes = newsObj[index]

        let view = UIView(frame: CGRectMake(5.0, 0.0, 200.0, 200.0))
        var imageView = UIImageView()
        let imageLabel = UIImageView()
        let newsLable = UILabel()
        let button = UIButton()

        var image: UIImage? {

            get { return imageView.image }
            set {
                imageView.image = newValue
                imageView.sizeToFit()
            }
        }

        newsLable.text = indexes.newsTitle

        imageURL = NSURL(string: indexes.newsImage)

        if let url = imageURL {
            let imageData = NSData(contentsOfURL: url)
            if imageData != nil {
                image = UIImage(data: imageData!)
            }
        }

        newsLable.lineBreakMode = .ByWordWrapping
        newsLable.font = UIFont(name: "Roboto-Bold", size: 18)
        newsLable.textColor = UIColor.whiteColor()
        newsLable.frame = CGRectMake(7.0, 90.0, 200.0, 50.0)
        newsLable.textAlignment = .Left
        newsLable.numberOfLines = 0

        button.frame = CGRectMake(0.0, 0.0, 200.0, 200.0)
        button.addTarget(indexes, action: #selector(tapAction2), forControlEvents: .TouchUpInside)
        print("This is 1 nObj \(self.nObj)")

        imageLabel.backgroundColor = UIColor.blackColor()
        imageLabel.alpha = 0.45
        imageLabel.frame = CGRectMake(5.0, 90.0, 200.0, 65.0)
        imageView.frame = view.frame

        view.addSubview(imageView)
        view.addSubview(imageLabel)
        view.addSubview(newsLable)
        view.addSubview(button)

        return view
    }

func tapAction2(index: Int) {
    print("This is func INDEX \(index)")
    //let segueIndex = index
    let storyBoard = UIStoryboard(name: "Main", bundle: nil)
    let newView = storyBoard.instantiateViewControllerWithIdentifier("NewsDetailViewController") as! NewsDetailViewController
    newView.modalPresentationStyle = UIModalPresentationStyle.Custom
    print("This is nObj \(self.nObj)")
    print("This is INDEX \(index)")
    newView.newsOfTitle = String(index)
    presentViewController(newView, animated: true, completion: nil)
}
}

I try to pass data with index. But in console I have empty data. Maybe problems with Realm. In console I see

This is INDEX 140659923446288

But I have not this data in my project

This is my Realm DB

enter image description here

Janserik
  • 2,306
  • 1
  • 24
  • 43

1 Answers1

0

As stated in UIButton Class Reference, the signature of an action method takes one of three forms

@IBAction func doSomething()
@IBAction func doSomething(sender: UIButton)
@IBAction func doSomething(sender: UIButton, forEvent event: UIEvent)

but not func doSomething(index: Int).

That's why you get the wrong index in your method.

Dmitry
  • 7,300
  • 6
  • 32
  • 55
  • What I need to do? – Janserik Oct 13 '16 at 09:49
  • 1
    You need to change your `tapAction2()` method to take a `UIButton` argument instead of an `Int` argument. Buttons don't report their indexes by default. You will need to write logic to figure out what index corresponds to what `UIButton` instance. – AustinZ Nov 22 '16 at 01:41