I have problems sending saved Data from a first VC to a second VC so it shows it on viewDidLoad().
It does everything but it doesn't show me the date if I restart the app.
For example first VC:
#IBAction func btnEg(sender: AnyObject) {
string = label.text;
date = NSDateFormatter.localizedStringFromDate(NSDate(),
dateStyle: NSDateFormatterStyle.MediumStyle,
timeStyle: NSDateFormatterStyle.ShortStyle);
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let statistikvc: StatistikViewController = segue.destinationViewController as! StatistikViewController
if (self.Count > 0) {
statistikvc.date = date
statistikvc.save(self.date)
}
}
Second ViewController:
var date = String();
func save(a: AnyObject?){
defaults.setObject(a,forKey:"date")
}
override func viewDidLoad() {
super.viewDidLoad()
if let _ = defaults.objectForKey("date") as? String {
self.txtDate.text = defaults.objectForKey("date") as? String
}
txtDate.text = date;
}
You are missing the call to defaults.synchronize() in your save function. Without it the data is not actually stored.
It should be:
func save(a: AnyObject?){
defaults.setObject(a,forKey:"date")
defaults.synchronize()
}
And you probably have to switch the two statements in your viewDidLoad:
txtDate.text = date;
if let _ = defaults.objectForKey("date") as? String {
self.txtDate.text = defaults.objectForKey("date") as? String
}
Note that this will get messy. You are mixing two different mechanics here. You should either always use the handed over date or always use the default stored.
Related
My idea is that when Button "A" is tapped every time, it will set the NSDate value automatically. When the current time is larger than the existing NSDate value, it print "yes". Here is my code but I dont know what's wrong.
import UIKit
class ViewController: UIViewController {
var currentDateTime = NSDate()
override func viewDidLoad() {
super.viewDidLoad()
observeTime()
}
#IBAction func show(_ sender: Any) {
print(UserDefaults.standard.dictionaryRepresentation())
}
let userDefaults = UserDefaults.standard
func observeTime() {
let posttime = userDefaults.object(forKey: "LastPostingTime") as? NSDate
if ((posttime?.isGreaterThanDate(dateToCompare: currentDateTime))!) {
print("yes")
}
}
#IBAction func hihi(_ sender: Any) {
observeTime()
userDefaults.set(NSDate(), forKey: "LastPostingTime")
}
}
extension NSDate {
func isGreaterThanDate(dateToCompare: NSDate) -> Bool {
//Declare Variables
var isGreater = false
//Compare Values
if self.compare(dateToCompare as Date) == ComparisonResult.orderedDescending {
isGreater = true
}
//Return Result
return isGreater
}
}
In function hihi you first store the current date into NSUserDefaults before reading it. So you will get back what you just stored: the current time.
You may want to read it first, compare it to currentDateTime and then store it into user defaults.
How can I send multiple variables through a segue in Swift? The QBBust gets sent over fine and prints on the view controller, but the QBName doesn't get sent over for some reason. Can anyone spot why?
if let send = sender as? Double{
destination.QBBust = send
}
if let sent = sender as? String{
destination.QBName = sent
}
}
}
private var _QBName:String!
var QBName: String{
get{
return _QBName
} set {
_QBName = newValue
}
}
private var _QBBust:Double!
var QBBust: Double {
get {
return _QBBust
} set{
_QBBust = newValue
}
}
override func viewDidLoad() {
super.viewDidLoad()
let bust = String(Int(_QBBust))
QBBustLabel.text = "\(bust)%"
QBNameLabel.text = _QBName
}
This next part is in the button function that triggers the segue
performSegue(withIdentifier: "QBResultVC", sender: QBBust)
performSegue(withIdentifier: "QBResultVC", sender: QBName)
As in Tiago's answer, you can create a new struct or class which has QBName and QBBust properties. In addition, you can also use tuple in Swift.
This is an example:
in Destination ViewController
declare var QBInfo:(name: String, bust: Double)?
and in the button function that triggers the segue
let QBInfo = (name: QBName, bust: QBBust)
performSegue(withIdentifier: "QBResultVC", sender: QBBust)
then in prepareForSegue:sender:method
destination.QBInfo = QBInfo
This question is duplicate, but you can create a Struct or new Class, and storage your data how properties and send the 'transport' object in segue.
For detail, look this answser:
Swift sending Multiple Objects to View Controller
Okay so I have two view controllers. One view controller loads up all the cells that is on my Plist and the second view controller opens up the cell and shows you the description. For example:
View Controller 1:
Dog
Cat
Mouse
Click on Dog cell it will take you to View Controller 2:
Dog goes Woof.
view controller 1 is written:
ovverride func prepare(for segue: UIStoryBoardSegue, sender: Any?) {
if segue.identifier == "showDetail" {
let animals: Animals
if isFiltering() {
animals = filteredData[indexPath.row]
}
else {
animals = originalData[indexPath.row]
}
let controller = (segue.destination as! UINavigationController).topViewController as! SecondViewController
controller.detailedAnimals = animals
controller.navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem
contrller.navigationItem.leftItemsSupplementBackButton = true
}
}
}
this is what i wrote in viewcontroller 2 updated
var isFavorite : Bool = false
#IBAction func addToFav(_ sender:UIButton) {
isFavorite = !isFavorite
UpdateButtonAppearance()
saveData()
}
private func UpdateButtonAppearance(){
if isFavorite{
let image = UIImage(named: "addFav")
favButton.setImage(image, for: . normal)
savedData()
}
else {
let image = UIImage(named: "addFavFilled")
favButton.setImage(image, for: . normal)
savedData()
}
}
ovveride func viewDidLoad(){
UpdateButtonAppearance()
saveData()
}
//updated code
func getFilePath () -> String {
var path: [AnyObject] = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true) as [AnyObject]
let documentsDirectory: String = path[0] as! String
let filepath = documentsDirectory.appending("Animals.plist")
return filepath
}
func saveData(){
let myDict : NSMutableDictionary = NSMutableDictionary()
myDict["fav"] = NSNumber(booleanLiteral: isFavorite)
myDict.write(toFile: self.getFilePath(), atomically: true)
}
func getBool(){
if FileManager.default.fileExists(atPath: self.getFilePath()) {
var myDict = NSDictionary(contentsOfFile: self.getFilePath()) as!
[String:AnyObject]
let myBool: Bool = myDict["fav"]!.boolValue
isFavorite = myBool
}
I saw a tutorial on how to change the bool in the Plist and wrote it this way. The code compiles but I don't think it is changing the bool value. So on my Animals Plist i have a Item 0 type dictionary, first key is called Animal, type is string, value is "dog" and second key is Description, type is string, value is "dog goes woof" and third key is called fav, type is Bool and value is No for now but I am trying to change this value to Yes but it is not working. Also thank you so much for your comment it was easy to follow and easy to understand.
The star is not filled when you go back to the 2nd view controller because you set the images in the #IBAction func addToFav(_ sender:UIButton) method. That is when you tap the button.
You should have that part of the code in another method that you also call in didLoad().
2nd View Controller should be something like this:
var isFavorite = UserDefaults.standard.bool(forKey: "isFavorite")
func didLoad() {
super.didLoad()
updateButtonAppearance()
}
private updateButtonAppearance() {
if isFavorite {
let image = UIImage(named: "addFavFilled")
button.setImage(image, for: . normal)
}
else {
let image = UIImage(named: "addFav")
button.setImage(image, for: . normal)
}
}
#IBAction func addToFav(_ sender:UIButton) {
isFavorite = !isFavorite
UserDefaults.standard.set(isFavorite, forKey: "isFavorite")
updateButtonAppearance()
}
Also the code could be improved to not save the variable in UserDefaults in addToFav method, but whenever isFavourite is changed. Maybe you will later want to change the state of isFavourite in another method which will require code duplication.
Also note that you save a value for all pets in UserDefaults under isFavourite. That means if you favor one pet, all other pets will be favored and vice versa. Consider replacing the bool value in user defaults with a dictionary that has keys for each pet and booleans as values.
I have been having some issues with my prepareForSegue call. Whenever I try to segue to the next view controller. It gets an error and crashes at the point where I am passing the tripLikes to the next view controller.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "toDetailScene") {
//Hooking up places-holder values
let viewController = segue.destinationViewController as! DetailViewController
let cell = sender as! CustomPFTableViewCell
viewController.tripName = cell.nameTextLabel.text!
viewController.tripAuthor = cell.authorTextLabel.text!
viewController.tripLikes = Int(cell.numLikes.text!)!
viewController.tripDescrip = cell.descriptionHolder
}
}
Each of the values that we are passing to are values in the destination view controller.
You're doing a lot of force-unwrapping with Int(cell.numLikes.text!)!. If any of those values are nil, your program will crash.
Why not try something safer, such as an if-let flow:
if let text = cell.numLikes.text {
if let textInt = Int(text) {
viewController.tripLikes = textInt
} else { // Int cannot be constructed from input
viewController.tripLikes = 0
}
} else { // cell.numLikes.text was nil
viewController.tripLikes = 0
}
This code is from the locations tableView view controller, where the locations are serialized from a Four Square API and I grab the data, passing it back the view controller where I create an event. The data is serialized correctly, populating the tableView and all, so I won't bother posting that for now, just the segue method to pass the data.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let indexPath = self.tableView.indexPathForSelectedRow
let item = self.responseItems![indexPath!.row]
var eventVC = CreateEventViewController()
if segue.identifier == "pushBackToEventVC" {
if let venue = item["venue"] as? NSDictionary {
let locationString = venue["name"] as! String
let location = venue["location"]
//get lat/long strings
print(location)
eventVC.updateWithLocation(locationString)
eventVC.updateWithGeoPoint(PFGeoPoint(latitude: locationLatitude, longitude: locationLongitude))
}
eventVC = segue.destinationViewController as! CreateEventViewController
//pass location coordinates
}
//the rest of the segue method
}
The update methods in the crete event view controller, when I put a breakpoint on these methods I can see the data has been passed correctly, and the location (a PFGeoPoint) works okay but the locationString (a string) throws a "Bad instruction error" although it does print it correctly to the console
func updateWithLocation(locationString: String) {
print(locationString)
self.locationLabel.text = locationString
}
func updateWithGeoPoint(venueLocation: PFGeoPoint) {
// print(venueLocation)
self.event?.location = venueLocation
self.eventLocation = venueLocation
print(self.eventLocation)
}
Any ideas? It's basically working but won't update the label with the string although I can see it's been passed correctly. Thanks for the help as always.
Try using this code block instead,
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "pushBackToEventVC") {
let detailViewController = ((segue.destinationViewController) as! CreateEventViewController)
let indexPath = self.tableView!.indexPathForSelectedRow!
detailViewController.locationString = venue["location"]
}
}