Unable to call embedded segue the second time - ios

I'm trying to build an app similar to Instagram, and I'm stuck at the comments portion. On the left side of the image, it is a VC that has an embedded container view with UITextField and UIButton at the bottom. The container view is embedded with a UITableView that contains all the user profile image, username and the comment itself.
At first load, it can perfectly grab all comments for that post from server side, and displayed perfectly. However, I am unable to call the segue again using prepareForSegue to update the UITableView. I'm receiving an error:
There are unexpected subviews in the container view. Perhaps the embed
segue has already fired once or a subview was added programmatically?
Below are my codes:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "getCommentSegue"){
if let destination = segue.destinationViewController as? CommentsTVC{
if(!self.commentID.isEmpty){
destination.UpdateCommentRow(self.profileImage, commentID: self.commentID, comment: self.postComment, dateTimePost: self.dateTime)
self.commentID = String()
}
destination.postID = self.postID
}
}
}
And after successfully adding a row to my database in the dispatch_async:
self.performSegueWithIdentifier("getCommentSegue", sender: nil)
I've also noticed that when it append new comment to my existing object that stores all the comments, the count for it is 0. I believe it is taking a new reference for the object. Please help!

embed navigationcontroller to container view and then try. It may solve youar problem i think..!!

Remove your container view's subviews before you call performSegue.

Related

Swift 3: How to make sure that data is set before presenting in next view controller

I am using prepare for segue to set a dictionary variable in another view controller. But the issue is the view controller opens the map without the data. How can I make sure I go to next view controller only after the dictionary variable data has been set.
Here is my code
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showMapSegue"
{
if let mapVC = segue.destination as? MapVC
{
if vendorStoreKeys.count != 0{
mapVC.vendorStore = vendorStoreKeys
}
}
}
}
But the vendor store array in next view controller is still empty when the controller is visible. It sets the data after some time but at that point I am unable to reload that view controller so I need to make sure I don't go to next view controller before the mapVC.vendorStore variable is properly set.
I was able to resolve the issue. Prepare for should have set the data before calling and it was. Issue was I actually forgot to remove the segue in the storyboard which was directly going from the button to the new view controller. So here is what I did
Removed the segue which was going directly from the collection view cell button to the next story board
Created a new segue from the top of the view controller to the next view controller
Named that segue "showMapSegue"
Build and Run. I got the data successfully in the next view controller. Hope it helps.

UIViewController call function in container view viewcontroller

Using Swift 3. This question has been around and there are quite a few examples. Unfortunately none of them have worked for me. I have a uiviewcontroller that has a container view. This container view has a segue to another view controller. Everything loads up correctly and displays correctly the first time. The problem that I am facing is that I have a button on the main uiviewcontroller which by tapping it, it will refresh the container view. This is where the problem is. The outlets in the child controller are nil.
This is how I am trying to all the function in the child controller.
let wcvc = self.storyboard!.instantiateViewController(withIdentifier: "WeatherCurrentViewController") as! WeatherCurrentViewController
wcvc.callJson()
In the child controller the label is like this
#IBOutlet var currentweather: UILabel!
But in the function that is called I get an error of unexpectedly found nil while unwrapping an Optional value when I try to do
print(currentweather)
Anyone have an idea of what I can do to call a function in a view controller that is loaded by segue in a container view and still have the outlets be valid? Please let me know if more info is needed.
Found the solution. I needed to use segues identifiers and not the storyboard. Doing this
private var currentEmbeddedViewController: WeatherCurrentViewController!
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let cvc = segue.destination as? WeatherCurrentViewController, segue.identifier == "CurrentWeather" {
self.currentEmbeddedViewController = cvc
}
}
And calling the function in the controller like so
self.currentEmbeddedViewController.callJson()
Was the solution. This question offered the solution. Sorry to waste everyones time. Hopefully, this will help someone else.
The reason why you get an exception is that your currentweather label will get loaded from the storyboard only when the view of that view controller loads. This means, that when you try to call callJson() the view is not yet loaded and thus no IBOutlets are loaded yet as well, meaning they are nil.
I would suggest passing some data to theWeatherCurrentViewController and only on viewDidLoad updating the views. viewDidLoad guarantees that view and all the outlets are loaded and are not nil

navigating through desired views (while skipping some views) using UINavigationBar back button

