C# 从另一个ViewController接收后的初始化数据
我有两个ViewController类 第一个是RolliController。 第二-详细信息 我在Xamarin iOS(C#)的MainStoryboard文件中创建了它 这里是截图 单击按钮\u箭头\u product002后: 1) 它需要打开DetailTovarProsmotr 2) 它需要将数据传递给DetailTovarProsmotr,并在某些字段中显示它,例如(titleproduct001),这是字符串 头等舱代码:C# 从另一个ViewController接收后的初始化数据,c#,ios,xamarin,xamarin.ios,C#,Ios,Xamarin,Xamarin.ios,我有两个ViewController类 第一个是RolliController。 第二-详细信息 我在Xamarin iOS(C#)的MainStoryboard文件中创建了它 这里是截图 单击按钮\u箭头\u product002后: 1) 它需要打开DetailTovarProsmotr 2) 它需要将数据传递给DetailTovarProsmotr,并在某些字段中显示它,例如(titleproduct001),这是字符串 头等舱代码: partial class HarachieR
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;
}
}
二级代码:
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;
}
}
在类和方法中,我有控制台显示,以查看我的应用程序中的工作阶段。控制台日志:
我能解决这个问题吗?我想我还是把这个问题作为答案,并附上代码以供参考 问题是你没有正确使用故事板。UIViewController实例之间的转换需要通过分段进行 现在你可能想知道。。。如何将DetailToVarposter传递给标题字符串?使用故事板时,不能使用构造函数参数传递数据,但可以使用分段 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)并使用其所有视图(如标题Product001)对其进行初始化。该视图为空,因为UIViewController未正确初始化
您说您已经在视图控制器之间定义了一个segue。如果您需要获取/设置ID的帮助,请告诉我,我也会发布。您不能使用“new Details ToVarProsMotr(…”,因为UIViewController是在情节提要中定义的。当您新建并尝试使用它时,该类没有正确初始化。你需要使用segue。您可以使用UIViewController的PerformSegue方法来显示DetailTovarProsmotr。我尝试了您的答案,但当我单击按钮时,我的应用程序关闭。您遇到了什么错误?您是否将“您的SEGUE ID”更改为正确的字符串?您是否填写了准备表格?此。性能表格(“标题”,此);public override void prepareforsgue(UIStoryboardSegue,NSObject sender){var detailTovarPostr=segue.DestinationViewController as DetailTovarProsmotr;}您说应用程序正在关闭。我想那意味着撞车。您是否能够获取堆栈跟踪或查看发生了什么错误?