Core Data detail view Error - swift

I have been working on an app that has a tableview where users can add information on a seperate ViewController via UITextField. I am now trying to set it up so the user can tap on any given Cell to be able to view the data back on that ViewController or edit it again. Everything is being saved using CoreData.

Best way is you can put the check like this. So that it will never crash or throw the error:-
if let sender = sender as? String {
guest.player = sender
}

Related

Mac OS - Change Status Item Button Image

I want to update the image when the user clicks something in the application but I can't get it to work.
The status item with the menu is defined in the AppDelegate. I am trying to update the image in the ViewController with this piece of code which I think should work:
AppDelegate().statusItem.button?.image = NSImage(named:NSImage.Name("icon-orange"))
No errors are showing up, but turns out it still doesn't work, so is it possible to change the image or am I doing something wrong?
AppDelegate() creates a brand new instance which is not the delegate class in Interface Builder.
You need the real reference:
(NSApp.delegate as! AppDelegate).statusItem.button?.image = NSImage(named:NSImage.Name("icon-orange"))

firebase dismiss reload table in Swift?

I have 3 viewControllers:
VcA: I read data with observers from database and pass with segue to VcB.
VcB: I read data and display it, with a button I call VcC to update data passing datas with segue.
VcC: a STATIC tableview in wich I update data and save them.
The problem is that when I dismiss after saved VcC and go back in VcB all data is old, even if in firebase all datas are perfectly updated.
In VcB for example I read the Title like this:
var groupName: String?
than in didAppear:
Name.title = groupName
Anyway if I also go back from VcB to VcC all data of the table are the old data.
To be more clear:
I call the database in VcA
click the name in the table and pass the data to Vcb, all is fine for now, title and other vars are ok
now I click EDIT and call to VcC, the table where I update the data, make change and save,
after save VcC dismiss and I go back to VcB -> title is still p2 but in firebase not!
I click again on EDIT to return to VcC (the edit table), title is the old one
I think that I have to recall datas from db... anyway to do it without refresh the data from firebase?
I know that if I go back to VcA after save all is fine but I really need to go to VcB, how can I do?
I found many threads, this is the most similar but not exact situation...
Reload tableView after dismiss a viewController
If I can follow this correctly.
VcC:
protocol %NAME%Delegate
{
func reloadData()
}
class VcC: UIViewController /*(or table)*/{
var mDelegate : %NAME%Delegate?
//in the func you use to dismiss self (VcC)
//mDelegate?. reloadData()
}
VcB:
class VcB: UIViewController /* (or table)*/,%NAME%Delegate {
func reloadData(){
tableView.reloadData()
//OR
//Observer for Firebase here to gather the new information. Make sure to reset all datasources if you do this.
}
}

swift update textview after typing new text

I'm making an app in swift 3
I have a TableViewController, when I click on a row it opens a ViewController in which there is a TextView. When I click on it, I am able to edit it, but the changes are not "saved". when I go back to my list and re-click on the same row, the text is back to "Default", I've been on this problem all day, and I don't know how to solve it, I've tried solution from stackoverflow but only give solution to change the text colours.
So how can I do that ?
Based on what you've said so far, I can only assume that you are not storing the new text that's been entered into your UITextView.
The reason that the changes are not being saved is that every time you "open" a new ViewController by tapping on a TableViewCell, it is a brand new instance of that ViewController. That means that it has no input as far as what should be displayed in the TextView, and therefore displays the default text.
But how do I fix it?
This is going to require that you save your TextView's text every time that you leave or dismiss the ViewController. This is because when you leave the ViewController, it is being released from memory. This means that anything that was written there no longer exists. I would suggest storing the text in an array of strings on your TableViewController, which should allow for the text to persist over the life cycle of the app.
The text disappears when I close the app!
This is something that will require data persistence, and for that I would suggest reading up on how to use Core Data to store and persist data across multiple app life cycles.
There are many ways to achieve that, the idea is to set the text of the text view in its view controller's initialization.
So, you need a place to save the data entered by the user, and then read that data again in the view controller's initialization.
One way to do that is using a singleton class to save the data, like this for example:
class SharedData {
static let instance = SharedData()
var textEnteredByUser: String?
}
Now, when the user enters text, save that text in the string we have in 'SharedData' like this:
SharedData.instance.textEnteredByUser = textView.text!
Now, we have the data entered by the user saved, we need to set the data in the text view before it appears in the screen, and this can be achieved inside viewDidLoad method of your view controller like this:
override func viewDidLoad() {
super.viewDidLoad()
textView.text = SharedData.instance.textEnteredByUser
}
You can also save your data in user defaults instead of the singleton class like this:
UserDefaults.standard.set(textView.text!, forKey: "textEnteredByUser")
And then retrieve the saved text this way:
UserDefaults.standard.string(forKey: "textEnteredByUser")
Note: The singleton solution keeps the data shared between different view controllers, but does not persist data after closing the application, and this is what user default does.

