【问题标题】:Updating Monotouch.Dialog RootElement更新 Monotouch.Dialog RootElement
【发布时间】:2012-11-07 10:15:22
【问题描述】:

我一直在关注 monotouch.dialog 任务示例应用 (http://docs.xamarin.com/ios/Guides/User_Interface/MonoTouch.Dialog/Elements_API_Walkthrough) 中的代码。

我似乎无法解决的问题是,当用户单击 + 按钮时,表格中会添加一个新行。用户触摸它并导航到另一个屏幕,他们可以在其中输入信息。现在,当他们导航回根视图时,我希望更新 rootElements 列表,以便使用输入的名称而不是默认名称“连接”

我将如何根据输入的内容更新每个 RootElements 的文本?

我希望一切都有意义!

-- 代码如下。

public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
    window = new UIWindow (UIScreen.MainScreen.Bounds);

    RootElement _rootElement = new RootElement ("To Do List"){ new Section()};

    DialogViewController _rootVC = new DialogViewController (_rootElement);

    // This adds an 'add' button to the root view controller, and handles by delegate,the push to a screen where you can add a new vCenter connection profile.
    UIBarButtonItem _addButton = new UIBarButtonItem (UIBarButtonSystemItem.Add);
    _rootVC.NavigationItem.RightBarButtonItem = _addButton;

    _addButton.Clicked += (sender, e) =>  {
        var connectionProfile = new connectionProfile{};
        // See, on the line below, I add a default RootElement with the text New Connection.

        var connectionProfileElements = new RootElement ("New Connection") {
            new Section () {
                // When they enter the name on the next line of code, I want to use this to update the New Connection text on the root screen.
                new EntryElement ("Name", "Enter Connection Name", connectionProfile._connectionName),
                new EntryElement ("VC Address", "Enter VC Address", connectionProfile._address),
                new EntryElement ("VC Port", "Enter VC Port", connectionProfile._port),
                new EntryElement ("Username", "Enter Username", connectionProfile._userID),
                new EntryElement ("Password", "Enter Password", connectionProfile._password,true)}      
            };
            _rootElement [0].Add (connectionProfileElements);
        };

        UINavigationController _nav = new UINavigationController (_rootVC);
        window.RootViewController = _nav;
        window.MakeKeyAndVisible ();

        return true;
    }
}

还有:

public class connectionProfile
{
public connectionProfile ()
    {
    }

    public string _connectionName { get; set; }
    public string _address { get; set; }
    public string _port { get; set; }
    public string _userID {get; set; }
    public string _password { get; set; }
}

【问题讨论】:

  • 我相信简而言之,我要问的是:给定一个带有默认“标题”新连接的 RootElement,我需要如何覆盖/处理什么事件才能使用连接配置文件更新它。 _connectionName 一旦被输入并且用户正在导航回表格视图?

标签: c# ios xamarin.ios monotouch.dialog


【解决方案1】:

你试过这样this.ReloadData();吗?

public NameOfYourDialogViewController() : base (UITableViewStyle.Grouped, null)
{
    Root = new RootElement ("test") {
        new Section ("First Section"){
            new StringElement ("Hello", () => {
                new UIAlertView ("Hola", "Thanks for tapping!", null, "Continue").Show (); 
            }),
            new EntryElement ("Name", "Enter your name", String.Empty)
        },
        new Section ("Second Section"){
        },
    };
    }
}
public override void ViewDidLoad ()
{
    base.ViewDidLoad ();
    // Do your stuff
}

public override void ViewWillAppear (bool animated)
{
    base.ViewWillAppear (animated);
    this.ReloadData();
}

您可以找到关于该here 的其他主题。

编辑

你忘了说它位于你的 AppDelegate 中,它不是为你正在做的事情而制作的。

首先,创建一个 DialogViewController:项目 > 添加新文件 > MonoTouch > DialogViewController。 而在我之前提到的不同方法中,放置了 ReloadData() 方法。 这些方法(ViewDidLoad、WillAppear 等)是 UIView 的重写方法。 AppDelegate 的存在是为了在你启动之前获取数据,为你的应用存储静态数据,等等。这是完全不同的用法。

对于你的 AppDelegate,你应该有这个,例如:

public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
    _window = new UIWindow (UIScreen.MainScreen.Bounds);
    _controller = new NameOfYourDialogViewController();

    _navigationController = new UINavigationController (_controller);

    UIImageView splash = new UIImageView(_window.Bounds);
    splash.Image = UIImage.FromFile("Default.png");

    _window.AddSubview(splash);
    _window.AddSubview(_navigationController.View);
    _window.BringSubviewToFront(splash);

    _window.MakeKeyAndVisible ();

    // This is used to create a fadding effect in your splashscreen
    UIView.Animate(1,
            delegate { splash.Alpha = 0f; },
            delegate {
                _window.RootViewController = _navigationController;
                splash.RemoveFromSuperview();
            });
    return true;
}

【讨论】:

  • 我无法添加 ViewWillAppear 部分,它在编辑器中出错,但我不知道为什么?我在这里附上了我的项目:dropbox.com/s/kaonx1vh7xp8zyg/Archive.zip 非常感谢任何和所有帮助
  • 我用一个更好、最完整的答案编辑了我的答案。 :) 我希望这会有所帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-27
  • 2012-01-22
  • 1970-01-01
相关资源
最近更新 更多