Monotouch:需要在每个页面中添加导航项(BarButtonItem)和事件

时间:2012-03-12 13:35:52

标签: iphone xamarin.ios monotouch.dialog

嗨,我对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 个答案:

答案 0 :(得分:1)

我认为最好的方法是创建一个新的DialogViewController并在那里实现你的保存代码。

答案 1 :(得分:0)

你应该继承UINavigationView的子类,你的所有视图都应该从它继承