我在swift 2.3
,xcode 7.3.1
开设了新闻应用。我有Realm DB
和服务器中的数据。当我按下按钮并显示所选数据(新闻)时,我需要移动到ViewController
。按钮由我的新闻元素组成。如何从选定的新闻元素传递数据以显示另一个ViewController
的详细信息?
这是代码:
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)
}
}
我尝试使用索引传递数据。但在控制台我有空数据。可能是Realm
的问题。
在控制台中我看到了
这是INDEX 140659923446288
但我的项目中没有这个数据
这是我的Realm DB
答案 0 :(得分:0)
如UIButton Class Reference所述,动作方法的签名采用三种形式之一
@IBAction func doSomething()
@IBAction func doSomething(sender: UIButton)
@IBAction func doSomething(sender: UIButton, forEvent event: UIEvent)
但不是func doSomething(index: Int)
。
这就是你在方法中得到错误索引的原因。