I need to create the GUI for setting my app preferences. Since I am not able to get multiple instances of the SharedUserDefaultsController in multiple storyboard scenes, I created a simple proxy for it. I'm trying to bind a text field content to a specific values of the SharedUserDefaultsController through the proxy which is represented in IB via an NSObject added to the scene that should contain the binding. For the binding I use self.defaults.values.default_key where default_key is a defined key in the StandardUserDefaults for my application.
When I try to show the view that contains the binding, though, it is not shown and the only displayed message in the console is
Missing placeholder for identifier UpstreamPlaceholder-37m-v1-XT6
What am I doing wrong? In the storyboard source there's no trace of that identifier. Here's the code of the SharedUserDefaultsControllerProxy class that I created:
import Cocoa
class SharedUserDefaultsControllerProxy: NSObject{
var defaults = NSUserDefaultsController.sharedUserDefaultsController()
override init() {
super.init()
}
}
Related
Using Xcode 12, and starting with the default XIB-based swift desktop app, I add a Model class, descended from NSObject with a single stringValue property. In the XIB file I instantiate the Model class, and add a text field to the default window, and bind the value of the text field to the stringValue property of my Model instance. The project builds and runs, and any value I set in the initializer of the Model class displays in the text field.
Here is my Model class:
import Foundation
#objc public class Model : NSObject {
#objc dynamic public var stringValue: NSString
override init() {
stringValue = "some string"
}
}
The full project is available here:
https://github.com/thomasmarschall/KvoLab2023
I started with a bit more complicated setup involving a separate library and framework, but my current setup seems to be super straightforward.
Having done all of this, Xcode displays a grey flag in the Model Key Path field of the text field value binding, and the tool tip for the flag reads "xcode cannot resolve the entered keypath". Also, the autocomplete in the key path field does not work. How can I resolve these issues? Any help would be appreciated.
In my application I want to change the editable var at once for 3 nstextfields in a custom NSView. Is it possible to do that using only one line of code (turning something on and of in the NSView perhaps?) or do I have to change it for every object individually? Again all the nstextfields are 'grouped' in one NSView. Hiding the nsview is no option, because it will seem like the entire program is nearly empty.
I have (for example) 3 NSTextFields in a custom NSView and I want to change their edibility option for all of them at once (if that is possible). So that I don't have to do: 'textfield1.editable=false' three times
Use Cocoa Bindings:
Create a dynamic variable editable in the target class
dynamic var editable = true
In Interface Builder bind Editable of each text field to the target class , Model Key Path editable.
Now changing the value of the variable affects all text fields simultaneously.
I have a maybe slightly unconventional question. I'm working on an app that has two different core data entities (sessions and routines). Subsequently I have two different view controllers in interface builder to fill the attributes of those entities. The thing is that those two entities are identical aside from the fact that I need sessions to have a many to one relationship with a clients entity and routines to have no relationship.
The question is this. Id like to use the same view controller in interface builder for both my session and routine classes, is there a way that I can programatically determine what class the interface builder viewcontroller is assigned to based on a segue identifier? I know it would have to be some sort of if segue.identifier == "myIdentifier" {
/*code for changing destination view controller's class*/} statement but I'm not sure what the code would be, and I also want to hear other suggestions of ways to possibly do this more efficiently.
Thanks!
The view-controller in your Storyboard is an archived object of a certain class, and you can't just assign it to some other class. (Just as if you have a String object and want to assign it to be an Int wouldn't work well)
You can check the destinationViewController property of the segue within prepareForSegue: if it is a certain class like this:
if let dvc = segue.destinationViewController as? MyExpectedVC {
// do something
}
but that is probably not what you want/should do.
I'd go along the lines of what the commenters of your question have said and would suggest that your view controller class implements a configuration method for both your models. Either a separate one for each class like this:
func prepareWithSession(session: Session) {/* ... */}
func prepareWithRoutine(session: Routine) {/* ... */}
or (imho a bit nicer) create a protocol for all classes that can be displayed by the view controller and have only one method:
func prepareWithDisplayable(session: DisplayableType) {/* ... */}
where DisplayableType would be a protocol that defines all properties that this view controller needs to know about.
You can have custom logic in the view controller that displays some fields only for specific Types. (e.g. sets views hidden for other Types)
I am in the process of cleaning up my code as my TableViewController class has become rather full and long. To clean up the code, I would like to create a number of utility classes that provide methods for the TableViewController. These methods/functions, in my current implementation, are able to change variables in the main TableViewController. Here is an example of my current setup:
class FooImplementation: NSObject {
var viewController: Foo
init(viewController: TableViewController) {
self.viewController = viewController
}
}
class FooUtility1: FooImplementation {
// Methods
}
class FooUtility2: FooImplementation {
// Methods
}
class Foo: TableViewController {
var fooUtility1: FooUtility1
var fooUtility2: FooUtility2
override viewDidLoad() {
fooUtlity1 = FooUtility1(self)
fooUtlity2 = FooUtility2(self)
}
// Use of the methods...
}
Is there a better/universally defined way of creating and using these classes? And can these classes be combined into one Utility class that has access to all of the methods FooUtility1 and FooUtility2 provide?
Thanks!
I don't consider adding utility classes a good design pattern, for at least the following reasons:
they can change the internal status of the view controller
you have to "upgrade" member properties to internal or public when they are private by nature
Of course I don't know what kind of behaviors you want to move from view controllers to the utility classes - my advice is to use one or more of the following:
create view controller extensions
create your own set of base view controllers, inherited from UIViewController, to be used as super classes for your final view controllers
move some logic to external classes, but use the delegation pattern to interact
The second case is one I frequently use, and the 3rd is one I've applied to my latest project, using better separation of concerns by implementing for each view to be displayed:
a view controller, responsible of managing (but not displaying) the view, and handling events received from its view(s)
a data source, responsible of providing the data to be displayed in the view, along with methods to add/delete/update if needed
a (hierarchy of) view, implemented in a separate class in its own xib file.
It makes development a little more complicated, but in the end I have a light view controller, and a view that is responsible of displaying data, handling events and forwarding them to the view controller.
Note that this is not the solution, just one possible solution. There are several variables to take into account, for sure what you want to move from the view controller, and I think also personal preference.
On my first project trying out Caliburn.Micro, I like a lot of the things :-)
One thing I miss (or havn't discovered yet) is how to separate the viewmodel and a command.
CM doesn't support ICommand, as it's way of doing things is superior. I'm sure it's true, so I would love a small push in the right direction to achieve this or perhaps discover a better way.
As I understand you have to put the "Execute" method and "CanExecute" property directly in the viewmodel, named to match the control in the view, to get the magic to work.
I would like to put the "Execute" and "CanExecute" in a different object that is property on the viewmodel and then CM would automatically bind to that object, using the control name and property names as usually.
Repost from the forum on Caliburn Micro, I didn't get any answers so I'm trying my luck here.
You should try to avoid invalidating the Law of Demeter. Therefore, on your view model you can have an execute method, and a CanExecute property (usually calculated), and these can call into the containing model where appropriate, e.g:
public void Save
{
// .. save logic
}
public bool CanSave
{
get
{
return this.model.CanSave ... and other logic etc.
}
}
You must remember to notify a change in the calculated property when the can save state changes, e.g:
public void CodeThatGetsRunWhenAPropertyOfTheModelChanges()
{
this.NotifyOfPropertyChanged(() => this.CanSave);
}
If you have e.g. a Button on your view with x:Name="Save", then Caliburn.Micro will automatically invoke your Save verb on the view model when the button is clicked, and will automatically enable and disable the button when the CanSave property value changes.
To avoid fat ViewModels you also need to avoid fat Views. Caliburn.Micro allows you to compose Views/ViewModels as described in Screens, Conductors and Composition.
The short version is, you can include a "DetailView" and "DetailViewModel" pair in a "MasterView"/"MasterViewModel" shell by defining a DetailViewModel-typed property in MasterViewModel and adding a ContentControl named after it in MasterView. Binding and actions work as usual, so you avoid both fat models/views and routing of commands.
Another option is to bind a MasterView element to a DetailViewModel property or action, by prepending the detail's property to the target's name. I can't find the specific URL yet, so the example is from memory.
Assuming you have the following classes:
public class MasterViewModel:Screen
{
public property DetailViewModel MyDetails{get;set;}
}
and
public class DetailViewModel:Screen
{
public property string SomeText{get;set;}
public void DoTheBoogie(){}
}
You can add a control in you MasterView named 'MyDetails_SomeText' to bind to the DetailViewModel.SomeText. You can also bind to DoTheBoogie the same way.
I prefer to create a separate View though, named DetailView and add a ContentControl named "MyDetails" in MasterView. This results in a cleaner and more modular design