I'm in need of some help. I'm using Xamarin IOS in Visual Studio 2017 on Windows 10. I'm using storyboard and I have a tab bar controller with 2 view controllers. My view controllers are A_ViewController and B_ViewController. What I would like to do is click a button in A_ViewController that calls an API function in a background thread. The function periodically return some string that I would like to have displayed in a TextView in B_ViewController.
I have the API callbacks working and can recieve the return strings but I'm not sure how I can get the instance of B_ViewController to update the TextView and display the API return strings.
I know that the tabbarcontroller has a property that contains the instances of all its view controllers because I can change view controllers by calling "this.TabBarController.SelectedIndex = 1;" but from A_ViewController how do I use tabbarcontroller (or any other way) to get B_ViewController's instance?
I've tried the following from A_ViewController:
B_ViewController vc = this.TabBarController.ViewControllers[1] as B_ViewController;
vc.Output_Update("Hello World"); <--- This code creates an exception.
My 2 ViewControllers look something like this:
public partial class A_ViewController : UIViewController
{
public override void ViewDidLoad(){
base.ViewDidLoad();
mbtnCallAPI.TouchUpInside += BtnCallAPI_TouchUpInside;
}
public void BtnCallAPI_TouchUpInside(object sender, EventArgs e) {
// Call API here. API Callback will return strings
// Send return string to B_ViewController's TextView.
}
}
public partial class B_ViewController : UIViewController
{
public void Output_Update(string msg) {
mTextView.Text += msg;
}
}
I assume that mTextView wasn't created yet. If you now try to set the text it will throw a NullReferenceException.
In my opinion it is not a good way to make the controller depending on each other. For example A_ViewController shouldn't know about B_ViewController. Instead persist the response-strings somewhere and update the text view in B_Controller when it will be visible.
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.