在swift中使用跨类变量
我正在尝试使用基于所选表格单元的信息填充详图视图控制器。我试图做我认为可能有效的事情,但在detail view controller类中,在表示if(detail)的行中出现了错误“使用未解析标识符'detail'在swift中使用跨类变量,swift,identifier,Swift,Identifier,我正在尝试使用基于所选表格单元的信息填充详图视图控制器。我试图做我认为可能有效的事情,但在detail view controller类中,在表示if(detail)的行中出现了错误“使用未解析标识符'detail' @IBOutlet var detailText: UILabel! override func viewDidLoad() { super.viewDidLoad() if (detail = "conferenceapp") { self
@IBOutlet var detailText: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
if (detail = "conferenceapp") {
self.detailText.text = "lol"
}
if (detail = "spaceshooter") {
self.detailText.text = "spaceshooter"
}
展位结构
struct Booth {
let category : String
let name : String
let detail : String
}
表视图控制器类代码段
var booths = [Booth]()
var filteredBooths = [Booth]()
override func viewDidLoad() {
super.viewDidLoad()
//fill array with data
self.booths = [Booth(category: "Tech", name: "Conference App", detail: "conferenceapp"),
Booth(category: "Tech", name: "Space Shooter", detail: "spaceshooter"),
Booth(category: "Tech", name: "RollABall", detail: "rollaball"),
Booth(category: "Animation", name: "Sugar Hill City Model", detail: "sugar"),
Booth(category: "Animation", name: "3D Sculpting 101", detail: "3d"),
Booth(category: "Animation", name: "HowTo - Texture", detail: "howto"),
Booth(category: "Science", name: "AP Biology for Dummies", detail: "apbio"),
Booth(category: "Science", name: "Cells", detail: "cells"),
Booth(category: "Science", name: "Space", detail: "space")]
请提供帮助,我需要修复错误将数据传递到详细信息视图的常用方法是通过prepareForSegue函数。示例:
import UIKit
class MasterViewController: UITableViewController {
let items = ["Bob","Joe"]
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Item", forIndexPath: indexPath) as UITableViewCell
cell.textLabel.text = items[indexPath.row]
return cell
}
//Passing details to detail VC
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let indexPath = self.tableView.indexPathForSelectedRow()
let theDestination = (segue.destinationViewController as DetailViewController)
theDestination.itemName = items[indexPath!.row]
}
}
import UIKit
class DetailViewController: UIViewController {
@IBOutlet weak var nameLabel: UILabel!
var itemName = ""
override func viewDidLoad() {
super.viewDidLoad()
nameLabel.text = itemName
}
}
问题是,你说的是
if(detail=“conference app”)
,但没有定义“detail”。哪里有“detail”“变量应该来自哪里?您是否试图参考展位的详细信息
?然后你需要一个摊位,你需要指定它的细节。但你们并没有展位,在任何情况下,仅仅说“细节”作为一个简单的词是不会涉及到它的细节的
另外,我想知道您的意思是
if(detail==“会议应用程序”)
。你不能像现在这样使用一个等号。我正在尝试访问其中一个展位的展位结构中的“细节”(类别:“Tech”,名称:“Space Shooter”,细节:“spaceshooter”),但是,正如我所说,这假设你有一个展位对象的参考。你没有。即使你这样做了,你也不能只说细节
。你需要说someBooth.detail
(如果“someBooth”是你的Booth对象的名称)。我正在尝试发送Booth结构中的细节,这些细节设置为等于字符串,如so self.booths=[Booth(category:“Tech”,name:“Conference App”,detail:“conferenceapp”),在detail类中,我必须检查“detail”等于在这些展位(类别:“技术”、名称:“太空射手”、细节:“太空射手”)、展位(类别:“技术”、名称:“滚球”、细节:“滚球”)、展位(类别:“动画”、名称:“糖山城市模型”、细节:“糖”)、展位(类别:“动画”、名称:“3D雕刻101”,细节:“3D”),
您仍然按照我向您展示的方式进行操作。我假设表中的每一行都从数组中获取一个元素?如果是这样,则在我的示例中传递的是booth[indexPath!.row],而不是items[indexPath!.row]