2013-07-09 82 views
0

我有一個tableview,當按下時會加載一個新的.xib。問題是,我需要將自定義對象傳遞給新的.xib。到目前爲止,我有:將自變量傳遞給自定義對象的新.xib

-(UITableViewCell *)doTestCellForTableView:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath { 

//some other code for current view 
TestObj *testobj = [[TestObj alloc]init]; 

if(shouldGo){ 
    cell.userInteractionEnabled = YES; 
    UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(loadTestView:)]; 
    tapped.numberOfTapsRequired = 1; 
    [cell addGestureRecognizer:tapped]; 
} 

而且在這個方法:

-(void)loadTestView:(TestObj *)withTestObj{ 

TestViewController *newView = 
[[TestViewController alloc] init]; 
//Set the property of type TestObj 
newView.testobjprop = withTestObj; 

[self presentViewController:newView animated:YES completion:nil]; 
} 

它拋出NSInvalidArgumentException。我怎樣才能讓選擇器發送「testobj」在tableview方法中初始化,以便我可以在那裏訪問?

如果我註釋掉「newView.testobjprop = withTestObj;」,新的.xib加載就好了。在loadTestView方法中。所以我認爲這個問題就在那裏,而且如何將一個對象從一個.xib傳遞到另一個。

+0

爲什麼你不在'didSelectRow'委託方法中做這件事?並且當你僅用alloc init呈現一個'viewcontroller'時,如果你在'storyboard'或'xib'文件中引用了'viewcontroller',並且對象的類型可能不相同,你會看到一個空白屏幕所以你應該在這裏發佈更多關於你的問題的信息,因爲可能會有很多事情出錯。 – soryngod

+0

@soryngod如果我在loadTestView方法中註釋掉「newView.testobjprop = withTestObj」,那麼新的.xib加載就好了,這就是爲什麼我沒有提到其他東西。這個問題真的歸結爲:「如何將對象從一個.xib傳遞給另一個.xib?」 –

+0

因此'textobjprop'與'withTestObj'是同一類型? – soryngod

回答

0

-(void)loadTestView:(TestObj *)withTestObj的參數是UIGestureRecognizer類型,所以這就是爲什麼你得到NSInvalidArgumentException

如果將其更改爲-(void)loadTestView:(UITapGestureRecognizer *)withTestObj這將是正常行爲。

這就是爲什麼您需要使用didSelectRow:AtIndexPath,因此您可以將正確的值傳遞給viewcontroller

在這裏,你根本沒有使用TestObj *testobj = [[TestObj alloc]init];

相關問題