Dar*_*ski 39 storyboard uitableview ios xcode4.2
我正在使用Xcode 4.2为iOS 5开发一些项目.我有一个UITableViewController,并希望在用户点击表格单元格时执行segue,但目标视图控制器依赖于对该单元格执行的操作.例如,当用户点击单元格时我想加载SomeViewController,但是当用户在编辑模式下点击同一单元格时,我想加载AnotherViewController.不幸的是,没有办法在Xcode 4.2 storyboard builder中的同一个单元格上配置多个segue,或者我只是没有得到它.也许有一种方法可以在代码编辑器中手动创建segue.通常我想要实现的是为用户提供一种方法,使用一个视图控制器"输入"由单元格表示的项目,并使用另一个视图控制器"编辑"由同一单元格表示的项目.在表编辑模式下切换到第二个视图控制器(编辑器)只是我的第一个,但也许有更好的方法.任何帮助将不胜感激.
小智 54
在查看Storyboard时,从源视图控制器控制拖动到目标视图控制器.这将创建一个segue,您可以直接从源视图控制器触发.确保你给Segue一个名字.此名称将传递到源View Controller的performSegue:withIdentifier:方法.
Rhu*_*arb 33
我正在添加一个额外的答案只是因为在阅读上面选定的一个以外的许多地方 - 这是正确的答案 - 我发现自己想知道如何检测通常会触发segue的触摸.你看,如果你通过从表格视图单元格拖动到下一个控制器来按常规方式创建你的segue,那么你会自动完成两件事.
但当然,你不能阻止segue.
现在,如果你想有条件地 segue,那么(如在其他答案中所提到的)你删除了segue并从UITableViewController创建一个新的(从对象导航器而不是故事板中拖动它)到下一个控制器 - 并给它一个名称.
然后 - 这是我缺少的部分 - tableView:didSelectRowAtIndexPath在表视图控制器中实现以编程方式和条件方式执行segue,如下所示.
请注意,您还需要以某种方式识别您的单元格,以便您知道是否已选择了您感兴趣的单元格.你可以通过知道静态表中的索引路径来做到这一点,但我更喜欢在IB中设置我的单元格唯一标识符(即使我不需要它出列,因为它是一个静态表)并检查它.这样,如果我在静态表中向上或向下移动我的单元格,我将不需要更改此代码.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Find the selected cell in the usual way
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
// Check if this is the cell I want to segue from by using the reuseIdenifier
// which I set in the "Identifier" field in Interface Builder
if ([cell.reuseIdentifier isEqualToString:@"CellIWantToSegueFrom"]) {
// Do my conditional logic - this was the whole point of changing the segue
if (myConditionForSegueIsSatisfied) {
// Perform the segue using the identifier I was careful to give it in IB
// Note I'm sending the cell as the sender because that's what the normal
// segue does and I already had code counting on that
[self performSegueWithIdentifier:@"SegueIdentifer" sender:cell];
}
}
Run Code Online (Sandbox Code Playgroud)
注意我是如何使用segue发送单元格的 - 来自单元格的正常segue就是这样做的,我最初传递的是nil,而我依赖它的代码停止了工作.
这是一个老问题和一些很好的正确答案,但我会发布一些我觉得有用的其他信息,当我遇到这个:
如果您通过ctl +从SomeVC拖动到AnotherVC在故事板中创建segue,请将其删除.
在对象导航器中,ctl +在ViewController级别(通常在VC名称下的层次结构中)从SomeVC拖动到AnotherVC.为segue提供唯一标识符.
tableView: didSelectRowAtIndexPath:如上所述实施.
我发现使用switch语句很有帮助tableView: didSelectRowAtIndexPath:
switch (indexPath.row) {
case 0:
[self performSegueWithIdentifier:@"yourIDHere" sender:self];
break;
case 1:
// enter conditional code here...
break;
case 2:
// enter conditional code here...
break;
// ... for however many cells you require
default:
break;
Run Code Online (Sandbox Code Playgroud)
}
感谢大家的精彩回答.
| 归档时间: |
|
| 查看次数: |
34898 次 |
| 最近记录: |