Perform segue with Identifier UIWebView blog, how to link? - swift

Big problem here guys, I'm on the verge of finishing my blog project until I came across a segue issue, I'm trying to display the contents of a blogs web view on a uiwebview in another controller. This is my first time working with MediaRSS and AFNetworking. I have been able to parse the contents before, but am having no luck this time around. All help is greatly appreciated. I keep getting NSIndexPath is not a member of PostLink
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "viewpost" {
let indexPath = tableView.indexPathForSelectedRow()
let item = items[indexPath!.row]
let detailViewController = segue.destinationViewController as PostViewController
detailViewController.postLink = indexPath.postLink
}
}
}
UPDATE 9:17PM
Looks like im performing a segue properly, because now my page will go to a blank. Can someone please confirm the parameters I am passing in, I just need someone to look and tell me please

NSIndexPath does not have any member named postLink. I think you mean to write item.postLink.
detailViewController.postLink = item.postLink
I hope item is of some type that has 'postLink' member and it is populated with appropriate value.

Related

Navigation controller between two UITableViewControllers in Swift

I am looking for a way to insert a navigation controller between two UITableViewControllers. I have a UITabBarController which then connects to a UITable which then connects to another UITable. I have attached a picture below to give a better reference at what I am talking about.
So from UITabBar, you go to the first table, from that, you make a selection and go to another table. The second table is where I want my back button. I know in the screenshot you can see the back button present but for some reason it does not show it at run time.
Moreover, when I try to embed Navigation Controller in the first table, I get an error in the code below.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let barViewControllers = segue.destination as! UITabBarController
let destinationViewController = barViewControllers.viewControllers?[0] as! MainVC
destinationViewController.TPNumber = txtTpNumber.text!
destinationViewController.intake = intake
let DVC = barViewControllers.viewControllers?[1] as! AttendanceTVC //This is where I get an error
DVC.TPNumber = txtTpNumber.text!
}
If anyone could guide me on how I can solve this? Thanks!
Go to your storyboard and click your Attendance (ViewController), than click your menu "Editor" -> "Embed in" -> UINavigationController.
You can firstly cast this destination as UINavigationController and then you can access the controller embed in UINavigationController by it's topViewController property

Cannot pull data between view controllers: Swift 3

I have a view controller with a name input text box in it. I want to be able to pull the name.text value from my main view controller, but I'm struggling with the code.
Sorry, I know there are similar questions, I am struggling to understand where I am going wrong though, here is my code:
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "statsegue" {
let statVC = segue.destination as! SecondViewController
NameLabelMain.text = statVC.NameDisplayStat.text
//or try that if above doesn't work: detailsVC.passedString = recipes[indexPath?row]
}
}
This is on my main page, trying to pull the data from my second view.
Any thoughts or suggestions would be awesome.
Thanks
This is a common timing problem.
In prepareForSegue the view of the destination view controller is not loaded yet so all IBOutlets are not connected yet (aka not available).
You can only access data which is available right after initialization.
However generally you are passing data from the source controller to the destination controller rather than the other way round.

what replaces indexPathForSelectedRow in Swift 3?

In projects prior to Swift 3 - If I was looking to segue an indexPath from a tableController, I'd have a line of code like
let indexPath = self.tableView.indexPathForSelectedRow!
However, in Swift 3, I am unable to write this. I can't seem to find any documentation on Apple's site or other SO questions to find a workable piece of code.
In response to Frankie's comment, I went back to my code (here is all of the current segue Swift 3 code) and have tried to copy and paste above code snippet... but to no avail. Xcode does not auto complete if I enter "self.tableView". It does not recognize indexPathForSelectedRow! either. I'm missing something.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "dineSegue"
{
if let destinationViewController = segue.destination as? RestaurantController
{
let indexPath = DineController.indexPathForSelectedRow!
destinationViewController.restaurantIndex = index
}
}
}
I googled 'indexPathForSelectedRow Apple documentation` and was able to find the property reference for you as it was conveniently the first result.
https://developer.apple.com/reference/uikit/uitableview/1615000-indexpathforselectedrow
That being said, it's exactly what you posted in Swift 3.
self.tableView.indexPathForSelectedRow
Are you sure the "DineController" reference is to the actual UITableView
When I had this issue, my initial reference was to the generic class, not the actual table I had coded.

Passing Core Data objects from UITableViewCell to another View Controller

