我是IOS Objective C Programming的新手。我正在尝试一个非常简单的应用程序我的主页是标准的View Controller,我有一个简单的UITableView。我想要做的是当用户在表视图上选择不同的单元格时,使用不同的视图控制器。目前我填充了3条简单的线条。我搜索了许多历史讨论,但似乎无法工作(从segue选项到简单的实例化视图选项)。我简要介绍一下我的代码,希望有人能指出我在这里做错了什么。
答案 0 :(得分:0)
按照您想要从tableview引用视图控制器的顺序创建一个包含segue名称的数组,然后在
中- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
你可以打电话
[self performSegueWithIdentifier:[array valueAtIndex:indexPath.row sender:nil];
答案 1 :(得分:0)
如果是通过XIB:
Your_ViewController *vc = [[Your_ViewController alloc] initWithNibName:@"Your_ViewController" bundle:nil];
[self.navigationController pushViewController:vc animated:YES];
如果是通过StoryBoard:
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main_storyboard" bundle:[NSBundle mainBundle]];
Your_ViewController *vc = [storyBoard instantiateViewControllerWithIdentifier:@"storyboardID"];
[[self navigationController] pushViewController:vc animated:YES];
如果是通过编码:
Your_ViewController *vc = [[Your_ViewController alloc] initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:vc animated:YES];
对于你的问题:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Put above code here according to your requirement....
}
答案 2 :(得分:0)
试试这个
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
firstViewController *u = [[ContactDetailsVC alloc]init];
u= [self.storyboard instantiateViewControllerWithIdentifier:@"firstView"];
[self.navigationController pushViewController:u animated:YES];
}
答案 3 :(得分:0)
<强>故事板强>
确保您使用NavigationController
或Not
进行嵌入,如果不是,
只需按以下方式执行
XCode menu
- &gt; Editor
- &gt; EmbedIn
- &gt; Navigation Controller
,喜欢
你得到了
的输出<强>步骤-2 强>
在这里,我们可以使用两种类型导航
通过连接
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *SegName = @"";
if (indexPath.row == 0)
{
SegName = @"firstSegumeName";
}
else if (indexPath.row == 1)
{
SegName = @"secondSegumeName";
}
else if (indexPath.row == 2)
{
SegName = @"thirdSegumeName";
}
[self performSegueWithIdentifier:SegName sender:self];
}
少连接
if use `storyboard ID` with connection less
例如
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIViewController *otherViewCon;
if (indexPath.row == 0)
{
otherViewCon = [self.storyboard instantiateViewControllerWithIdentifier:@"yourfirstIdentifierName"];
}
else if (indexPath.row == 1)
{
otherViewCon = [self.storyboard instantiateViewControllerWithIdentifier:@"yoursecondIdentifierName"];
}
else if (indexPath.row == 2)
{
otherViewCon = [self.storyboard instantiateViewControllerWithIdentifier:@"yourthirdIdentifierName"];
}
[self.navigationController pushViewController:otherViewCon animated:YES];
}
答案 4 :(得分:-1)
将didSelectRowAtIndexPath
的最后2行替换为:
UINavigationController *navC = [[UINavigationController alloc] initWithRootViewController:myView];
[self presentViewController:navC animated:YES completion:nil];