Mark
Mark

Reputation: 43

Passing data between viewcontrollers in xamarin.ios

I am developing an iPhone app in Xamarin.iOS (C#) and I am stuck at passing variables between ViewControllers (I am using storyboards).

General layout of my app is listed below:

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

SCREEN SHOT ADDED:

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

What I would like to do is when user' credentials successfully validated at the "LoginViewController", the app will take user to "MainViewController".

I also want to pass a variable (let's say 'userName') from "LoginViewController" to "MainViewController". However, I got no luck.

I posted my code below. It is working (passes variable) but the problem is that since we are pushing directly from LoginView to MainView, Tabbar and Navigation Bar do not shown anymore.

What are the best practices for passing data between ViewController if tabViewController exists?

// 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);

Any suggestions will be appreciated!

Upvotes: 0

Views: 1424

Answers (2)

Narendra Sharma
Narendra Sharma

Reputation: 682

Subclass the ViewController, adding some new parameter(s) on it's constructor for the data, so that you can pass the data to the controller when you display it.

Public class MyViewController : UIViewController {

    private MyData _myData;

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

Then use it:

(assuming we're already in another view controller that has a NavigationController):

var myViewController = new MyViewController(myData); this.NavigationController.PushViewController(myViewController, true);

or (as a "modal")

var myViewController = new MyViewController(myData); this.PresentViewController(myViewController, true);

//Binding Data to tableView //In you 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;
        }
}

Upvotes: 1

Karan Rami
Karan Rami

Reputation: 321

You can save some data in NSUserDefaults.

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

and you can use it anywhere in your app

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

The way you are trying to pass data is correct but you can't directly open mainview from LoginView. For more help you should try to read some articles on using TabView.

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

Upvotes: 1

Related Questions