Need to use a NSTextView (or NSTextField) for a clickable URL

I'm given an arbitrary NSAttributedString (parsed from markdown, not that it matters here) which may contain URLs that I want to be clickable in a text field within an NSTableView cell. The requirements state that if the user clicks the URL, they be taken to it with the default browser. IF they click anywhere else in the cell, we have default behavior (displaying an additional info popup).
I'm attempting to use a NSTextView to display the content. However, clicking outside the URL but within the view selects the text and eats the mouse click. Making the view not selectable won't allow clicking the URL either. I also don't really want the text to be selectable but that's a minor side problem.
So... I decided to make my view controller an NSTextViewDelegate so I could use some of those callbacks. But my app crashes if I set the NSTextView's delegate property to 'self'. This happens even if I don't implement any of the functions, even though they are all optional.
I'm using Swift 3 and wonder if there's some bug or other issue there? The call stack appears to be sending a textView:willChangeSelectionFromCharacterRanges:toCharacterRanges: message even though it's not implemented. And incidentally, implementing that method isn't helping either.
Any help, or sample code in Swift 3 using the delegate protocol, would be greatly appreciated.
Here's the crash I get by simply setting the delegate property on NSTextView:
By request, here's the code that set's the delegate. Currently I just set it whenever the message changes. This can obviously be optimized but for now I just want to see it work.
var notification: SSNotification! {
didSet {
guard let notificationCellView = self.view as? SSNotificationCellView else { return }
notificationCellView.subjectLabel.stringValue = notification.subject
if let description = notification.message , description != "" {
let attrString = TSMarkdownParser.standard().attributedString(fromMarkdown: description)
notificationCellView.messageLabel.textStorage?.setAttributedString(attrString)
notificationCellView.messageLabel.isHidden = false
notificationCellView.messageLabel.delegate = self
} else {
notificationCellView.messageLabel.isHidden = true
}
}
}
I never did figure out why I was crashing but I was able to come up with a workaround. I was originally trying to make the view controller for the table cell which contained NSTextView be the delegate. I changed it so that the cell's view subclass itself was the delegate and all is well.
I don't get it but it works, so that's what matters.

Update TableView in a view controller with updated data

I am fetching some data from the WCF webservice in a separate thread after each 30 seconds...
and I want something like that a view controller having table view keeps on getting updated whenever i receive fresh data...how can I achieve that...I tried notification but that requires me to come on that particular view controller....
Any advise?
Thanks,
You need not to be on the controller when you send notification.
You can achieve it also using delegates, just called the method of the controller which reload the data of table view when you receive the data.
I hope I have understood your question correctly: You want to get data from a webservice and then update a tableview once you have retrieved the data.
Here's some mockup code:
wcfWebserviceFetcher.tableViewDelegate = mainTableView;
Now in the wcfWebserviceFetcher:
-(void) dataFetchDone {
if(tableViewDelegate!=nil) {
tableViewDelegate.data = self.wcfWebserviceDataResult;
[tableViewDelegate reloadData];
}
}