Comments (20)
Subscribe to "INJECTION_BUNDLE_NOtiFICATION" and refresh your view controller. The new code needs to be executed to take effect.
from hotreloading.
Subscribe to "INJECTION_BUNDLE_NOtiFICATION" and refresh your view. The new code needs to be executed to take effect.
I use extensions like this:
import UIKit
extension UIViewController {
#if DEBUG
@objc func injected() {
for subview in self.view.subviews {
subview.removeFromSuperview()
}
if let sublayers = self.view.layer.sublayers {
for sublayer in sublayers {
sublayer.removeFromSuperlayer()
}
}
viewDidLoad()
}
#endif
}
#if DEBUG
extension UIView {
@objc func injected() {
guard let vc = self.viewController() else { return }
for subview in vc.view.subviews {
subview.removeFromSuperview()
}
if let sublayers = vc.view.layer.sublayers {
for sublayer in sublayers {
sublayer.removeFromSuperlayer()
}
}
vc.viewDidLoad()
}
}
#endif
this works fine with code changes in viewcontroller, but not in custom views like a custom tableView cell class.
from hotreloading.
By the way, Sir, I want to buy your book Swift Secrets, but seems it's not available for sale in China in any form. Is there a way for me to buy it?
from hotreloading.
As the view controller class is not changing it doe not get the injected() call. The simplest thing you can do is to subscribe to "INJECTION_BUNDLE_NOtiFICATION" in the view controller displaying the custom cells to redraw unless you can add @injected method to the cells themselves to call setNeedsDisplay().
from hotreloading.
I don't know why it would not be available in China. Have you tried amazon? There is a kindle edition.
from hotreloading.
I don't know why it would not be available in China. Have you tried amazon? There is a kindle edition.
Lol, I finally made a purchase with an US account, haha
from hotreloading.
Enjoy π
from hotreloading.
As the view controller class is not changing it doe not get the injected() call. The simplest thing you can do is to subscribe to "INJECTION_BUNDLE_NOtiFICATION" in the view controller displaying the custom cells to redraw unless you can add @injected method to the cells themselves to call setNeedsDisplay().
So, I update the code to:
` @objc func reset() {
// setupUI()
setNeedsDisplay()
}
override init(frame: CGRect) {
super.init(frame: frame)
NotificationCenter.default.addObserver(self, selector: #selector(reset), name: NSNotification.Name(rawValue: "INJECTION_BUNDLE_NOTIFICATION"), object: nil)
setupUI()
}`
The reset method gets called when command + s, but the cell layout still not changed
from hotreloading.
Is there a way to force the cell to re-layout? Subscribe to "INJECTION_BUNDLE_NOtiFICATION" in the view controller reload the table is probably best. Injecting constraints on table cells doesn't sound straightforward.
from hotreloading.
Is there a way to force the cell to re-layout? Subscribe to "INJECTION_BUNDLE_NOtiFICATION" in the view controller reload the table is probably best. Injecting constraints on table cells doesn't sound straightforward.
Tried tableView.reloadData in viewController's notification observing method, still no luck
from hotreloading.
Any chance you could post a small example project showing constraints not being injected on table cells then I could look at it.
from hotreloading.
InjectionTest.zip
Sure, you can search FIXME to see where my modification not being injected ( I do think it's injected, only I can't find a way to refresh the UI)
from hotreloading.
Thanks for the example. There were just a couple misunderstandings. I added an @objc injected() to OrderCellView calling setupUI() and added a setNeedsLayout() to the end of that function so the constraints get recalculated. The other problem was where you were expecting statusLbl to get updated on injection. This would only happen if the OrderCellView instance was recreated as it is a property that retains the label in an instance variable. Change 'private var statusLbl:UILabel = {...' to 'private var statusLbl:UILabel {...' and you be getting closer though you would need to store the label as follows so it gets recalculated.
let status = statusLbl
addSubview(status)
status.snp.makeConstraints { (make) in
Note that getters are not normally injected but as setupUI is in the same file, when it gets injected, the getters get injected. Reloading the table probably didn't work because the cells where re-used rather than recreated. Injection can only change the code. Without using the injected method in the right way it can't guarantee the code is called again.
from hotreloading.
This is getting weird, did you change the constraints a couple of times? To me, when I only implement @objc injected() in orderCellView, and call setupUI() and setNeedLayout, nothing happened. But when I implement @objc injected() both in orderCellView and ViewController, it works only once. The rest of the tries, the console only give out layout constraints issues.
from hotreloading.
Injection takes a bit of getting used to. The problem is probably the constraints are accumulating and need to be cleared out in OrderCellView.setupUI() somehow or recreate the OrderCellView in EAOrderCell on injection rather than trying to reuse it.
from hotreloading.
Things work much better if you add this to OrderCellView.setupUI()
@objc func setupUI() {
for view in subviews {
view.removeFromSuperview()
}
from hotreloading.
Things work much better if you add this to
OrderCellView.setupUI()
@objc func setupUI() { for view in subviews { view.removeFromSuperview() }
Sorry for the late reply and thank you for the solution offered, I tested it and it works very well in terms of constraints modification. This is a big time saver already regarding layout. Only if we can find a way to replace all the subviews with their new instance respectively, so that the background color, text and other properies of the subview can also take effect after the modification.
from hotreloading.
I figure completely recreating the OrderCellView each time the EAOrderCell is @objc injected() rather than storing it is probably the your best bet but I'm not really familiar with the code.
from hotreloading.
I figure completely recreating the OrderCellView each time the EAOrderCell is @objc injected() rather than storing it is probably the your best bet but I'm not really familiar with the code.
I guess you can close this now, cause I observe the notification in VC, which will call setupUI, and in setupUI I assign a new tableview instance to the variable, everything works perfectly once I press command + s, even the labels and buttons text of the cell subview. So, thanks a lot for this wonderful tool!
from hotreloading.
Excellent π π π
from hotreloading.
Related Issues (20)
- [Help / Question] - Get initial injection working HOT 20
- HotReloading not linking to project HOT 14
- device injection build error in Xcode14 HOT 2
- Unable to create socket for device injection HOT 2
- Crashing on injecting to device HOT 21
- Problems using HotReloading as a Swift package HOT 4
- iOS device injection not working when running as a Mac app HOT 17
- Updates overlap in UI HOT 1
- class category injection failed on device HOT 7
- Some questions about the implementation. HOT 1
- Crasing in SwiftInjection HOT 25
- Is there anyway to re-compile a file only when cmd + s ? HOT 3
- cold start on device encounter watch dog HOT 13
- Hot reloading not changing my views HOT 6
- Does it support C functions γvariable in other files? HOT 7
- Will the DDHotKey affect the file watch ? HOT 6
- Cannot use HotReloading with Other debug configurations (ex. Dev, Test Configuration) HOT 12
- Xcode 15 beta link SwiftTrace failed HOT 8
- Hot reloading failing on real iOS device HOT 12
- Connection refused HOT 12
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from hotreloading.