I have a background in .NET backend and I have decided to learn swift.
It is going very well, but I have a few issues in SwiftUI.
My problem right now is with the picker.
My goal is to pick an item from the picker, return the object and save the object in a variable, so I can post it to api.
The picker is a list of recipes from API, with: name and ID.
I can populate the picker just fine and get the object back, but I can’t save it.
I got the picker and with the selection I can easy get the name.
But how do I save the full object?
Picker("Pick a recipe", selection: $selectedItem) {
ForEach(recipes, id: \.self) { recipe in
Text(recipe.name)
// var res = recipe - I can’t do that, what do I do then?
}
}
Related
All
Something is wired when use swiftui.
object is not update in closure whick assign on second view when navigate back.
code like this:
NavigationLink(destination:
DiscountsView(selectFunc: { (discount: DessertDiscount) in
self.collection.discount = DiscountEntity(discount: discount)
self.testDesc = discount.name
})
) {
Text("优惠方案:\(self.collection.discount.name)")
.font(ViewApperance.shared.font)
.foregroundColor(ViewApperance.shared.fontColor)
}
)
The data Of collection not update, but testDesc update work, is anyone know what happen in this case, and what is the priciple of Object update in Swift?
In a typical list -> details SwiftUI view, I have basically an Array of objects, and for read/edit, I can easily bind these (using #Binding on the view) to view and edit the elements in the array.
What about adding new elements?
I would like to re-use my views for editing; but they expect an #Binding as I mentioned. How do I transition to this view if I want to allow the user to add a new element to the list?
One option is that I can pre-create an object before loading the view, and then binding the view to the new element. However, this makes "cancel" clunky (now I have to remove from the list). Also, it's not clear how to inject this logic (create a new element) in a NavigationLink.
Another option is that I can pass the list to the view and bind a constant empty object, and in the view I can manage adding the new element to the list upon successful creation.
What is the recommended approach to this? I see a lot of tutorials on how to edit and view lists, but not on how to add.
Sounds like you need a backing database. I used Apple's Core Data to add and retrieve stored objects to/from. Here's the guide I used: https://www.hackingwithswift.com/books/ios-swiftui/how-to-combine-core-data-and-swiftui
I figured out a way to do this that is quite nice.
What I've done is that in the list (say, LandmarkList), I added a default LandMark element (which represents the new element to add).
#Published var newLandMark : Landmark
I wrap my view with a new view, which binds against the list:
struct NewLandmarkView : LandmarkDetail {
#Binding var landmarkList : LandmarkList
}
This view adds buttons for save and cancel. Add takes the newLandmark and adds it to the list.
I then use the following to show modally (you can navigate to it if you want instead):
Button(action: {
// In the folder list view, we'll add a note to the "notes" folder
self.showModal = true
}) {
Image(systemName: "square.and.pencil")
}.sheet(isPresented: self.$showModal) {
CreateLandmarkView(landmarkList: self.$landmarkList)
}
This worked pretty well for me as a pattern.
How do I send coredata values when selecting a row in table view to another view controller using SHOW SEGUE in swift 3?
This is my code in objective c using Present Modally Segue and I want the equivalent of that code in swift 3 using Show Segue because I'm using a navigation controller. I've been working on this for a couple hours but as of now, I still haven't been able to do it.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"UpdateDevice"])
{
NSManagedObject *selectedDevice = [self.accounts objectAtIndex:[[self.tableView indexPathForSelectedRow] row]];
detailViewController *destViewController = segue.destinationViewController;
destViewController.account = selectedDevice;
}
}
I'm using polymer 1.0.
I'm trying to open a with on a clic on an element created dynamically with template repeat.
Here is the code :
<paper-button
data-dialog="modal"
on-click="dialogClick">
Click
</paper-button>
and the script (from doc) :
dialogClick: function(e) {
var button = e.target;
while (!button.hasAttribute('data-dialog') && button !== document.body) {
button = button.parentElement;
}
if (!button.hasAttribute('data-dialog')) {
return;
}
var id = button.getAttribute('data-dialog');
var dialog = document.getElementById(id);
alert(dialog);
if (dialog) {
dialog.open();
}
}
This work only if the value of data-dialog is simple text. If I want to change it by data-dialog="{{item.dialogName}}" for instance, it doesn't work. It is not found by the while loop and exit with the if. in the source code of the page, there is no data-dialog in the paper-button.
Any idea ?
I've ran into a similar problem when using data attributes on custom elements. You might want to subscribe to this polymer issue.
As a workaround, place the data attribute in a standard element that is a parent of the button and search for that one instead.
Also, you might want to consider using var button = Polymer.dom(e).localTarget instead of directly accessing e.target, since the later will give you an element deeper in the dom tree under shady dom.
I am developing my application in Sencha touch. In that I have a list and Picker and I want to update the list data dynamically when selecting the picker i.e., I want to add data to list dynamically when tap on 'Done' button of Picker. I used some logic for this but this doesn't update the list content.
listeners: {
change: function(picker,value) {
textValue = picker.getValue()['name'];
var me = this,
nameList = this.down('#namesList');
nameList.add({fullname:textValue}) ;
}
}
When I update like this, it throws me the error that 'Uncaught TypeError: Cannot call method 'add' of null' eventhough 'namesList' is already defined. Please show me the way to solve this problem.
I would add a record to the data/store of the list, and then capture the list and refresh.
The issue there is that nameList isnt actually the list component.
Try adding for example an id to the list, and then in the picker change listener:
Ext.getCmp('mylist-id').refresh()
Hope it helps.