1

I am using Xamarin.iOS. I have created UIView with a few UITextFields. I am looking for best way to initialize text value in these textfields from code. I can pass text data in the constructor of UIViewContoller, but I don't have access to textFields inside it (they are null). I can change text value of textFields in viewDidLoad method.

I don't want to create additional fields in controller class to store data passed by constructor and use them in viewDidLoad. Do you know better solution ?

4

1 回答 1

8

我不想在控制器类中创建额外的字段来存储构造函数传递的数据并在 viewDidLoad 中使用它们。

但这就是它的意思。

或者,如果您使用 MVVM 模式,您可以在视图控制器中创建更少的字段/属性:

public class UserViewModel {
    public string Name { get; set;}
    public string Title { get; set;}
}

public class UserViewController : UIViewController
{
    UserViewModel viewModel;
    public UserViewController (UserViewModel viewModel) : base (...)
    {
        this.viewModel = viewModel;
    }

    public override void ViewDidLoad ()
    {
        userName.Text = viewModel.Name;
        userTitle.Text = viewModel.Title;
    }
}

这种模式可以为您提供大量跨平台(android、WP、...)的代码重用,并清楚地分离关注点。这是一个(非常)一点额外的代码,但它值得每个字节。

于 2013-10-16T13:12:50.163 回答