2015-02-24 61 views
0

Noob Xamarin/MonoTouch.Dialog问题:我在Xamarin Studio的故事板设计器中布置了我的iOS应用程序。我有一个UINavigationController的根视图,包含一个带有静态单元的UITableView,实质上是创建一个主菜单。单元格延续到它们各自的UIViewControllers。将一个DialogViewController拖到一个ViewController到NavigationController栈上给出2页

我想使用MonoTouch.Dialog布局about /设置屏幕。然而,由于我在Xamarin/MonoTouch.Dialog中的新手性,我遇到了一些将整体故事板方法与部分MonoTouch.Dialogue方法相结合的问题。当我在UIViewController上实例化一个新的DialogViewController时,最初的UITableView(AccountViewController),我基本上获得了两个视图添加到导航堆栈,第一个只出现简要,然后显示DialogViewController。我的代码实例化DialogViewController是这样的:

partial class AccountViewController : UIViewController 
{ 
    public AccountViewController (IntPtr handle) : base (handle) 
    { 
    } 

    public override void ViewDidLoad() 
    { 
     base.ViewDidLoad(); 
     var root = new RootElement ("Settings") { 
      new Section(){ 
       new BooleanElement ("Airplane Mode", false), 
       new RootElement ("Notifications", 0, 0) { 
        new Section (null, 
         "Turn off Notifications to disable Sounds\n" + 
         "Alerts and Home Screen Badges for the\napplications below."){ 
         new BooleanElement ("Notifications", false) 
        } 
       }} 
     }; 
     var dialog = new DialogViewController (root, true); 
     this.NavigationController.PushViewController (dialog, true); 
     //this.NavigationController.PresentViewController (dialog, true, null); tried this but then the UINavigationController shows no back buttons 
     //View.AddSubview (dialog.View); tried this but then the UINavigationController shows no back buttons 
    } 
} 

我可能推DialogViewController下旬在堆栈中,创造了中介黑屏,但我无法揣摩出正确的地方是推动DialogViewController根据我当前的体系结构导入导航堆栈。 在interwebs中的大多数样本几乎100%MonoTouch.Dialog,没有故事板...

谢谢!

回答

1

我固定它以下列方式:

我创造了我的TableViewController一个自定义类(含TableView中静态细胞):

partial class MainMenuTableViewController : UITableViewController 
{ 
    public MainMenuTableViewController (IntPtr handle) : base (handle) 
    { 
    } 

    public override void ViewDidLoad() 
    { 
     base.ViewDidLoad(); 
     this.TableView.Delegate = new MainMenuTableViewDelegate (this); //this is the important part. Here I set a custom delegate class for the TableView. I pass the current TableViewController as a parameter in the constructor so I can call the NavigationController from the delegate class to push my custom MonoTouch DialogViewController onto the navigation stack 
    } 
} 

下面是TableViewDelegate代码:

public class MainMenuTableViewDelegate : UITableViewDelegate 
    { 
     private UITableViewController _parentController; 

     public MainMenuTableViewDelegate(UITableViewController parentController) : base() 
     { 
      _parentController = parentController; 
     } 

     public override void RowSelected (UITableView tableView, NSIndexPath indexPath) 
     { 
      if (indexPath.Row == 2) {     
       _parentController.NavigationController.PushViewController (new AccountDialogViewController(), true); 
      } 
     } 
    } 

我重写TableView委托类中的RowSelected方法,并检查当前选定的行是否等于索引2,我想要的TableViewCell的索引已经显示我的MonoTouch DialogViewController(称为AccountDialogViewController)。如果为true,则通过构造函数传入的父级TableViewController的NavigationController属性将我的AccountDialogViewController的新实例推入导航堆栈。

这里是我的AccountDialogViewController:

public class AccountDialogViewController : DialogViewController 
{ 
    public AccountDialogViewController() : base(new RootElement("Account settings"), true) 
    {    
    } 

    public override void ViewDidLoad() 
    { 
     base.ViewDidLoad(); 
     var btnUpdatePassword = UIButton.FromType(UIButtonType.RoundedRect); 
     btnUpdatePassword.SetTitle("Save new password", UIControlState.Normal); 
     btnUpdatePassword.Frame = new RectangleF(0, 0, 320, 44); 
     btnUpdatePassword.TouchUpInside += delegate(object sender, EventArgs e) { 
      var alert = new UIAlertView("Test", "msg", null, "Cancel", null); 
      alert.Show(); 
     }; 
     Root.Add(new Section ("General") { 
      new EntryElement("Username", "type...", "Test"), 
      new EntryElement("E-mail", "type...", "Test"), 
      new RootElement ("Change password") { 
       new Section (null, btnUpdatePassword) { 
        new EntryElement("New password", null, null, true), 
        new EntryElement("Confirm", null, null, true)    
       } 
      }, 
      new StringElement ("Logout", delegate { 
       var alert = new UIAlertView("Are you sure?", "Tapping Yes will log you out of your account", null, "Cancel", "Yes"); 
       alert.Show(); 
      }) 
     }); 
    } 
} 

那么,它就在那里。没有更奇怪的空白屏幕:-)

1

当您的当前ViewController仍在尝试加载时,您正试图将另一个ViewController(在本例中为DialogViewController)推送到导航堆栈上。换句话说,在你的ViewDidLoad中做这件事很糟糕。您必须等到您的当前ViewController完成加载并获得焦点后,才能将另一个视图控制器推入堆栈。

部分使用故事板涉及内置的导航。我可以安全地假设你的“AccountViewController”真的没有做任何事情吗?在那种情况下,我不会继续这样做。取而代之,在您之前的视图控制器上,只需创建DialogViewController,然后手动将其推入堆栈。很难说,没有看你的故事板和架构。

+0

好的,谢谢。所以我想问题是:当UITableViewController中的某个静态单元被点击/触摸时,如何实例化DialogViewController,然后将该DialogViewController推到导航栈上? – Corstiaan 2015-02-25 09:30:47

+1

@Corstiaan在你的UITableSource(或你的UITableViewController)中,你想覆盖'RowSelected()'方法。这是当从UITableView的一行被点击或选中时触发的方法。查看'indexPath.Row'和/或'indexPath.Section'来确定哪一行被选中。从那里您可以将DialogViewController推入导航堆栈。看看如何使用UITableViews的Xamarin文档,它会给你更详细的代码示例。 [Xamarin Docs for UITableViews](http://developer.xamarin.com/guides/ios/user_interface/tables/) – SharpMobileCode 2015-02-25 16:18:12

+0

受你答案的启发,我解决了它。将在明天发布代码:-) – Corstiaan 2015-02-25 17:20:42

相关问题