2015-10-26 62 views
0

我使用的是[self.view viewWithTag]以引用UIViewController中的對象。然後我開始將一個特定的子視圖移到一個新的UIViewController中,然後將其添加爲一個子視圖控制器並將該視圖添加爲子視圖。但是,現在我無法在子視圖控制器內使用viewWithTag來訪問不同的對象。每次使用viewWithTag檢索到的對象都是零。viewWith標籤總是在子視圖控制器中返回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時,它返回爲零。同樣的事情發生在視圖控制器中的其他UI元素中,我也嘗試以這種方式檢索。

+0

一些代碼顯示了添加子視圖控制器的位置和方式,以及如何嘗試獲取所需的視圖將會很有用。 –

+0

當然,我會補充一點。 – Charles

+0

什麼是在這種情況下'self' UIButton * updateButton =(UIButton *)[self.view viewWithTag:5];'? –

回答

0

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

相關問題