如何以编程方式从 MonoTouch 中的 ViewController 加载另一个 viewController [英] How to programmatically load another viewController from a ViewController in MonoTouch

查看:19
本文介绍了如何以编程方式从 MonoTouch 中的 ViewController 加载另一个 viewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Monotouch 中使用 StoryBoards,并且有一个被指定为初始 VC 的 ViewController.我有第二个 VC,我想通过第一个 VC 的 ViewDidLoad 方法以编程方式显示.Objective-C 的步骤是这样的

I am using StoryBoards in Monotouch and have a ViewController which has been designated as the Initial VC. I have a second VC that I want to display programmatically from the ViewDidLoad method of the first VC. The Objective-C steps are like so

  1. 通过Storyboard.InstantiateViewController("SecondVC")
  2. 实例化第二个VC
  3. 设置它的ModalTransitionalStyle
  4. 将其委托设置为 self 就像 secondVC.delegate = self;
  5. 使用 this.PresentViewController(secondVC, true, nil)

我如何在 MonoTouch C# 代码中实现这一点?

How do I accomplish that in MonoTouch C# code?

我使用 Storyboard.Ins.. 方法实例化的 VC 没有要设置的委托或委托属性.尽管我的代码可以编译,但我没有看到第二个视图.我只看到第一个视图

the VC that I instantiate using Storyboard.Ins.. method does not have a delegate or Delegate property to set. And although my code compiles, I do not see the second view. I see only the first View

非常感谢任何帮助

谢谢

推荐答案

如果您延迟调用它,您应该能够做到这一点.我使用 Threading.Timer 在加载后一秒钟调用 PresentViewController.就委托而言, UIViewController 没有该属性.您必须转换为适用于您正在加载的控制器的控制器类型.然后你可以设置委托.如果您想使用它(self),您可能需要设置 WeakDelegate 而不是 Delegate.

You should be able to do this if you call it on a delay. I used a Threading.Timer to call PresentViewController a second after load. As far as the delegate, a UIViewController does not have that property. You will have to cast to the controller type that is applicable to the controller you are loading. Then you can set the delegate. You will probably want to set the WeakDelegate instead of the Delegate if you want to use this (self).

public override void ViewDidLoad ()
{
    base.ViewDidLoad ();

    Timer tm = new Timer (new TimerCallback ( (state)=> {
        this.InvokeOnMainThread (new NSAction (()=> {
            UIStoryboard board = UIStoryboard.FromName ("MainStoryboard", null);
            UIViewController ctrl = (UIViewController)board.InstantiateViewController ("Number2VC");
            ctrl.ModalTransitionStyle = UIModalTransitionStyle.CrossDissolve;
            this.PresentViewController (ctrl, true, null);
        }));
    }), null, 1000, Timeout.Infinite);
}

这篇关于如何以编程方式从 MonoTouch 中的 ViewController 加载另一个 viewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