简体   繁体   中英

Updating Monotouch.Dialog RootElement

I have been following the code from the monotouch.dialog task sample app (http://docs.xamarin.com/ios/Guides/User_Interface/MonoTouch.Dialog/Elements_API_Walkthrough).

Something I dont seem able to work out, is when the user clicks the + button a new row to the table is added. The user touches this and navigates to another screen where they can enter information. Now, when they navigate back to the root view, I want the list of rootElements to be updated so the entered name is used instead of the default name 'connection'

How would I go about updating the text for each of the RootElements based upon what has been entered?

I hope that all makes sense!

-- code below.

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;
    }
}

And :

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; }
}

Did you try this.ReloadData(); like this ?

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();
}

You can find other topic about that here .

EDIT

You forgot to say that this is located in your AppDelegate, which is not made for what you are doing.

At first, create a DialogViewController: Project > Add new file > MonoTouch > DialogViewController . And in the different method I mentioned earlier, put the ReloadData() method. These methods (ViewDidLoad, WillAppear and so on) are override methods from UIView. AppDelegate exists to get data before your is launched, store static data for your app, and so on. It's completly different usage.

And for your AppDelegate, you should have this for example:

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM