问题
I want to user click table row then after it user will go to other page. The page has Storyboard ID "Other View" and Restoration ID "Other View". But I can't go to purposed view
This is my code :
- (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;
}
This code doesn't work :
UIViewController *otherViewCon;
otherViewCon = [self.storyboard instantiateViewControllerWithIdentifier:@"Other View"];
[self.navigationController pushViewController:otherViewCon animated:YES];
UPDATED I have insert new code. I use didSelectRowAtIndexPath method but it doesn't work :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIViewController *otherViewCon;
otherViewCon = [self.storyboard instantiateViewControllerWithIdentifier:@"Other View"];
[self.navigationController pushViewController:otherViewCon animated:YES];
}
回答1:
Try the method didSelectRowAtIndexPath
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:@"ShowDetail" sender:tableView];
}
If you want to pass a variable or perform an action before it segues, do the following:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"ShowDetail"]) {
//Do something
Detail *detailController = (Detail*)segue.destinationViewController;
}
}
If you want to know how to name a segue, here is a great tutorial from apple docs: Naming Segues
回答2:
Your code seems to be correct. just i feel problem in finding UIStoryboard
object .replace following things may this work fine.
- (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];
}
回答3:
You should write navigation code in
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
this method.
Or you can directly go to another view controller by drawing push segue in storyboard.No need to write code.
How to make a push segue when a UITableViewCell is selected
回答4:
-(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];
}
回答5:
Try this its help you. 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];
}
}
回答6:
in here showDataVC is a my new file name for second ViewController. in a first created a object of new file and initialize. and initialize showDataVC with object to use object in pushviewcontroller.
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
showDataVc *objshowDataVc = [self.storyboard instantiateViewControllerWithIdentifier:@"showDataVc"];
[self.navigationController pushViewController:objshowDataVc animated:YES];
}
回答7:
Implement your code in didSelectRowAtIndexPath
method. And please read the documentations before writing code n research before you put questions here.
来源:https://stackoverflow.com/questions/30860793/objective-c-click-table-row-and-go-to-another-page