问题描述
限时送ChatGPT账号..我正在尝试在 SettingViewController 关闭之前分配文本字段条目,并在 URLViewController 出现时检查它们.
I am trying to assign textfield entries just right before SettingViewController dismissed, and checking them while URLViewController appears.
但是,一旦我检查它返回 null,即使用户输入了所有必需的信息;
However, once I check it returns null even though user entered all required information;
//设置视图控制器
partial class SettingViewController : UIViewController
{
public ServerSettings sSettings;
public SettingViewController (IntPtr handle) : base (handle)
{
this.Title = "Settings";
this.sSettings = new ServerSettings();
}
public override void ViewWillDisappear (bool animated)
{
sSettings.server=serverTF.Text;
sSettings.port = portTF.Text;
sSettings.password = passwordTF.Text;
sSettings.userid = inboxuserTF.Text;
sSettings.username = usernameTF.Text;
}
}
//URLViewController
//URLViewController
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear (animated);
SettingViewController callSetting = this.Storyboard.InstantiateViewController ("SettingViewController") as SettingViewController;
if (callSetting == null)
throw new Exception("callSetting is null"); // Or if you can handle having a null callSetting then correct for it, but realistically this is a problem, so I'd throw an Exception
if (callSetting.sSettings == null)
throw new Exception("sSetting is null"); // Or if you can handle having a null callSetting.sSetting then correct it (such as using a default value).
if (string.IsNullOrEmpty(callSetting.sSettings.password)==true)
Console.WriteLine("is null or empty");
else
Console.WriteLine (callSetting.sSettings.password);
}
即使用户填写了所有文本字段条目,它也会返回 null.//设置视图控制器
Even though user fullfill all the textfield entries, it returns null. //SettingsViewController
//URLViewController
//URLViewController
推荐答案
我会通过使用 dictionary& 来解决这个问题用户默认设置如下:
I would approach the problem by using dictionary& userdefaults as follows:
在 B 视图控制器中,将用户条目保留在字典中,将其映射并分配给用户默认值.
In the B Viewcontroller, keep the user entries in the dictionary, map it and assign it to the userdefault.
partial class BViewController : UIViewController
{
const string server = "server";
const string port = "port";
const string password = "password";
const string username = "username";
const string inboxuserid = "inboxuserid";
.............
.............
public override void ViewWillDisappear (bool animated)
{
var appDefaults = new NSDictionary (server, serverTF.Text, port, portTF.Text, password, passwordTF.Text,username,usernameTF.Text, inboxuserid,inboxuserTF.Text);
NSUserDefaults.StandardUserDefaults.RegisterDefaults (appDefaults);
NSUserDefaults.StandardUserDefaults.Synchronize ();
}
}
并在 A ViewController 中检索它们,如下所示:
and retrieve them in the A ViewController as follows:
partial class AViewController : UIViewController
{
public override void ViewWillAppear(bool animated)
{
NSUserDefaults.StandardUserDefaults.RegisterDefaults (appDefaults);
NSUserDefaults.StandardUserDefaults.Synchronize ();
var username = NSUserDefaults.StandardUserDefaults.StringForKey ("username");
Console.WriteLine (username);
}
}
以下链接中也有很好的例子:
There are also good examples in the following links:
https://github/xamarin/monotouch-样本/blob/master/AppPrefs/Settings.cshttp://forums.xamarin/discussion/9089/nsuserdefaults-standarduserdefaults-returns-null-first-time
这篇关于Xamarin 中的 UserDefault的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论