Reputation: 5462
I'm using MonoTouch for developing my iPhone application. I want to use Monotouch. Dialog for showing some data to a client and let them change data and then save them to the file again.
My code is something like the code of Xamarin tutorials: (Orginal Sample link)
public enum Category
{
Travel,
Lodging,
Books
}
public class ExpesObject{
public string name;
}
public class Expense
{
[Section("Expense Entry")]
[Entry("Enter expense name")]
public string Name;
[Section("Expense Details")]
[Caption("Description")]
[Entry]
public string Details;
[Checkbox]
public bool IsApproved = true;
[Caption("Category")]
public Category ExpenseCategory;
}
It is representing the TableView
so good.
But the question is this, how we can save the data of this elements and the use them in other class of application? What is the best way for doing this?
I guess that we can save data to a file when user changed them. but how we can detect when the user change the data?
Upvotes: 1
Views: 619
Reputation: 2398
In the example you have shown, you are using the simple Reflection API for Monotouch.Dialog. While this is nice and easy, it really limits what you can do. I would suggest learning to use the Elements API seen here (http://docs.xamarin.com/guides/ios/user_interface/monotouch.dialog/elements_api_walkthrough) of Monotouch.Dialog which will give you so much more control over each item in the table, and be able to detect the changes, etc.
Each of the table cells (e.g. Name is a cell, which you can edit) have actions/events for when certain things happen, like text being changed.
For example, the above screen can be made with the elements API doing the following.
public class ExpenseViewController : DialogViewController
{
EntryElement nameEntry;
public ExpenseViewController() : base(null, true)
{
Root = CreateRootElement();
// Here is where you can handle text changing
nameEntry.Changed += (sender, e) => {
SaveEntryData(); // Make your own method of saving info.
};
}
void CreateRootElement(){
return new RootElement("Expense Form"){
new Section("Expense Entry"){
(nameEntry = new EntryElement("Name", "Enter expense name", "", false))
},
new Section("Expense Details"){
new EntryElement("Description", "", "", false),
new BooleanElement("Approved", false, ""),
new RootElement("Category", new Group("Categories")){
new CheckboxElement("Travel", true, "Categories"),
new CheckboxElement("Personal", false, "Categories"),
new CheckboxElement("Other", false, "Categories")
}
}
};
}
void SaveEntryData(){
// Implement some method for saving data. e.g. to file, or to a SQLite database.
}
}
Consider these areas to get started using the Elements API: Source: https://github.com/migueldeicaza/MonoTouch.Dialog
MT.D intro: http://docs.xamarin.com/guides/ios/user_interface/monotouch.dialog
MT.D Elements walkthrough: http://docs.xamarin.com/guides/ios/user_interface/monotouch.dialog/elements_api_walkthrough
Upvotes: 2