Xcode 4.2 iOS 5:来自 UITableView 的多个 segue
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8809833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode 4.2 iOS 5 : Multiple segues from a UITableView
提问by user1141673
I'm starting now with Xcode on 4.2 for iOS5 and there are a few changes and I'm now crossing a problem that I can't figure out a way to solve it.
我现在开始使用 Xcode 4.2 for iOS5,有一些变化,我现在遇到了一个我无法找到解决方法的问题。
I'm doing an example with a UITablwView that is populated programmatically with 2 Sections, 1st section with only 1 Row, and 2nd Section with 3 Rows.
我正在做一个 UITablwView 的例子,它以编程方式填充有 2 个部分,第 1 部分只有 1 行,第 2 部分有 3 行。
My aim is to select a row from the table, and based on that row, the user will be redirected to different Views.
我的目标是从表中选择一行,并根据该行将用户重定向到不同的视图。
For example: selecting section 0 row 0, app pushes to view 1 - name setting // selecting section 1 row 0, app pushes to view 3 - address setting
例如:选择section 0第0行,app推送查看1-名称设置//选择section 1第0行,app推送查看3-地址设置
The old fashion way, this is quite simple, just needed to init a UIViewController with initWithNibName and then push the view.
老式的方式,这很简单,只需要用 initWithNibName 初始化一个 UIViewController 然后推送视图。
Now with the storyBoard everything changes, or at least I think it changes because I can't see how to get the same result since I can't set multiple segue's from the tableView to different UIViewControllers...and to do the old fashion way I can't see where I can get the NIB names from the views on the storyBoard to init an UIViewController to push.
现在有了 storyBoard,一切都变了,或者至少我认为它发生了变化,因为我无法看到如何获得相同的结果,因为我无法将 tableView 中的多个 segue 设置为不同的 UIViewControllers ......并按照旧时尚的方式进行我看不到在哪里可以从 storyBoard 的视图中获取 NIB 名称来初始化 UIViewController 以进行推送。
Does any one knows how to get to this result??
有谁知道如何得到这个结果?
回答by Marco
Define two "generic" segues (identified as "segue1" and "segue2", for example) in the storyboard from your source view controller, one to each destination view controller. These segues won't be associated with any action.
在源视图控制器的故事板中定义两个“通用”segue(例如,标识为“segue1”和“segue2”),一个到每个目标视图控制器。这些 segue 不会与任何操作相关联。
Then, conditionally perform the segues in your UITableViewDelegate
:
然后,有条件地在您的UITableViewDelegate
:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Conditionally perform segues, here is an example:
if (indexPath.row == 0)
{
[self performSegueWithIdentifier:@"segue1" sender:self];
}
else
{
[self performSegueWithIdentifier:@"segue2" sender:self];
}
}
回答by u617377
I have the same problem as you do. The problem is that you can't link your tableViewCellto multiple view controllers. However you can link your source view itselfto multiple view controllers.
我和你有同样的问题。问题是您无法将tableViewCell链接到多个视图控制器。但是,您可以将源视图本身链接到多个视图控制器。
Control-drag the master view controller(instead of table view cell) from the scene viewer to whatever view controller you want to link. You can do this as much as you want. Notice that the segue shown in source view controller scene should be something like "Push Segue from Root View Controller ..." instead of "Push Segue from NavCell to ...".
Identify each segue link a unique name like "toDetailView1"
Finally, custom the selection in your source view controllers:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row % 2 == 1) { [self performSegueWithIdentifier:@"toDetailView1" sender:self]; } else { [self performSegueWithIdentifier:@"toDetailView2" sender:self]; } }
按住 Control 键将主视图控制器(而不是表格视图单元格)从场景查看器拖动到您想要链接的任何视图控制器。您可以随心所欲地执行此操作。请注意,源视图控制器场景中显示的 segue 应该类似于“从根视图控制器推送 Segue ...”而不是“从 NavCell 推送 Segue 到 ...”。
标识每个 segue 链接的唯一名称,如“toDetailView1”
最后,在源视图控制器中自定义选择:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row % 2 == 1) { [self performSegueWithIdentifier:@"toDetailView1" sender:self]; } else { [self performSegueWithIdentifier:@"toDetailView2" sender:self]; } }
回答by Alex Cio
Like @陳仁乾 and @Marco explained was completely correct. To make everything a little bit easier I would recommend you to use a single NSArray
which will be initialized when viewDidLoad
. Just name the segues the same as your UIViewControllers
, this way you can Display a correct description of what UIViewControllers
you can choose from and you can also perform the segues
from this NSArray
:
就像@陈仁干和@Marco解释的完全正确。为了让一切变得更简单,我建议您使用一个NSArray
将在viewDidLoad
. 仅举的塞格斯一样的UIViewControllers
,这样就可以显示一个什么样的正确描述UIViewControllers
,你可以选择,你也可以执行segues
此NSArray
:
(Actually I'm not sure if it can cause any problems calling the segue the same as the UIViewController
you want to call. Please let me know if this is BadPractise)
(实际上,我不确定它是否会导致与UIViewController
您想要调用的相同的 segue 出现任何问题。请让我知道这是否是BadPractise)
viewDidLoad
查看已加载
- (void)viewDidLoad
{
[super viewDidLoad];
_arraySessions = [[NSArray alloc] initWithObjects:
@"MyViewControllerName", nil];
}
cellForRowAtIndexPath
cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell =
[tableView dequeueReusableCellWithIdentifier:@"overviewCell"
forIndexPath:indexPath];
[cell.textLabel setText:_arraySessions[indexPath.row]];
return cell;
}
didSelectRowAtIndexPath
didSelectRowAtIndexPath
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self performSegueWithIdentifier:_arraySessions[indexPath.row]
sender:self];
}