我正在开发一个 iOS 应用程序Xamarin
,其中我有两个 底部viewControllers
相同UICollectionView
的应用程序,其中加载了一个带有快照的列表(类似于 1 行的画廊)。选择第一个 viewController 的快照后,我转到第二个 viewController,我希望在加载viewController时预先选择快照,但我正在接收System.NullReferenceException
,就在屏幕加载后一秒钟。任何想法为什么?
在第一个 viewController 中:
我将 indexPath 保存在帮助程序类中,并使用NotificationCenter
.
public void ItemSelected (UIKit.UICollectionView collectionView, Foundation.NSIndexPath indexPath)
{
UICollectionViewCell cell = collectionView.CellForItem (indexPath);
var newImage = (UIImageView)cell.ViewWithTag (100);
var selectedBgView = new UIView (cell.Bounds);
cell.SelectedBackgroundView = selectedBgView;
cell.SelectedBackgroundView.BackgroundColor = UIColor.Orange;
ApplicationState.Instance.TappedSnapshot = AppUtils.UIImageToBase64 (newImage.Image);
ApplicationState.Instance.SnapshotIndexPath = collectionView.IndexPathForCell (cell);
NSNotification notif = NSNotification.FromName ("imageTapped", this);
NSNotificationCenter.DefaultCenter.PostNotification (notif);
}
在第二个视图控制器中:
public override void ViewDidAppear (bool animated)
{
base.ViewDidAppear (animated);
CollectionView.SelectItem (ApplicationState.Instance.SnapshotIndexPath, true, UICollectionViewScrollPosition.Right);
this.ItemSelected (CollectionView, ApplicationState.Instance.SnapshotIndexPath);
}
public void ItemSelected (UIKit.UICollectionView collectionView, Foundation.NSIndexPath indexPath)
{
UICollectionViewCell cell = collectionView.CellForItem (indexPath);
var selectedBgView = new UIView (cell.Bounds);
cell.SelectedBackgroundView = selectedBgView;
cell.SelectedBackgroundView.BackgroundColor = UIColor.Orange;
}
编辑:当我不在 ViewDidAppear 中使用“ItemSelected”方法时,它不会崩溃!