在 xamarin.ios 中的视图控制器之间传递数据

Passing data between viewcontrollers in xamarin.ios

我正在 Xamarin.iOS (C#) 中开发一个 iPhone 应用程序,我被困在 ViewController 之间传递变量(我正在使用故事板)。

下面列出了我的应用程序的总体布局:

LoginViewController --> TabViewController --> NavigationController --> MainViewController

已添加屏幕截图:

Overview of my Storboard - Explains what I want to do:

我想做的是当用户的凭据在“登录ViewController”成功验证时,应用程序会将用户带到“主要ViewController”。

我还想将一个变量(比方说 'userName')从“LoginViewController”传递到“MainViewController”。但是,我没有运气。

我在下面发布了我的代码。它正在工作(传递变量)但问题是因为我们直接从 LoginView 推送到 MainView,Tabbar 和 Navigation Bar 不再显示.

如果 tabViewController 存在,在 ViewController 之间传递数据的最佳做法是什么?

// Instantiating the MainViewController
MainViewController controller = this.Storyboard.InstantiateViewController("MainViewController") as MainViewController;

//Here I pass the data from the LoginViewController to the MainViewController
controller.userName= this.userName;

// Show the MainViewController
this.NavigationController.PushViewController(controller , true);

如有任何建议,我们将不胜感激!

Subclass ViewController,在它的数据构造函数上添加一些新参数,这样您就可以在显示数据时将数据传递给控制器​​。

Public class 我的ViewController : UIViewController {

    private MyData _myData;

    public MyViewController(MyData myData)
    {
        _myData = myData;
    }
}

然后使用它:

(假设我们已经在另一个具有 NavigationController 的视图控制器中):

var myViewController = new MyViewController(myData); this.NavigationController.PushViewController(我的ViewController, 真);

或(作为 "modal")

var myViewController = new MyViewController(myData); this.PresentViewController(我的ViewController, 真);

//绑定数据到tableView //在你Main VC

public override void ViewDidLoad ()
{
    base.ViewDidLoad ();
    table = new UITableView(View.Bounds); // defaults to Plain style`
    string[] tableItems = new string[] {"a","b","c","d"};//your data to be bind ..You can pass list also
    table.Source = new TableSource(tableItems); //Or you can provide your table name. 
    Add (table);
}
//Create TableView Source to bind data which is coming from VC
public class TableSource : UITableViewSource {

        string[] TableItems;
        string CellIdentifier = "TableCell";

        public TableSource (string[] items)
        {
            TableItems = items;
        }

        public override nint RowsInSection (UITableView tableview, nint section)
        {
            return TableItems.Length;
        }

        public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
        {
            UITableViewCell cell = tableView.DequeueReusableCell (CellIdentifier);
            string item = TableItems[indexPath.Row];

            //---- if there are no cells to reuse, create a new one
            if (cell == null)
            { cell = new UITableViewCell (UITableViewCellStyle.Default, CellIdentifier); }

            cell.TextLabel.Text = item;

            return cell;
        }
}

您可以在 NSUserDefaults 中保存一些数据。

var UserData = NSUserDefaults.StandardUserDefaults;
  UserData.SetString(this.UserName,"UserName");

而且您可以在应用中的任何地方使用它

var UserData = NSUserDefaults.StandardUserDefaults;
UserData.StringForKey("UserName");

您尝试传递数据的方式是正确的,但您无法直接从登录视图打开主视图。如需更多帮助,您应该尝试阅读一些有关使用 TabView 的文章。

https://developer.xamarin.com/guides/ios/user_interface/controls/creating_tabbed_applications/