Currently I have the application loading up the data in a tableview presented to the user that I want them to select the object they wish to view further details about. From their I want to be able to read out the data in the object to the user in various forms either labels or a tableview dependent on the data.
I have been reading around and trying to understand this stuff but it is all still new to me! I had another guy on here help me out with saving the data and passing the data around to store but reading it out is a seemingly different thing. Any examples in swift language would lead me down the right path I know that I need to use the didselectrow method and call a segue and also that I need a prepare for segue but I am not quite sure how it should look.
I have read multiple posts and some do actually attempt to pass objects but not in the manner in which I am trying to..Are you able to pass whole objects after they have been have been selected in a tableview to another view controller to present all data related to that object or are you only able to pass information from that object to the next viewcontroller? I have examples of prepareforsegue and perform segue before not sure what else I am missing but I am currently not able to pass any information between the tableview and the viewcontroller.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "decktoRecord") {
var detailVC: StatWidgetViewController = segue.destinationViewController as! StatWidgetViewController
detailVC.deckRecord = decktoPass
}
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let indexPath = tableView.indexPathForSelectedRow()
let selectedCell = tableView.cellForRowAtIndexPath(indexPath!) as UITableViewCell?
self.performSegueWithIdentifier("decktoRecord", sender: indexPath);
}
decktoPass is just a variable that contains the type of data entity that is the primary entity of the object.
A little confused by what you're asking but from what I understand, once the user clicks on a cell you want to segue to another view controller where they can edit the the details?
To add an exception breakpoint, open up your left panel with all your files/viewcontrollers, at the very top of it should be a small panel with a few icons, the first one is a folder, click on the second last one (the one that looks like a tag)
click on the plus in the bottom right and click add exception breakpoint, this should let you know where the problem in your code is occurring
Okay to edit the details in another View controller start by preparing the segue from the original view controller
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showTaskDetail" {
let detailVC: TaskDetailViewController = segue.destinationViewController as! TaskDetailViewController
let indexPath = self.tableView.indexPathForSelectedRow()
let thisTask = fetchedResultsController.objectAtIndexPath(indexPath!) as! TaskModel
detailVC.detailTaskModel = thisTask
detailVC.delegate = self
}
else if segue.identifier == "showTaskAdd" {
let addTaskVC:AddTaskViewController = segue.destinationViewController as! AddTaskViewController
addTaskVC.delegate = self
}
}
okay so like the code is showing above, I have a segue called showTaskDetail which shows the details of whatever it is, in my case its a simple task. You said you want to edit this information in another view controller when a user clicks on the row, well you need to be able to get this information in the other view controller.
So create a variable in the other viewcontroller that will hold these values, for me i called it detailTaskModel
var detailTaskModel: TaskModel!
Incase you're confused about what TaskModel is, I'm using CoreData to store my data, and TaskModel is a NSMangedObject class.
let detailVC: TaskDetailViewController = segue.destinationViewController as! TaskDetailViewController
this line you're just specifying what your other view controller is, replace TaskDetailViewController with your swift class.
let detailVC: TaskDetailViewController = segue.destinationViewController as! TaskDetailViewController
this line fetches the data from the row selected
Now you should be able to pass your information into the other view controller and edit it using the detailTaskModel, do you need help with that as well?

performSegue after completion handler

ANSWER BELOW
Im facing a little issue that you may help me with.
the app Im working on allows you to request for content based on your location.
the first ViewController is somewhat a form that grab your location / a specified location + some other information to target specific answers.
I need to perform a segue to pass the "question" variables to my second ViewController where I load "answers" with a query based on the question details.
What is causing me trouble is that, whenever the question is geolocalized, I can't retrieve the information using prepareForSegue because it doesn't wait for the geoPoint to be made (completed).The second controller display my latitude and longitude as nil.
I see that I can call the "prepareForSegue" method using "perfomSegueWithIdentifier", and retrieve the information in my second view controller but it perform the segue twice... How can I trigger the segue only when Im ready but using the prepareForSegue data parameter I need to preserve?
Is there a way to pass variable from one controller to another using performSegue?
Any help would be awesome.
Also, while I don't think the code is relevant for my question, here is the code I use.
geoPointing method
#IBAction func doPostQuestion(sender: UIButton) {
var thereQ:PFObject = PFObject(className: "tquestion")
if(somewhereLabel.text == "my location"){
println("Location is geolocalized")
PFGeoPoint.geoPointForCurrentLocationInBackground {
(geoPoint: PFGeoPoint!, error: NSError!) -> Void in
if error == nil {
self.geoLati = geoPoint.latitude as Double
self.geoLong = geoPoint.longitude as Double
self.performSegueWithIdentifier("goto_results", sender:self) // call prepareForSegue when ready but implies to have a segue done on click... (performed twiced)
}
}
}
self.navigationController?.popToRootViewControllerAnimated(true)
}
prepareForSegue
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "goto_results"){
// Get Label
let theDestination = (segue.destinationViewController as displayAnswersViewController)
theDestination.lat = self.geoLati
theDestination.lng = self.geoLong
}
}
ANSWER SOLUTION:
As suggested, to solve this problem you just need to create your segue from your viewController1 to your viewController2 and not from a button. This way you can trigger prepareForSegue programatically using the "performSegue" method that will call prepareForSegue anyway.
To solve this problem you just need to create your segue from your viewController1 to your viewController2 and not from a button. This way you can trigger prepareForSegue programatically using the "performSegue" method that will call prepareForSegue anyway.