我是 iOS 和 Swift 的新手,非常感谢。
我想通过tripListData
将addNewWishViewController
传递给第二个 ViewController (prepareForSegue
):
var tripListData: NSMutableArray = NSMutableArray()
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "goToAddNewWishVC" {
var selectedIndexPath: NSIndexPath = self.tableView.indexPathForSelectedRow()!
var addNewWishViewController: AddNewWishViewController = segue.destinationViewController as AddNewWishViewController
addNewWishViewController.tripDataFromTripList = tripListData.objectAtIndex(selectedIndexPath.row) as? NSDictionary
println(tripListData.objectAtIndex(selectedIndexPath.row))
println(addNewWishViewController.tripDataFromTripList)
}
}
没有错误,但第二个println
始终返回nil
。因此,它在第二个 ViewController 中崩溃,因为tripDataFRomTripList
是nil
。以下是println
结果:
<Destination: 0x7f9401cd84f0, objectId: nCK0VvwueZ, localId: (null)> {
dateAdd = "29/01/2015";
dateFly = "28/02/2015";
destinationCity = Frankfurt;
sourceCity = Hong Kong;
user = "<PFUser: 0x7f9401dc6d00, objectId: 71cgaaZcvw>";
}
nil
这是addNewWishViewController
class AddNewWishViewController: UIViewController {
@IBOutlet var newWishList: UITextView! = UITextView()
@IBOutlet var tripInfoTextField: UILabel!
var tripDataFromTripList:NSDictionary? = NSDictionary()
override func viewDidLoad() {
super.viewDidLoad()
if tripDataFromTripList == nil{
println("nodata")
}else{
println(tripDataFromTripList!)
}
出现这种情况的原因是什么? 再次感谢:)