我正在尝试构建iOS应用程序,其中视图控制器正在调用(推送)另一个视图控制器。在第二个视图控制器中,我有静态TableViewController
,包含1个部分和2个静态单元格。
每当我访问单元格的元素时,代码都会抛出异常。
按以下代码推送View Controller 2:
var destinationController = new EditAccountTableViewController(accounts[indexPath.Row]);
parentController.NavigationController.PushViewController(destinationController, true);`
EdditAccountTablewViewController类:
public partial class EditAccountTableViewController : UITableViewController
{
private Account account;
public EditAccountTableViewController()
{
var loc = NSBundle.MainBundle.LocalizedString("New", "");
var loc2 = NSBundle.MainBundle.LocalizedString("Account", "");
Title = $"{loc} {loc2}";
this.account = new Account();
AccountNameCell.DetailTextLabel.Text = account.Name;
AccountDescriptionCell.DetailTextLabel.Text = account.Description;
}
public EditAccountTableViewController(Account account)
{
var loc = NSBundle.MainBundle.LocalizedString("Account", "");
Title = $"{loc}";
this.account = account;
AccountNameCell.DetailTextLabel.Text = account.Name;
AccountDescriptionCell.DetailTextLabel.Text = account.Description;
}
partial void SaveAccountButton_Activated(UIBarButtonItem sender)
{
if (account.Name != string.Empty)
{
Database.Instance.DataAccess.SaveModelItem<Account>(account);
}
}
}
AccountNameCell
和AccountDescriptionCell
是单元格名称。单元格在故事板中创建:
我很清楚,该异常是由AccountNameCell
null
引起的。我不明白为什么它是null
,也不知道如何解决它。
感谢您的帮助。
答案 0 :(得分:0)
我使用segue而不是推送viewController来解决问题,我正在利用prepareForSegue方法。这是我正在使用的代码:
public override void PrepareForSegue(UIStoryboardSegue segue, NSObject sender)
{
base.PrepareForSegue(segue, sender);
if (segue.Identifier == "EditAccountSegue")
{
var secondViewController = segue.DestinationViewController as EditAccountTableViewController;
var selectedPath = TableView.IndexPathForSelectedRow;
var accountSource = TableView.Source as AccountsSettingSource;
var account = accountSource.getItem(selectedPath.Row);
secondViewController.Account = account;
}
else if (segue.Identifier == "AddAccountSegue")
{
var destinationController = segue.DestinationViewController as EditAccountTableViewController;
destinationController.Account = new Account();
}
}