2015-06-16 28 views
3

我想用戶點擊表格行然後用戶將轉到其他頁面。該頁面具有故事板ID「其他視圖」和恢復ID「其他視圖」。 但我不能去旨意視圖客觀c - 點擊表格行並轉到另一個頁面

這是我的代碼:

- (UITableViewCell *)tableView:(UITableView *)tabel cellForRowAtIndexPath:(NSIndexPath *)indeksBaris 
{ 

    static NSString *simpleTableIdentifier = @"TampilanCell"; 
    TabelBeritaCell *cell = (TabelBeritaCell *)[tabel dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 

    NSLog(@"nilai : %d", indeksBaris.row); 
    //detecting NIB of table view 
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TableView" owner:self options:nil]; 
    cell = [nib objectAtIndex:0]; 

    //Go To View Controller which have identifier "Other View" 
    UIViewController *otherViewCon; 
    otherViewCon = [self.storyboard instantiateViewControllerWithIdentifier:@"Other View"]; 
    [self.navigationController pushViewController:otherViewCon animated:YES]; 

    cell.judulBerita.text = [listBerita objectAtIndex:indeksBaris.row]; 

    return cell; 
} 

此代碼不起作用:

UIViewController *otherViewCon; 
otherViewCon = [self.storyboard instantiateViewControllerWithIdentifier:@"Other View"]; 
[self.navigationController pushViewController:otherViewCon animated:YES]; 

修訂 我插入新的代碼。我使用didSelectRowAtIndexPath方法方法,但它不工作:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
     UIViewController *otherViewCon; 
     otherViewCon = [self.storyboard instantiateViewControllerWithIdentifier:@"Other View"]; 
     [self.navigationController pushViewController:otherViewCon animated:YES]; 
    } 
+0

顯示didSelectRowAtInedxPath方法的代碼 –

+0

不,我沒有使用該方法。我會先嚐試... –

+1

爲什麼你不嘗試我在我的答案中寫的:'[self performSegueWithIdentifier:@「ShowDetail」sender:tableView];'你不需要創建一個新的UIViewController – Jessica

回答

4

嘗試的方法didSelectRowAtIndexPath

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [self performSegueWithIdentifier:@"ShowDetail" sender:tableView]; 
} 

如果你想傳遞一個變量或者塞格斯之前執行的操作,請執行以下操作:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([segue.identifier isEqualToString:@"ShowDetail"]) { 
     //Do something 
     Detail *detailController = (Detail*)segue.destinationViewController; 
    } 
} 

如果你想知道如何命名SEGUE,這裏是蘋果的文檔有很大的教程:Naming Segues

+0

OK,我會嘗試先 –

+2

下,選民能請解釋一下? – Jessica

+0

此代碼有效。但是我有錯誤「‘NSInvalidArgumentException’的,原因是:「接收器(<視圖控制器:0x79442370>)具有標識符沒有賽格瑞‘其他查看’」 事實上,我已創建與命名視圖故事板ID和恢復ID「其他查看' –

0

didSelectRowAtIndexPath方法中實現您的代碼。在你寫問題之前,請在編寫代碼n的研究之前閱讀文檔。

0

你應該

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 

這種方法編寫代碼導航。

或者您可以通過在storyboard中繪製push segue直接轉到另一個視圖控制器。無需編寫代碼。

How to make a push segue when a UITableViewCell is selected

+0

繪圖從表格視圖推segue?我做不到!如果元素是按鈕,我只能繪製。 –

+0

如果您在選擇任何行後進入相同的頁面,則可以從表格中繪製推式賽格。 –

+0

檢查答案點擊鏈接希望它有幫助 –

1

您的代碼似乎是正確的。只是我覺得UIStoryboard對象.replace下面的東西可能會工作正常的問題。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
     UIViewController *otherViewCon; 
     UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"your_storyboard_name" bundle:nil];// like Main.storyboard for used "Main" 
     otherViewCon = [storyboard instantiateViewControllerWithIdentifier:@"Other View"]; 
     [self.navigationController pushViewController:otherViewCon animated:YES]; 
} 
+0

我不認爲@andika_kurniawan想要創建一個新的viewController prgrammatically。我認爲問題只是在繼續。 – Jessica

+0

@Jessica,我不創建新的viewController。我只是以編程方式獲取已存在的viewController引用。 –

+0

糟糕。你是對的!!但你爲什麼要創造一個新的故事板? – Jessica

0
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 

{ 
     NSString * storyboardIdentifier = @"storyboardName";// for example "Main.storyboard" then you have to take only "Main" 
     UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardIdentifier bundle: [NSBundle mainBundle]]; 
     UIViewController * UIVC = [storyboard instantiateViewControllerWithIdentifier:@"secondVCStoryboardID"]; 
    [self presentViewController:UIVC animated:YES completion:nil]; 
} 
0

試試這個它的幫助你。 didSelectRowAtIndexPath

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if(indexpath.row==0)//means you click row 0 
    { 
    UIViewController *otherView=[self.storyboard instantiateViewControllerWithIdentifier:@"otherview"]; 
     [self presentViewController:otherView animated:YES completion:nil]; 
    } 
    else 
    { 
    UIViewController *detailView=[self.storyboard instantiateViewControllerWithIdentifier:@"detailView"]; 
     [self presentViewController:detailView animated:YES completion:nil]; 
    } 
} 
0

在這裏showDataVC是第二視圖控制器我的一個新的文件名。 首先創建一個新文件的對象並初始化。 並用pushviewcontroller中的對象初始化showDataVC以使用對象。

(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    showDataVc *objshowDataVc = [self.storyboard instantiateViewControllerWithIdentifier:@"showDataVc"]; 

    [self.navigationController pushViewController:objshowDataVc animated:YES]; 
} 
相關問題