c# - 从另一个 ViewController 接收后的初始化数据
<p><p>我有两个 ViewController 类。 </p>
<p>首先 - HarachieRolliController。
第二 - DetailTovarProsmotr</p>
<p>我在 Xamarin iOS (C#) 的 MainStoryboard 文件中创建了它</p>
<p>这里截图<a href="/image/jM4Pb.png" rel="noreferrer noopener nofollow"><img src="/image/jM4Pb.png" alt="enter image description here"/></a> </p>
<p>点击 button_arrow_product002 后:</p>
<p>1) 需要打开DetailTovarProsmotr
2) 需要将数据传给DetailTovarProsmotr,并显示在某些字段中,例如(titleproduct001),这是字符串。</p>
<p>头等舱代码:</p>
<pre><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["post_title"].ToString();
button_arrow_product002.TouchUpInside += delegate {
Console.Out.WriteLine ("Clicked Button (button_arrow_product002)");
DetailTovarProsmotr myvarible = new DetailTovarProsmotr (title);
};
}
}
}
</code></pre>
<p>二等代号:</p>
<pre><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;
}
}
</code></pre>
<p>在类和方法中,我有控制台显示,以查看在我的应用程序中工作的阶段。控制台日志:</p>
<ol>
<li><p>ViewDidLoad 方法已运行</p></li>
<li><p>点击的按钮 (button_arrow_product002)</p></li>
<li><p>运行构造函数 DetailTovarProsmotr</p></li>
<li><p>"Горячий ролл с лососем и угрем"Console.Out.WriteLine (mytitle)</p></li>
<li><p>"Горячий ролл с лососем и угрем "HendlerButtonClicked 方法已运行</p></li>
</ol>
<p>我认为数据通过之后 ViewController 开始工作。因为这个titleproduct001.Text = title;不起作用。结果我有警告。
<a href="/image/y3ely.png" rel="noreferrer noopener nofollow"><img src="/image/y3ely.png" alt="enter image description here"/></a>
哇,我可以解决这个问题吗?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>我想我不妨将此作为答案与代码一起发布以供引用。</p>
<p>问题在于您没有正确使用 Storyboard 。 UIViewController 实例之间的转换需要通过 segues 发生。 </p>
<p>现在您可能想知道...如何将标题字符串传递给 DetailTovarPostr?使用 Storyboard 时,不能使用构造函数参数来传递数据,但可以使用 segue!</p>
<p>UIViewController 有一个 PrepareForSegue 方法,可以完全按照您的意愿行事。</p>
<pre><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["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 :)
}
}
</code></pre>
<p>一旦你使用 segue,iOS 将实例化 UIViewController(调用 new)并用它的所有 View 初始化它(如 titleproduct001)。该 View 为 NULL,因为 UIViewController 未正确初始化。</p>
<p>你说你已经在 ViewController 之间定义了一个 segue。如果您需要获取/设置 ID 的帮助,请告诉我,我也会发布。</p></p>
<p style="font-size: 20px;">关于c# - 从另一个 ViewController 接收后的初始化数据,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/34633235/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/34633235/
</a>
</p>
页:
[1]