Post

Replies

Boosts

Views

Activity

Reply to Need Help Transfer Data From Button
Thanks for posting code. But we need to know: what do you get as result of print (are they fired ?):             print("this is the segue destination: \(segue.destination)")             print("name being transferred is \(selectedNameInCell)") how did you define "hs" segue, very precisely: from where (cell ? VC itself ?) did you do a clean build folder after removing one of the duplicates ? Could you also add a print : override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? { if let cell = tableView.cellForRow(at: indexPath) as? FavCell { selectedNameInCell = cell.name.text print("selectedNameInCell", selectedNameInCell) } return indexPath }
Topic: Programming Languages SubTopic: Swift Tags:
Aug ’21
Reply to [Swift] How to properly subclass UITextfield and work with its delegate
Your init is not correct. You miss call to super. required override init(frame: CGRect) {           super.init(frame: frame) delegate = self } why do you add required ? What is the compiler error you get where // CAN'T DO THIS!!! You should also need the init(coder): required init?(coder: NSCoder) { super.init(coder: coder) delegate = self } With this, textFieldDidBeginEditing works as expected.
Topic: Programming Languages SubTopic: Swift Tags:
Aug ’21
Reply to Animate sizeToFit
What I understand from what you show: the change of text should be done in the completion handler of the animation but it will not be possible to animate the sizeToFit stated otherwise, the change from "oldText" to "long, long text" cannot be animated the way you want ; the animation is an image "morphing" animation, not a text substitution. In your case, as sizeToFit is not a property for animation, the delay is not applied ! Test the following and see difference: prints after 4 seconds now. @IBAction func animate2LabelAction(_ sender: NSButton) { self.labelForAnimation.stringValue = "short text" NSAnimationContext.runAnimationGroup({ (context) in context.duration = 4.0 // labelForAnimation.animator().sizeToFit() labelForAnimation.animator().alphaValue = 0.5 // This is animatable }, completionHandler: { print("Completion") } ) } Or add label change in completion: @IBAction func animate2LabelAction(_ sender: NSButton) { self.labelForAnimation.stringValue = "short text" NSAnimationContext.runAnimationGroup({ (context) in context.duration = 4.0 // labelForAnimation.animator().sizeToFit() labelForAnimation.animator().alphaValue = 0.5 }, completionHandler: { self.labelForAnimation.stringValue = "long, long text" print("Completion") } ) }
Topic: Programming Languages SubTopic: Swift Tags:
Aug ’21
Reply to Animate sizeToFit
You can probably try something to get an approaching result. In the IBAction, create a hidden TextField, with same font as label1. Compute initialWidth, the original width of final text in this textField. https://stackoverflow.com/questions/19128797/calculating-uilabel-text-size If initialWidth is larger than label1Width (label1), then apply the animation, with the following compute let scaleRatio = initialWidth / label1Width in the animation, apply a transform with this scale factor in the completion handler, set the finalText in label1 and sizeToFit()
Topic: Programming Languages SubTopic: Swift Tags:
Aug ’21
Reply to [Swift] How to properly subclass UITextfield and work with its delegate
Thanks for feedback. Don't forget to close the thread. As for your next question : the ViewController code which uses MyTextField to also have a UITextFieldDelegate:  class ViewController: UIViewController, UITextFieldDelegate { override func viewDidLoad() { let myTF = MyTextField( .... ) myTF.delegate = self }    func textFieldDidBeginEditing(_ textField: UITextField) {     print("XXXXX"). // This is printed instead of "Inside subclassed textfield" within the subclass.   }  So, if the subclassed textfield's delegate is set to self within the calling code (ViewController in this case), the textFieldDidBeginEditing() inside the subclass is never called. In general, is there a way to trigger textFieldDidBeginEditing() in both the calling code as well as the one inside the subclass? Sequence happens as follows: in viewDidLoad, all subviews are loaded. hence the subclassed textfield's delegate is set to itself  but then, you execute code in viewDidLoad and override the delegate to be the class (ViewController) So only this one will be triggered I do not know of direct mechanism to achieve what you want. And if that was possible, that could make debugging a bit hard. What you could do: remove the delegate declaration in ViewController for myTF in the MyTextField subclass, post a new notification in textFieldDidBeginEditing make ViewController class subscribe to this notification and handle it.
Topic: Programming Languages SubTopic: Swift Tags:
Aug ’21
Reply to TableView Returns Nil - Trying to Update UI in another class.
When posting code, you should use code formatter to make it readable: I have a a static object in HomeVC: static data: MyType = [] { ... didSet { let storyboard = UIStoryboard(name: "Main", bundle: nil) let logsVC = storyboard.instantiateViewController(identifier: "LogsView") as? LogsViewController logsVC?.updateUI(with: HomeViewController.data) } } And a func to update tableview in DetailVC: func updateUI(with data: [MyType]) { self.tableView.reloadData() // this gives nil error on runtime. } What is nil ? tableView ? you call updateUI on a LogsViewController, but you say it is defined in DetailVC ?!?! where is tableView ? In HomeVC ? In LogsViewController ? In DetailVC ? You create a new instance of LogsViewController VC each time you update. Why ? could you post more code so that we can understand better.
Topic: UI Frameworks SubTopic: UIKit Tags:
Aug ’21
Reply to Delete Cells From A Set
The problem is you declare only a get for favSet in the computed var    var favSet: OrderedSet<CurrentPlayers> {     FavouriteManager.shared.favSet   } Declare get and set and that will work. Why don't you use an Array and manage to eliminate duplicate entries in the array ?when you append to it (just test if the new entry is already inside).
Topic: Programming Languages SubTopic: Swift Tags:
Aug ’21
Reply to Delete Cells From A Set
What is the tableView dataSource ? It must be updated before deleteRow. If the dataSource is FavouriteManager.shared.favSet, you must update it as well:     if editingStyle == .delete {       tableView.beginUpdates()       favSet.remove(at: favSet.index(favSet.startIndex, offsetBy: indexPath.row)) FavouriteManager.shared.favSet = favSet // I do not know how you defined, can't be sure it will work       tableView.deleteRows(at: [indexPath], with: .fade)       tableView.endUpdates()     } Please show all delegate func for the tableView, in particular cellForRow
Topic: Programming Languages SubTopic: Swift Tags:
Aug ’21
Reply to Delete Cells From A Set
So your dataSource is favSet. But you get it from the shared. I would try: var favSet: OrderedSet<CurrentPlayers> {     get { FavouriteManager.shared.favSet } set { FavouriteManager.shared.favSet = newValue }   } and revert to the .delete:   if editingStyle == .delete {       tableView.beginUpdates()       favSet.remove(at: favSet.index(favSet.startIndex, offsetBy: indexPath.row))       tableView.deleteRows(at: [indexPath], with: .fade)       tableView.endUpdates()     }
Topic: Programming Languages SubTopic: Swift Tags:
Aug ’21
Reply to DateComponents configuration for x months
I see the documentation says DateComponents.month = a month or count of months.  Where exactly did you read this ? You may misinterpret it. AFAIU, this means that month may be the month value or a number of months to add to a date Day, week, weekday, month, and year numbers are generally 1-based, but there may be calendar-specific exceptions. Ordinal numbers, where they occur, are 1-based. Some calendars may have to map their basic unit concepts into the year/month/week/day/… nomenclature. The particular values of the unit are defined by each calendar and are not necessarily consistent with values for that unit in another calendar. Listing 4 shows how you can create a date components object that you can use to create the date where the year unit is 2004, the month unit is 5, and the day unit is 6 (in the Gregorian calendar this is May 6th, 2004). You can also use it to add 2004 year units, 5 month units, and 6 day units to an existing date. The value of weekday is undefined since it is not otherwise specified. https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/DatesAndTimes/Articles/dtCalendars.html#//apple_ref/doc/uid/TP40003470 What is needed to achieve my goal of repeating every x months? I would try to skip the month: var dateComponents = DateComponents() dateComponents.calendar = Calendar.current // REMOVE THIS dateComponents.month = 1 dateComponents.day = 1 dateComponents.hour = 7
Aug ’21