Edit 1: I've restructured my ViewControllers to make it easier to get what I want done.
Edit 2: I realized something major was missing while adding notes to my code, another function overrides the first segue.
This ViewController is where the annotation is created; all I need from this view is for the touchMapCoordinates to be transferred to the other ViewController so I can save the PFGeoPoint in an array.
Edit 3
After long work on understanding what is going on and simplifying the code, i've came down to the final conclusion based off of Swift- variable not initialized before use (but it's not used) , that the current method that I'm trying to use will not work in any case or scenario due to it saving Asynchronously. If anyone knows a work around, then you have officially done something that hasn't been done before :).
Error that is showing up is
Constant 'boi' used before being initialized
Subclass that is declared in Appdata to be used anywhere within the project
import Foundation
import Parse
import MapKit
class MyAnnotation: PFObject, PFSubclassing, MKAnnotation {
// MARK: - Properties
#NSManaged var location: PFGeoPoint
// MARK: - Initializers
init(coordinate: CLLocationCoordinate2D) {
super.init()
self.location = PFGeoPoint(latitude: coordinate.latitude, longitude: coordinate.longitude)
print(location)
}
override class func initialize() {
struct Static {
static var onceToken : dispatch_once_t = 0;
}
dispatch_once(&Static.onceToken) {
self.registerSubclass()
}
}
// MARK: - PFSubclassing protocol
static func parseClassName() -> String {
return "AnnotationPins"
}
// MARK: - MKAnnotation protocol
var coordinate: CLLocationCoordinate2D {
return CLLocationCoordinate2DMake(location.latitude, location.longitude)
}
var title: String? = "Start Topic"
}
Where the code will all be saved asynchronously together
} else {
let imageData = UIImagePNGRepresentation(self.galleryCameraImage.image!)
let parseImageFile = PFFile(name: "upload_image.png", data: imageData!)
let boi : MyAnnotation
let textTitleandText = PFObject(className: "AnnotationPins")
textTitleandText["textTopic"] = userTopic.text
textTitleandText["textInformation"] = userText.text
textTitleandText["userUploader"] = PFUser.currentUser()
textTitleandText["imageFile"] = parseImageFile!
textTitleandText["location"] = boi.location
textTitleandText.saveInBackgroundWithBlock { (success: Bool, error: NSError?) -> Void in
if error == nil {
If anyone could help it would be really appreciated!
Over ride prepareForSegue method like below.
override func prepareForSegue(segue: UIStoryboardSegue, sender:
AnyObject?) {
if segue.identifier == "SegueID" {
let destinationVC = segue.destinationViewController as! DestinationViewController
// Create property in destinationView controller & assign required data from here.
}
}
Hope it helps.
Lets treat your Location data as a normal data to be transferred through segues.
You can use this method to configure your destination View controller variable(same type) that will hold your location data.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
//check your segue name
if segue.identifier == "YourSegueIdentifier" {
let destinationVC = segue.destinationViewController as! YourDestinationViewController
destinationVC.locationVariableInDestinationVC = locationVariableInCurrentVC
}
}
Above is the simplest way to pass data via segue, you can use the same approach for your location data too.
Hope that helps!!
Update: Based on your updated code
Move func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {} out of your handleLongPress function. PrepareForSegue function gets called automatically when there is any navigation happening through segues..
If you want to initiate a segue navigation programatically then assign a identifier to the segue and just call self.performSegueWithIdentifier("YourSegueIdentifier", sender: nil)
Related
I want to take user settings details from this view controller and read these details to the previous view controller. I have tried many different ways, but I cannot take values until I visit this view controller
I have tried first method from this page Pass Data Tutorial
This method is also not working. I think it is very simple, but I cannot figure out the right way to do it.
class SetConvViewController: UIViewController {
var engS = "engS"
#IBOutlet weak var swithEnglish: UISwitch!
override func viewDidLoad() {
super.viewDidLoad()
if let eng2 = defaults.value(forKey: engS)
{
swithEnglish.isOn = eng2 as! Bool
}
}
let defaults = UserDefaults.standard
#IBAction func switchEng(_ sender: UISwitch) {
defaults.set(sender.isOn, forKey: engS)
}
}
If I understand you correctly from this part - „but I cannot take values until I visit this view controller” - your problem lies with the fact, that until you visit your settings, there is no value for them in UserDefaults.
If you are reading them using getObject(forKey:) method, I’d recommend you to switch to using getBool(forKey:), since it will return false even if the value has not been set yet for that key ( docs )
Anyhow, if you want to set some default/initial values you can do so in your didFinishLaunching method in AppDelegate :
if UserDefaults.standard.object(forKey: „engS”) == nil {
// the value has not been set yet, assign a default value
}
I’ve also noticed in your code that you used value(forKey:) - you should not do that on UserDefaults - this is an excellent answer as to why - What is the difference between object(forKey:) and value(forKey:) in UserDefaults?.
On a side note, if you are using a class from iOS SDK for the first time, I highly recommend looking through its docs - they are well written and will provide you with general understanding as to what is possible.
I would recommend you to store this kind of data as a static field in some object to be able to read it from any place. e.g.
class AppController{
static var userDefaults = UserDefaults.standard
}
and then you can save it in your SetConvViewController like
#IBAction func switchEng(_ sender: UISwitch) {
AppController.userDefaults.set(sender.isOn, forKey: engS)
}
and after that you can just read it from any other view controller just by calling
AppController.userDefaults
Using segues you can set to any destination whether it be next vc or previous:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "PreviousVC" {
if let prevVC = segue.destination as? PreviousViewController {
//Your previous vc should have your storage variable.
prevVC.value = self.value
}
}
If you're presenting the view controller:
Destination vc:
//If using storyboard...
let destVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "DestinationViewController") as! DestinationViewController
destVC.value = self.value
self.present(destVC, animated: true, completion: nil)
Previous vc:
weak var prevVC = self.presentingViewController as? PreviousViewController
if let prevVC = prevVC {
prevVC.value = self.value
}
I have an async function (geocodeAddressString) that gets called in the prepare for segue function. The problem is that this async function returns after the segue is executed. The async function below converts an address string into coordinates. I can't call this async function before this prepare function as the user address field can be edited by the user at any time. I've tried calling the async function when the save button is pressed and then coding the segue when the async function returns, but then the loading time between the button being pressed and the segue occurring is too long.
So my question is if there is any way that I can catch this async method in the new view? Then I could just replace some temporary coordinates with the actual returned values. Any help is appreciated, Thanks!
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
//convert address to coordinates
let geoCoder = CLGeocoder()
geoCoder.geocodeAddressString(addressTxt.text!) { (placemarks, error) in
guard
let placemarks = placemarks,
let location = placemarks.first?.location
else {
// handle no location found
return
}
let tempCoord = CLLocationCoordinate2D(latitude: (location.coordinate.latitude), longitude: (location.coordinate.longitude))
self.donatedItem = DonatedItem(self.titleTxt.text!, self.itemImg.image!, self.donated, self.descTxt.text!, expirationDate, tempCoord, (user?.uid)!, (self.donatedItem?.itemID)!, self.addressTxt.text!, reserved: false, reservedBy: "NA")
self.donatedItem?.updateItem()
}
switch(segue.identifier ?? "") {
case "saveItem":
homeViewController.isReturningSegue = true
homeViewController.tempItem = self.donatedItem
default:
print("Undefined segue")
}
}
Of course you can. Post a notification to notify the new view. Or use closure. For example, this is notification.
NotificationCenter.default.post(name: Notification.Name(rawValue: "locationUpdated"), object: coordinate)
This is how to do this with closure.
typealias closure = (String) -> Void
class A: UIViewController {
var coordinateUpdated: closure? // call this after coordinate updated
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let b = segue.destination as? B {
coordinateUpdated = b.coordinateUpdated
}
}
}
class B: UIViewController {
let coordinateUpdated: closure = { (coordinate: String) in
// update here
}
}
You could instead pass on addressTxt.text! and then call the async method in the next view controller .
In the current view controller:
homeViewController.isReturningSegue = true
homeViewController.tempItem = self.donatedItem
homeViewController.address = self.addressTxt.text!
In the next view controller:
func viewWillAppear() {
supper.viewWillAppear()
let geoCoder = CLGeocoder()
geoCoder.geocodeAddressString(self.address) { ... }
}
If geocodeAddressString is taking too long (and affecting the UX) you could also put up a progress indicator. For instance, MBProgressHUD is a pretty common framework for such tasks.
I have a segue which passes location data from a table view. I need to add a map annotation after this segue has completed, but the map is my initial VC, so it crashes on load due to the lack of data as the segue has not occurred.
I think I need to wrap the following code (in the viewDidLoad) in an if statement that checks if the segue has occurred, or check if there is any data available to use.
let roomPin = MGLPointAnnotation()
roomPin.coordinate = CLLocationCoordinate2D(latitude: room.latitude, longitude: room.longitude)
mapView.addAnnotation(roomPin)
Not entirely sure how I should be checking if there is any data to use.
edit:
room is a custom class loaded with alamofire
class Room {
var roomNumber : String!
var roomName : String!
var latitude : Double!
var longitude : Double!
init(json : JSON) {
roomNumber = json["room_number"].stringValue
roomName = json["room_name"].stringValue
latitude = json["latitude"].double
longitude = json ["longitude"].double
}
}
then is called in a tableview to display the name and number. When pressed it performs a segue passing the Room object to my mapVC
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "mapSegue" {
if let destination = segue.destination as? ViewController {
destination.room = sender as! Room
}
}
}
and in my map view controller I have that above annotation code in the viewDidLoad, with room being declared as:
var room : Room!
Any help is appreciated!
I guess the problem is in the line destination.room = sender as! Room because sender will be of type UITableViewCell class and when you try to typecast sender to Room object type it crashes.
I have a MapViewController with a prepareForSegue(_:sender:)method, which I intend to use to send data to LandmarkTableViewController, and is called when a button is pressed.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let destinationvc = segue.destinationViewController
if let landmarkvc = destinationvc as? LandmarkTableViewController {
if let identifier = segue.identifier {
let library = Landmark(name: "Run Run Shaw Library", properties: ["Chinese Kanji", "Gray", "Green Plants"])
let bank = Landmark(name: "Hang Seng Bank", properties: ["Chinese Kanji", "Green"])
switch identifier {
case "showLibrary" : landmarkvc.passedLandmark = library // pass data to LandmarkTableViewController
case "showBank" : landmarkvc.passedLandmark = bank // pass data to LandmarkTableViewController
default : break
}
}
}
}
The LandmarkTableViewController is properly set up to display the String array properties, with one String on each row. So what I intend to do is pass the appropriate data for the table to properties according to which button was pressed, and let LandmarkTableViewController display the corresponding properties.
class LandmarkTableViewController: UITableViewController {
var properties = [String]()
var passedLandmark = Landmark(name: "temp", properties: ["temp"]) // initially set to default value
override func viewDidLoad() {
super.viewDidLoad()
loadSampleProperties()
}
func loadSampleProperties() {
self.properties = passedLandmark!.properties
}
// other methods....
}
class Landmark {
var name: String
var properties: [String]
init?(name: String, properties: [String]) {
self.name = name
self.properties = properties
// Initialization should fail if there is no name or if there is no property.
if name.isEmpty || properties.isEmpty {
return nil
}
}
However, when I run the code, only temp is displayed in the table view. I've been stuck on this for a long time now, so any help is much appreciated!
Edit: loadData() inside of viewDidLoad() is changed to the correct loadSampleProperties(). I made an error while posting the code to the question.
I think this should solve your problem if not double check your identifiers
and you can make sure to data passing with adding print(passedLandmark) to viewDidLoad() or breakpoint to make sure you getting the data
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let destinationvc = segue.destinationViewController
if let landmarkvc = destinationvc as? LandmarkTableViewController {
if segue.identifier == "showLibrary" {
let library = Landmark(name: "Run Run Shaw Library", properties: ["Chinese Kanji", "Gray", "Green Plants"])
landmarkvc.passedLandmark = library
}
if segue.identifier == "showBank" {
let bank = Landmark(name: "Hang Seng Bank", properties: ["Chinese Kanji", "Green"])
landmarkvc.passedLandmark = bank
}
}
}
Hope this will helps
Code is missing from your quote, so I can't be sure, but I assume your loadData() method is the one that reloads the table view data with Landmark you've passed in prepareForSegue. If that is the case:
viewDidLoad() is called before prepareForSegue, so that all the views and elements of the destinationViewController are loaded and ready to use. Thus, in your case, the table view is loaded with your "temp" data and nothing makes it reload when you set the proper one.
You have two options:
You could call loadData()/reloadData() in viewWillAppear for example, which is called after prepareForSegue(). Bare in mind that viewWillAppear will possibly be called again in some other navigation.
Otherwise, you could instantiate and present/push the new controller in your parent view controller, instead of using the segue.
I want to make such thing:
On one ViewControleer I'm making a query to Parse.com, where I'm sending objects fields to Label.Text. By clicking one button objects randomly changes, by clicking another one- next ViewController is opening. Just imagine Tinder - on the first VC I swiping girls, on the new one chat is opening, with the girl's name in the head of the NavigatorItem
So I want to send Object Field "Name" that I'm using in that view to another without other query.
I don't know, whether I can do it via segue, or protocol. Can U somehow help me with implementation?
here is the code of my random function
func retriveJobData() {
var query: PFQuery = PFQuery(className: "Jobs")
query.getObjectInBackgroundWithId("AUeuvj0zk2") {
(newJobObject: PFObject?, error: NSError?) -> Void in
if error == nil && newJobObject != nil {
println(newJobObject)
if let newJobObject = newJobObject {
self.PrcieTextField.text = newJobObject["jobPrice"] as? String
self.DateTextField.text = newJobObject["jobDate"] as? String
self.DescriptionTextField.text = newJobObject["jobDescription"] as? String
}
} else {
println(error)
}
}
}
I want to send newJobObject["jobName"] to NavigatorItemName of another ViewController
you can override prepareForSegue for this purpose:
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if (segue.identifier == "yourSegueIdentifier") {
// pass data to next view
}
}
Assuming you have some method that triggers a push to the new viewController and that you're using the storyboard, call performSegue using the identifier you set up in the storyboard
#IBAction func buttonPressed(sender: UIButton!) {
performSegueWithIdentifier("identifier", sender: nil)
}
Then override prepareForSegue and pass in the string
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "identifier" {
let controller = segue.destinationViewController as! ViewController
controller.jobName = someWayThatYouRetrieveNewJobObjectName
}
Then in ViewController ensure you have a property for jobName
var jobName:String! //declare this as an optional instead if needed
And set the navigation title
navigationItem.title = jobName