Monotouch: необходимо добавить элемент навигации (BarButtonItem) и событие на каждой странице - PullRequest
0 голосов
/ 12 марта 2012

Привет, я очень новичок в программировании для iphone. Я использовал DialogViewController в моем приложении. В следующем коде событие addButton.Clicked создаст новый корневой элемент с разделами (ячейка таблицы), и мы можем перейти на другую страницу, используя эти разделы.

Мне нужен NavigationItem (RightBarButtonItem) и его событие на каждой навигационной странице для сохранения данных на текущей странице

это очень срочно! Спасибо.

Часть моего кода:

public partial class AppDelegate : UIApplicationDelegate 
    {
        UIWindow _window;
        UINavigationController _nav;
        DialogViewController _RootVC;
        RootElement _rootElement;
        UIBarButtonItem _addButton;
        UIBarButtonItem _EditButton;
        DataBaseAccess da =new DataBaseAccess();

        public string _name; 

        EntryElement StrainName;





        //Load all the datas
        private bool LoadMain()
        {
            _window = new UIWindow (UIScreen.MainScreen.Bounds);
             _rootElement = new RootElement("Strain")
            {
                new Section()
                {
                    (StrainName = new EntryElement ("Strain Name","Enter Name",""))
                },
                new Section()
                {
                }
            };

              List ();


            _RootVC = new DialogViewController (UITableViewStyle.Grouped,_rootElement,true);
            _nav = new UINavigationController(_RootVC);
            //_EditButton= new UIBarButtonItem(UIBarButtonSystemItem.Edit);
            _EditButton  = new UIBarButtonItem("Delete", UIBarButtonItemStyle.Plain,null);
            _addButton = new UIBarButtonItem(UIBarButtonSystemItem.Add);

            _RootVC.NavigationItem.LeftBarButtonItem = _EditButton;
            _RootVC.NavigationItem.RightBarButtonItem = _addButton;

            _addButton.Clicked += (sender, e) => 
            {
                if (StrainName.Value=="")
                {

                    return ;
                }


                da.Addnew (StrainName.Value);


            var strain = new Strains{Name = StrainName.Value};





                var strainElement = new RootElement(StrainName.Value)
                {



                    new Section()
                    {
                        new StringElement("Name",strain.Name)
                    },
                    new Section()
                    {
                        new EntryElement("Strain Type","  Enter Type","")
                    },
                    new Section()
                    {

                        new RootElement("Dispensory")
                            {

                                new Section()
                                {
                                    new EntryElement("Dispensory","  Enter Dispensory name","")
                                },
                            new Section()
                                {
                                    new EntryElement("Address","  Enter Address","")
                                },

                            new Section()
                                {
                                    new EntryElement("City","Enter City","")
                                },
                            new Section()
                                {
                                    new EntryElement("State","Enter State","")
                                },
                            new Section()
                                {
                                    new EntryElement("Zip","Enter Zip","")
                                },
                            new Section()
                                {
                                    new EntryElement("Phone","Enter Phone","")
                                }

                            }
                    },

                    new Section()
                    {
                        new EntryElement("Price","Enter Price per Gram","")
                    },
                    new Section()
                    {
                        new EntryElement("Rating","Enter Rating 1-10","")
                    }

                };
                StrainName.Value = "";

                _rootElement[0].Add(strainElement); 

            };

            _EditButton.Clicked += (sender, e) => 
            {
                 Edit_Elements();

            };


            _window.RootViewController = _nav;
            _window.MakeKeyAndVisible ();


            return true;

        }

        public override bool FinishedLaunching (UIApplication app, NSDictionary options)
        {

              return   LoadMain ();

        }


       //List the data from DB
            private void List (){






            DataBaseAccess Lobj_da=new DataBaseAccess();
            Lobj_da.getstrains ();
            List<string> strains=Lobj_da.Starins;

            foreach (string name in strains)
            {


            var strain = new Strains{Name = name};

                var strainElement = new RootElement(name)
                {

                    new Section()
                    {

                        new EntryElement("Name",strain.Name,strain.Name)
                    },
                    new Section()
                    {
                        new EntryElement("Strain Type","  Enter Type","")
                    },
                    new Section()
                    {

                        new RootElement("Dispensory")
                            {
                                new Section()
                                {
                                    new EntryElement("Dispensory","  Enter Dispensory name","")
                                },
                            new Section()
                                {
                                    new EntryElement("Address","  Enter Address","")
                                },

                            new Section()
                                {
                                    new EntryElement("City","Enter City","")
                                },
                            new Section()
                                {
                                    new EntryElement("State","Enter State","")
                                },
                            new Section()
                                {
                                    new EntryElement("Zip","Enter Zip","")
                                },
                            new Section()
                                {
                                    new EntryElement("Phone","Enter Phone","")
                                }

                            }
                    },

                    new Section()
                    {
                        new EntryElement("Price","Enter Price per Gram","")
                    },
                    new Section()
                    {
                        new EntryElement("Rating","Enter Rating 1-10","")
                    }

                };
                StrainName.Value = "";

                _rootElement[0].Add(strainElement); 

            }





        }
        //



        //Edit Changes
         void ConfigEdit (DialogViewController dvc)
        {
            //dvc.NavigationItem.RightBarButtonItem = new UIBarButtonItem (UIBarButtonSystemItem.Edit, delegate {

            dvc.NavigationItem.LeftBarButtonItem  = new UIBarButtonItem("Update", UIBarButtonItemStyle.Plain,null);
                    dvc.TableView.SetEditing (true, true);
                    ConfigDone (dvc);
        //  });

        }

        void ConfigDone (DialogViewController dvc)
        {
            dvc.NavigationItem.RightBarButtonItem = new UIBarButtonItem (UIBarButtonSystemItem.Done, delegate {


                dvc.TableView.SetEditing (false, true);
                ConfigEdit (dvc);
                LoadMain();
            });
        }

        public void Edit_Elements () 
        {

            RootElement _rootElement_Edit;
             _rootElement_Edit = new RootElement("Strain")
            {        
                new Section()
                {
                }
            };
                DataBaseAccess LEobj_da=new DataBaseAccess();
                LEobj_da.getstrains ();
               List<string> strains=LEobj_da.Starins;

            foreach (string name in strains){


            var strain = new Strains{Name = name};

                var strainElement = new RootElement(name)
                {

                    new Section()
                    {

                        new StringElement("Name",strain.Name)
                    }

                };
                _rootElement_Edit[0].Add(strainElement);
            }


            var dvc = new EditingDialog (_rootElement_Edit, true);
            ConfigEdit (dvc);

            _nav.PushViewController (dvc, true); 

    }



    } `

Ответы [ 2 ]

1 голос
/ 12 марта 2012

Я думаю, что лучший способ - это создать новый DialogViewController и реализовать там свой код сохранения.

0 голосов
/ 12 марта 2012

Вы должны создать подкласс UINavigationView, и все ваши представления должны наследовать его

...