2015-06-16 29 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]; 
} 
相关问题