有没有办法访问RootView控制器上的UILabel?我可以使用以下代码获取RootViewController:

var window = UIApplication.SharedApplication.KeyWindow;
var vc = window.RootViewController;
while (vc.PresentedViewController != null)
{
    vc = vc.PresentedViewController;
}

使用调试器并单步执行代码,我可以看到我有正确的视图控制器.在那个控制器上,我有一个UILabel,我想要为它设置文本,有什么方法可以这样做吗?我用C#编写代码,使用的是Visual Studio 2022.

**更新**

根据@Jason的建议,我将代码更改为:

var window = UIApplication.SharedApplication.KeyWindow;
MyViewController vc = (MyViewController)window.RootViewController.PresentedViewController;
vc.UpdateLabel(); 

我在UpdateLabel()方法中设置了一个断点,但它从未被调用过.不知道这里发生了什么,即使我得到了一个全新的MyViewController实例,我预计我仍然会达到临界点.

**更新**

为了让大家理解我想要做的事情,这里有更详细的解释和更多的代码.在我的视图控制器上,我有一个CollectionView,当用户从微调器中 Select 选项时,CollectionView会填充显示信息的标签.当用户 Select 或取消 Select CollectionView中的单元格时,我需要更新另一个标签中的数据,在CollectionViewSource中,我有以下代码来执行此操作:

public override void ItemSelected(UICollectionView collectionView, NSIndexPath indexPath)
    {
        MyCell thecell = collectionView.CellForItem(indexPath) as MyCell;
        if (Utils.DataCheck[indexPath.Row][1] == "1")
        {
            Utils.DataCheck[indexPath.Row][1] = "0";
        }
        else
        {
            Utils.DataCheck[indexPath.Row][1] = "1";
        }
        thecell.ToggleCheckBox();

        var window = UIApplication.SharedApplication.KeyWindow;
        MyViewController vc = (MyViewController )window.RootViewController.PresentedViewController;
        vc.UpdateLabel(); 
    }

Updatelabel()在静态类实用程序中对相同的静态List<string[]>进行操作.UpdateLabel()方法在执行时会做到这一点.当我初始化CollectionView时,我也调用UpdateLabel()方法,它执行得很好.只是不能在CollectionViewSource代码中执行.

推荐答案

再一次,解决方案被证明是相当简单的.在我的UICollectionViewSource中,我声明了一个变量private MyViewController MyParentController { get; set; }.当我初始化传入父视图控制器的UICollectionViewSource时,我的`PhoneNumberSource:code:

public class PhoneNumberSource : UICollectionViewSource
{
    public IList<string> PhoneNumbers;
    private MyViewController MyParentController { get; set; }

    public override nint NumberOfSections(UICollectionView collectionView)
    {
        return 1;
    }
    public override nint GetItemsCount(UICollectionView collectionView, nint section)
    {
        return PhoneNumbers.Count;
    }

    public PhoneNumberSource(MyViewController  pMyParentController, IList<string> pPhoneNumbers)
    {
        PhoneNumbers = pPhoneNumbers;
        MyParentController = pMyParentController;
    }
    public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
    {
        var phoneNumberCell = (PhoneNumberCell)collectionView.DequeueReusableCell(PhoneNumberCell.Key, indexPath);
        string phoneinfo = PhoneNumbers[indexPath.Row];
        phoneNumberCell.SetData(phoneinfo);
        return phoneNumberCell;
    }
    public override void ItemSelected(UICollectionView collectionView, NSIndexPath indexPath)
    {
        PhoneNumberCell thecell = collectionView.CellForItem(indexPath) as PhoneNumberCell;
        if (Utils.PhoneNumbersCheck[indexPath.Row][1] == "1")
        {
            Utils.PhoneNumbersCheck[indexPath.Row][1] = "0";
        }
        else
        {
            Utils.PhoneNumbersCheck[indexPath.Row][1] = "1";
        }
        thecell.ToggleCheckBox();

        MyParentController.Updatelabel();
    }
}

在视图控制器中,我按如下方式初始化CollectionViewSouce:

PhoneNumberList.Source = new PhoneNumberSource(this, Utils.PhoneNumbers);

标签每次都会更新.而且似乎我可以访问父视图控制器的所有方法和属性.

Csharp相关问答推荐

ASP.NET Core将SON重新序列化为派生类

如何从C#中有类.x和类.y的类列表中映射List(字符串x,字符串y)?

如何将ref T*重新解释为ref nint?

并行令牌更新

System.Text.Json数据化的C#类映射

Unity 2D自顶向下弓旋转

AsNoTrackingWithIdentitySolutions()似乎不起作用?

如何管理Azure认证客户端响应和证书 fingerprint

异步任务调用程序集

如何在.NET AOT中为所有枚举启用JsonStringEnumConverter

为值对象编写自定义JsonConverter

使用C#和.NET 7.0无法访问Cookie中的数据

如何避免在.NET中将日志(log)写入相对路径

在C#中,当输入一个方法/局部函数时,我的IEnumerator被重置/重新创建.为什么?

在同一个捕获中可以有多种类型的异常吗?

在ObservableCollection上使用[NotifyPropertyChangedFor()]源代码生成器不会更新UI

Azure队列触发器未使用隔离的工作进程执行

Xamarin中出错.表单:应用程序的分部声明不能指定不同的基类

.NET EF Core Automapper项目到筛选不起作用

外部应用&&的LINQ;左外部连接&类似于PostgreSQL的查询