I am making an application of the news portal swift 2.3 and xcode 7.3.1. I use Realm . I have news in the horizontal scroll and when I click on one of them I need to go to its detailed view and display the news in NewsDetailViewController . I can not display the selected item and its detailed information.
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() // Uncomment the following line to preserve selection between presentations //self.clearsSelectionOnViewWillAppear = false // Register cell classes 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(self, action: #selector(tapAction2), forControlEvents: .TouchUpInside) button.indexOfAccessibilityElement(indexes) button.accessibilityElementAtIndex(index) 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(index2: Int) { //let indexes2 = newsObj[index2] print("This is func INDEX2 \(index2)") let segueIndex = index2 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 \(index2)") newView.newsOfTitle = String(segueIndex) presentViewController(newView, animated: true, completion: nil) } } Here is the horizontal scroll
Here is the data in Realm
Target ViewController code:
import UIKit import RealmSwift class NewsDetailViewController: UIViewController { var sliderMenu = SliderMenu() var newsOfTitle: String? @IBAction func newsDetailMenuButton(sender: UIButton) { showSliderMenu() } @IBAction func newsDetailLoginButton(sender: UIButton) { let newView = self.storyboard!.instantiateViewControllerWithIdentifier("LoginViewController") as! LoginViewController newView.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext self.presentViewController(newView, animated: true, completion: nil) } @IBAction func newsDetailSearchButton(sender: UIButton) { } @IBOutlet weak var newsDetailImage: UIImageView! @IBOutlet weak var newsDetailTitle: UILabel! @IBOutlet weak var newsDetailFulltext: UILabel! override func viewDidLoad() { newsDetailTitle.text = newsOfTitle let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(NewsController.handleSwipes(_:))) rightSwipe.direction = .Right view.addGestureRecognizer(rightSwipe) } func handleSwipes(sender:UISwipeGestureRecognizer) { if (sender.direction == .Right) { print("Swipe Right") dismissView() } } func dismissView() { self.dismissViewControllerAnimated(true, completion: nil) } func showSliderMenu() { sliderMenu.showSliderMenu() } } 
