I have two ViewController classes.
First - HarachieRolliController. Second - DetailTovarProsmotr
I created it in MainStoryboard file in Xamarin iOS (C#)
After clicking to button_arrow_product002 :
1) It needs to open DetailTovarProsmotr 2) It need to pass data to DetailTovarProsmotr and display it in some fields , for example (titleproduct001), this is string.
First class code:
partial class HarachieRolliController : UIViewController
{
public HarachieRolliController (IntPtr handle) : base (handle)
{
}
public async override void ViewDidLoad ()
{
base.ViewDidLoad ();
// Perform any additional setup after loading the view, typically from a nib.
string url2 = "http://papajohn.pp.ua/?mkapi=getProductsByCat&cat_id=83";
JsonValue json = await FetchAsync(url2);
ParseAndDisplay (json);
}
private async void ParseAndDisplay(JsonValue json)
{
String title = json[1]["post_title"].ToString();
button_arrow_product002.TouchUpInside += delegate {
Console.Out.WriteLine ("Clicked Button (button_arrow_product002)");
DetailTovarProsmotr myvarible = new DetailTovarProsmotr (title);
};
}
}
}
Second class code:
public partial class DetailTovarProsmotr : UIViewController
{
public string mytitle;
public DetailTovarProsmotr (IntPtr handle) : base (handle)
{
}
public DetailTovarProsmotr(String title){
this.mytitle = title;
Console.Out.WriteLine ("Constructor DetailTovarProsmotr is run");
Console.Out.WriteLine (mytitle+" Console.Out.WriteLine (mytitle)");
HendlerButtonClicked (mytitle);
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
Console.Out.WriteLine (mytitle+" ViewDidLoad metod is run");
}
public void HendlerButtonClicked(String title){
Console.Out.WriteLine (title+" HendlerButtonClicked metod is run");
titleproduct001.Text = title;
}
}
In classes and methods I have Console displaying, to see stages of working in my app. Console log:
ViewDidLoad metod is run
Clicked Button (button_arrow_product002)
Constructor DetailTovarProsmotr is run
"Горячий ролл с лососем и угрем" Console.Out.WriteLine (mytitle)
" Горячий ролл с лососем и угрем " HendlerButtonClicked metod is run
I think that data passes later that ViewController start to work. And because of this titleproduct001.Text = title; doesn't works. I have warning as result.
Gow I can fix this?
I figured I might as well post this as an answer with the code for reference.
The problem is that you're not using the Storyboard correctly. Transitions between UIViewController instances needs to occur through segues.
Now you might be wondering... How do I pass the DetailTovarPostr the title string? When using a Storyboard, you can't use constructor parameters to pass data, but you can use segues!
UIViewController has a PrepareForSegue method which will do exactly what you want.
Once you use a segue, then iOS will instantiate the UIViewController (call new) AND initialize it with all of its views (like the titleproduct001). That view is NULL because the UIViewController was not properly initialized.
You say that you already have a segue defined between the view controllers. If you need some help getting/setting the ID, then let me know and I'll post that as well.