I am using a UICollectionView to show a list of images.
1)By clicking on any cell the image and some description about it is displayed in the resulting view controller(using a push segue).
2)When i swipe from the left/right edge(using PanGesture) i need to display the details of the previous/next image in the collection view.
3)But the back button of the navigation bar has to take me back to the collection view and not to the previous displayed details (shown by the PanGesture).
I know how to get 1 and 2 done but don't have a concrete idea to get the 3rd job done.
Any help would be appreciated.
You can find your desired UIViewController in your navigation stack using for loop. Try this. This is in Swift
for (var i = 0; i < self.navigationController?.viewControllers.count; i++) {
if (self.navigationController?.viewControllers[i].isKindOfClass(YourViewController) == true) {
println("is sw \(self.navigationController!.viewControllers[i])")
(self.navigationController!.viewControllers[i] as! YourViewController)
self.navigationController?.popToViewController(self.navigationController!.viewControllers[i] as! YourViewController, animated: true)
break;
}
When you navigate from one view to another (e.g. by showing a detail view of your images one after the other, then all these views are internally piled up as a stack.
Thus, if you want to jump directly to a view somewhere in between this stack (e.g. the collection view), then you can use the Unwind Segue.
In your case it should work like this:
First, in your collection view (i.e. your back button destination) you need to implement a UIStoryboard Segue as follows
#IBAction func myGoBackPoint(segue: UIStoryboardSegue) {
println("Jump directly back here from any other view")
}
Then, in the storyboard of your detail view you ctrl-drag directly to top rightmost Exit marker and choose the previously created back button destination:
In the code of the detail view implement the "go back instruction"
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "My Unwind Segue" {
if let myUnwindSegue = segue.destinationViewController as? MyCollectionViewController {
// prepare for segue
}
}
}
Hope this helps.
Finally after researching a lot i found that using a collection view with each cell taking up the whole screen is the best way to go about solving this issue.
Thankyou

Swift - Performing Segue from UITableViewCells to different View Controller Destinations

so I am having issues figuring out how to implement code for the following purpose:
Essentially my app allows a user on the opening screen to type different categories in a UItextfield, hit enter, and then have those categories appear in a UITableView (all on the same initial view controller). For example the user could enter "Friends from home", "College Friends", "NBA Players".
From there, I want the user to be able to drill down on one of the rows, and be taken by a segue to a UITableView controller where they can add names to those lists and ultimately be able to rank them for fun...
But my question currently is once the user has added the groups that he wants on the initial view controller, how does he/she drill down and segue to a SPECIFIC view controller based on the row he/she tapped?
(I want to post a picture of my storyboard but I don't have the necessary 10 reputation points yet!)
I tried code like this but have gotten no results. I will also note that I dragged a show segue from the uitableviewcell on the opening view controller to the UITableViewController (segue is named "EachGroup").
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "EachGroup") {
// pass data to next view
let UIViewController:ViewController = segue.destinationViewController as ViewController
let indexPath = self.tableView.indexPathForSelectedRow()
}
That's not a new question, many topics about.
You should specify a tag on each cell : cell.tag = 0
In your prepareForSegue, use :
if sender!.tag.littleEndian == 0
{
let UIViewController:ViewController = segue.destinationViewController as YourSpecificViewcontroller
}
else if sender!.tag.littleEndian == 1
{
let UIViewController:ViewController = segue.destinationViewController as YourOtherSpecificViewcontroller
}
Hope it helps,
Sidd.

Swift selecting rows in TableViewController and opening up a ViewController

I started learning App development in Swift recently and I'm still fairly new. I'm trying to make an app where at the home screen, the user will click a button called profiles, and it'll show a list of names in a tableViewController. When the user clicks a name it'll open up the person's profile in a viewController.
I'm stuck so far on what method and what to code in that method to allow the user to click on the list of names in my TableViewController and then open up a viewController for that person. I've already created my other viewController (DetailsViewController) and created a segue between the TableViewController and the DetailsViewController.
I've been searching for a long time on how to do this but couldn't find any luck. If someone could just point me in the right direction that'd be great.
You have to create the segue from the cell to the detail view controller. To provide the detail controller with the data, give it a property (e.g. name) that you can set in prepareForSegue which you override in the parent controller.
override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?) {
let cell = sender as UITableViewCell // or your cell subclass
let indexPath = self.tableView.indexPathForCell(cell)
let name = dataArray[indexPath.row]
// or something else depending on the structure of your data model
let controller = segue!.destinationViewController() as DetailViewController
controller.name = name
}
The detail view controller can now populate its view with the passed information.

Resources