I have the following seque. This goes into the ViewImages storyboard.
if segue.identifier == "ShowImagesSeque", let destination = segue.destination as? ViewImages, let itemIndex = tableView.indexPathForSelectedRow?.row {
let selectedItem = self.currentItemsArray[itemIndex]
destination.itemId = selectedItem.itemID
}
Here is the ViewImages class. In the story board, the class is there for the ViewController. When I print out itemId, its nil. Why?
class ViewImages: UIViewController {
...
weak var mDelegate:MyProtocol?
var itemId: String!
...
}
Related
I'm doing a network request twice, in the view controllers connected to a tab bar. If I can do the network request directly in the tab bar controller, I can pass the data from there in just one network request.
I tried this but it is giving a lot of errors.
- var pages : [page] = [] is an array of structs that I want to pass. It is working fine, I just need to figure out the passing part.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let feedVC = segue.destination as! FeedViewController
let categoriesVC = segue.destination as! CategoriesGridViewController
feedVC.pages = pages as! [[String:Any]]
categoriesVC.pages = pages [[String:Any]]
}
Screenshot of my StoryBoard
I fetch from a database of images and their information. I make an array of structs and use that to display the data in the App. All of that is working fine.
Page -
struct page {
var urlsArray : [String]
var caption : String
var title : String
var time : String
}
Without seeing what data you are actually trying to pass, I would guess you should create a Class (not struct) of type Page (or whatever you want to call it) and pass an array of [Page] through the view controllers. Here's an example of how to set it up:
class Page {
private var _title: String
private var _otherVariable: Int
var title: String {
return _title
}
var otherVariable: Int {
return _otherVariable
}
init(title: String, otherVariable: Int) {
self._title = title
self._otherVariable = otherVariable
}
}
//IN VIEW CONTROLLER 1
//when you get the data, set data for a Page
let item = Page(title: "Example", otherVariable: 12345)
let item2 = Page(title: "Example2", otherVariable: 67890)
//call this to segue
func segueToFeedViewController() {
let dataToSendToNextViewController: [Page] = [item, item2] //add data here
performSegue(withIdentifier: "ToFeedViewController", sender: dataToSendToNextViewController)
}
//use if let for safety
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let newVC = segue.destination as? FeedViewController {
assert(sender as? [Page] != nil)
newVC.pages = sender as! [Page]
}
}
I'm new to ios dev and i'm trying to develop my first app! :)
so far so good...
Now I'm trying to pass data from main controller to the item details view.
It seems i can't figure out what i'm doing wrong but i keep getting an error:
"fatal error: unexpectedly found nil while unwrapping an Optional value"
now i understand that the value is not assigned but i don't understand why...
here's my code:
-------> MainVC
struct Ananlysys {
var descrizione: String!
var data: String!
var immagine: String!
var seganle: String!
var oraUpload: String!
var valuta: String!
}
var analysisList = [Ananlysys]()
var alertsRef = FIRDatabase.database().reference().child("analisi")
override func viewDidLoad() {
super.viewDidLoad()
fetchDataFromDB()
}
func fetchDataFromDB(){
alertsRef.observe(.childAdded, with: { snapshot in
if let dict = snapshot.value as? [String: AnyObject] {
let descrizione = dict["descrizione"] as! String
let data = dict["data"] as! String
let stato = dict["stato"] as! String
let immagine = dict["imageURL"] as! String
let seganle = dict["segnale"] as! String
let oraUpload = dict["oraupload"] as! String
let valuta = dict["valuta"] as! String
self.analysisList.insert(Ananlysys(descrizione: descrizione,
data:data, immagine:immagine, seganle: seganle, oraUpload: oraUpload,
valuta: valuta), at: 0)
self.tableView.reloadData()
}
})
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "ItemDetailVC"{
if let indexPath = self.tableView.indexPathForSelectedRow {
let destination = segue.destination as! ItemDetailVC
let cell = tableView.cellForRow(at: indexPath) as? ItemCell
destination.valuta = (cell?.valutaLabel.text)!
destination.segnale = (cell?.signalLabel.text)!
destination.desc = (cell?.descriptionLabel.text)!
destination.immagine = (cell?.imageThumb.image)!
}
}
And in My ItemDetailsVC controller (which is the destination) i've just created the IBoutlet. Here' the code:
class ItemDetailVC: OpenAlertsVC {
#IBOutlet weak var crossLabel: UILabel!
#IBOutlet weak var signalLbl: UILabel!
#IBOutlet weak var imageDetail: UIImageView!
#IBOutlet weak var analysisDesc: UILabel!
var valuta = ""
var segnale = ""
var immagine: UIImage!
var desc = ""
}
override func viewDidLoad() {
super.viewDidLoad()
crossLabel.text = valuta
signalLbl.text = segnale
analysisDesc.text = desc
imageDetail.image = immagine
}
Probably i'm just doing some stupid error... :) but it's my first app and i really don't know how to figure this out!
If anybody could help that would be much appreciate! :)
Thank you!
Cheers!
UPDATE:
I think my problems is because of the image.
I have an url which i retrive from firebase in this way:
let url = analysisList[indexPath.row].immagine
and then i download async the images:
cell.imageThumb.downloadImageFrom(link: url!, contentMode:
UIViewContentMode.scaleToFill)
in the segue i do this:
destination.immagine = (cell?.imageThumb.image)!
and in my DetailVC:
var immagine: UIImage!
imageDetail.image = immagine
this is the screen of the error
enter image description here
Saw this recently passing variables between views. I was able to get them moving by first setting the value to a top level variable (when a row is selected) and then re-reference that variable during the segue prepare function. Hope this helps.
Set a top level variable:
var valuta: String!
Breakout the self.tableView.indexPathForSelectedRow section into it's own delegate. This tutorial is a great example. (http://shrikar.com/swift-ios-tutorial-uisearchbar-and-uisearchbardelegate)
Using the "didSelectRowAt indexPath" delegate, set the top level variable value.
func tableView(_ tableView: UITableView, didSelectRowAt IndexPath: IndexPath){
let cell = tableView.cellForRow(at: indexPath)!
valuta = cell?.valutaLabel.text
}
Sample adjustments to the prepare function:
override func prepare(for segue: UIStoryboardSegue, sender: Any?){
if segue.identifier == "ItemDetailVC"{
if let destination =
segue.destination as? name_of_your_next_view_controller {
destination.passedValuta = valuta
}
}
Make sure to put a receiving variable in the next view
var passedValuta: String!
I have like a social app with a sort of newsfeed. if u click on the users name from a post in the newsfeed, you will go to his profile. Now i can't retrieve the data from that specific cell/post to the other viewController.
so i have to display the user's profile, with he's username, etc. but that doesn't work?
i have a Post model:
class Post {
private var _postDescription: String!
private var _profileImageURL: String?
private var _likes: Int!
private var _username: String!
private var _postKey: String!
private var _timeStamp: String!
private var _postRef: Firebase!
var postDescription: String? {
return _postDescription
}
var likes: Int {
return _likes
}
var username: String {
return _username
}
var postKey: String {
return _postKey
}
var profileImageURL: String? {
return _profileImageURL
}
init(description: String, username: String, profileImageURL: String?) {
self._postDescription = description
self._username = username
self._profileImageURL = profileImageURL
}
init(postKey: String, dictionary: Dictionary<String, AnyObject>) {
self._postKey = postKey
if let likes = dictionary["likes"] as? Int {
self._likes = likes
}
if let desc = dictionary ["description"] as? String {
self._postDescription = desc
}
if let imgUrl = dictionary["profileImg"] as? String {
self._profileImageURL = imgUrl
}
if let user = dictionary ["username"] as? String {
self._username = user
} else {
self._username = ""
}
self._postRef = DataService.ds.REF_POST.childByAppendingPath(self._postKey)
}
}
this is my profileVC:
class ProfileVC: UIViewController {
#IBOutlet weak var username: UILabel!
var post: Post?
override func viewDidLoad() {
super.viewDidLoad()
username.text = post.username // gives me a nil error.
}
}
and i use a TapGestureRecognizer in my tableViewCell to perform the segue.
in my cellForRowAtIndexPath:
let profileLblTapRecognizer = UITapGestureRecognizer(target: self, action: #selector(NewsVC.goToProfileScreen(_:)))
profileLblTapRecognizer.numberOfTapsRequired = 1
profileLblTapRecognizer.delegate = self
cell.usernameLabel.tag = indexPath.row
cell.usernameLabel.userInteractionEnabled = true
cell.usernameLabel.addGestureRecognizer(profileLblTapRecognizer)
and the goToProfileScreen function:
func goToProfileScreen(gesture: UITapGestureRecognizer) {
self.performSegueWithIdentifier("ProfileScreen", sender: self)
}
this is my datamodel on firebase:
UPDATE:
i tried this instead:
let profileLblTapRecognizer = UITapGestureRecognizer(target: self, action: #selector(NewsVC.prepareForSegue(_:sender:)))
with this function:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "ProfileScreen" {
if let cell = sender as? NewsCell, row = tableView.indexPathForCell(cell)?.row, vc = segue.destinationViewController as? ProfileVC {
vc.post = posts[row]
}
}
}
but that gave me an error on appDelegate: Thread 1: EXC_BAD_ACCESS(code=1, address = 0x1)
I've added this as an answer rather than a comment so that I can add and format some code examples.
When you call performSegueWithIdentifier, a NEW instance of the view controller identified by that segue is created, so all of its properties will be their defaults.
You have two ways of instantiating this view controller and setting properties before it loads. The first is the prepareForSegue option, in your case it may look something like this:
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if (segue.identifier == "ProfileScreen") {
let vc = segue.destinationViewController as! ProfileVC
vc.post = post
}
}
Another option is to create and present the view controller yourself, this example uses a storyboardID
let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc = mainStoryboard.instantiateViewControllerWithIdentifier("profileVC") as! ProfileVC
vc.post = post
presentViewController(vc, animated: false, completion: nil)
Update:
I'm not sure why you are adding a tap gesture recogniser to this, you could just use didSelectRowAtIndexPath, have a look at this other question and answer
you could have a property on your table view controller called selectedItem or something similar. and then in didSelectRowAtIndexPath set selectedItem to the item at the current index. Then in prepare for segue you would just do vc.post = selectedItem
Update Two:
After the op sharing their code privately, I noticed that the issue is that the user is using tapGestureRecogniser in the tableView. I added some code into the called function to get the row in which contained the tapped view, once I had the indexPath it was then easy to store it in a temporary property and retreive later in the prepareForSegue method, details below
// temp property
var selectedPost:Post?
// function called on tap
func viewProfile(sender:UITapGestureRecognizer) {
if (sender.state == UIGestureRecognizerState.Ended) {
let point = sender.locationInView(self.tableView)
if let indexPath = self.tableView.indexPathForRowAtPoint(point) {
selectedPost = self.posts[indexPath.row]
performSegueWithIdentifier("ProfileScreen", sender: self)
}
}
}
// Prepare for segue
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "ProfileScreen") {
let vc = segue.destinationViewController as! ProfileVC
if let post = selectedPost {
vc.post = post
}
}
}
When trying to pass String variables to a new ViewController, only one of the variables is being sent. I'm not sure what is going on here, because when I print object["eventDescription"], the console does indeed print the description. Only eventName is being passed to the new view controller.
Sending view controller
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "detailSegue") {
if let destination = segue.destinationViewController as? EventDetailViewController {
if let eventIndex = self.tableView.indexPathForCell(sender as! FeedCustomCell) {
var object = self.eventsArray[eventIndex.row]
destination.nameText = object["eventName"] as! String
destination.descriptionText = object["eventDescription"] as! String
print(object["eventDescription"])
The destination view controller (EventDetailViewController):
class EventDetailViewController: UIViewController {
#IBOutlet weak var eventName: UILabel!
#IBOutlet weak var descLabel: UILabel!
var descriptionText = String()
var nameText = String()
override func viewDidLoad() {
super.viewDidLoad()
self.eventName.text = nameText
self.descLabel.text = descriptionText
}
}
Sorry for wasting anybody's time, but I had to place constraints onto the description UILabel in the EventDetailController, and then I could see the description.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if segue.identifier == "shoesDetails" {
let indexPath = self.table.indexPathForSelectedRow()
let shoesCon:ShoesTableControler = segue.destinationViewController as! ShoesTableControler
let shoesCategories:RepoSitory = arrofRepository[indexPath!.row]
shoesCon.object = shoesCategories
}
}
class ShoesTableControler: UIViewController {
var object = [ProductShoes]()
}
class ProductShoes {
var product_id : String
var product_name : String
init(json :NSDictionary) {
self.product_id = json["product_id"] as! String
self.product_name = json["product_name"] as! String
}
class RepoSitory {
var name : String
var ids : String
init(json :NSDictionary) {
self.name = json["category_name"] as! String
self.ids = json["id"] as! String
}
}
I have 3 classes in firstViewController. We show the data it's done. After that, when user click on specific cell, then show the data. Now I have created the ShoesTableController and in ShoesTableController we create the Product Class obj. I want to access this. The problem is in this line in Objective C. I am doing this and it is working perfect but in swift it does know what happened:
shoesCon.object = shoesCategories
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if segue.identifier == "shoesDetails" {
let indexPath = self.table.indexPathForSelectedRow()
let shoesCon:ShoesTableControler = segue.destinationViewController as! ShoesTableControler
let shoesCategories:RepoSitory = arrofRepository[indexPath!.row]
shoesCon.object = shoesCategories
}
}
class ShoesTableControler: UIViewController {
var object : RepoSitory?
its solve my problem i have my own mistake sorry for posting this question