我有两个ViewController类。
首先 - HarachieRolliController。 第二 - DetailTovarProsmotr
我在Xamarin iOS(C#)
的MainStoryboard文件中创建了它点击button_arrow_product002后:
1)需要打开DetailTovarProsmotr 2)需要将数据传递给DetailTovarProsmotr并在某些字段中显示,例如(titleproduct001),这是字符串。
头等代码:
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);
};
}
}
}
第二类代码:
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;
}
}
在类和方法中,我有控制台显示,以查看在我的应用程序中工作的阶段。控制台日志:
答案 0 :(得分:1)
我想我也可以发布这个作为答案的代码供参考。
问题是您没有正确使用故事板。 UIViewController实例之间的转换需要通过segue进行。
现在您可能想知道......如何将DetailTovarPostr传递给标题字符串?使用Storyboard时,不能使用构造函数参数来传递数据,但可以使用segues!
UIViewController有一个PrepareForSegue方法,可以完全按照你想要的方式完成。
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)");
// Use a segue to display the DetailTovarProsmotr
this.PerformSegue('YOUR SEGUE ID', this);
// The segue needs to be defined in the storyboard with a unique id
};
}
protected override void PrepareForSegue(UIStoryboardSegue segue, NSObject sender) {
var detailTovarPostr = segue.DestinationViewController as DetailTovarPostr;
// Initialized your custom view controller however you like
// Consider defining properties or an initialize method and pass in the title and other data
// TODO pass data :)
}
}
一旦你使用了segue,iOS就会实例化UIViewController(调用new)并用它的所有视图初始化它(比如titleproduct001)。该视图为NULL,因为UIViewController未正确初始化。
您说您已经在视图控制器之间定义了segue。如果您需要一些帮助来获取/设置ID,请告诉我,我也会发布。