簡體   English   中英

viewWithTag始終在子視圖控制器中返回nil

[英]viewWithTag always returns nil in child view controller

我正在使用[self.view viewWithTag]來引用UIViewController中的對象。 然后,我繼續將特定的子視圖移至新的UIViewController,然后將其添加為子視圖控制器,然后將視圖添加為子視圖。 但是,現在我無法在子視圖控制器中使用viewWithTag來訪問不同的對象。 每次,使用viewWithTag檢索的對象viewWithTag nil。

我不認為我使用的ViewWithTag不正確,因為在將視圖移到新的視圖控制器之前,它運行良好。 一種解決方案是使用viewWithTag我所引用的每個視圖創建屬性,但是我認為這不是一個好方法。 有更好的解決方案嗎? 為什么會這樣呢?

根據要求編輯:

這是我添加子視圖控制器的方法:

self.runeTable = [RuneTableViewController new];
[self addChildViewController:self.runeTable];
self.runeTable.view.frame = CGRectMake(screenshotWidth + 32, navBarHeight, self.view.frame.size.width-screenshotWidth-32.0, self.view.frame.size.height-navBarHeight);
[self.view addSubview:self.runeTable.view];

這是創建按鈕的代碼:

UIButton *updateButton = [UIButton buttonWithType:UIButtonTypeCustom];
updateButton.tag = 5;
[updateButton setTitleColor:HIGHLIGHT_COLOR forState:UIControlStateNormal];
[updateButton.titleLabel setFont:FONT_MED_LARGE_BOLD];
[updateButton setTitle:@"Update" forState:UIControlStateNormal];
updateButton.frame = CGRectMake(283, 280-60, 100, 60);
[detailInnerView addSubview:updateButton];

在這里,通過其他方法,我試圖檢索目標並將其添加到該按鈕:

UIButton *updateButton = (UIButton *)[self.view viewWithTag:5];
[updateButton addTarget:self action:@selector(updateCurrentDictionaryWithRuneInfo) forControlEvents:UIControlEventTouchUpInside];

當我添加斷點和po updateButton ,它返回nil。 在視圖控制器中的其他UI元素也發生了同樣的事情,我也嘗試以此方式進行檢索。

我想到了。 updateButton添加到另一個UIView,該UIView添加到父視圖控制器。 因此,為了檢索updateButton,我應該使用[self.parentViewController.view viewwithTag:5]而不是[self.view viewWithTag:5]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM