从以编程方式创建的表视图

时间:2015-07-03 18:28:30

标签: ios swift segue programmatically-created

我是iOS开发的新手,在完成了一些基础教程和初学者级别的书籍后,我看了一下iOS编程的例子。之后我发现了这个。

https://github.com/uacaps/PageMenu

我下载了示例,但我遇到了问题。我正在使用PageMenuDemoTabbar(https://github.com/uacaps/PageMenu/tree/master/Demos/Demo%204/PageMenuDemoTabbar) 例。我想在用户点击tableviewcell时添加segues。但这是问题所在。因为以编程方式创建了tableviews,所以我无法从storyboard中添加segues。

我怎么能做到这一点?

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

如果你想从Storyboard转到另一个视图控制器,只需将StoryboardID添加到这个视图控制器(就像我有带有StoryboardID RootVC 的RootViewController):

enter image description here

然后为要传递给视图控制器的数据创建公共属性(即@property (nonatomic, strong) NSDictionary *propertyYouSet)。 然后在-tableView:didSelectItemAtIndexPath: UITableView委托方法中添加以下代码:

YourViewController *yourViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"YourViewControllerStoryboardID"];
yourViewController.propertyYouSet = @{ 'someKey': 'someValueYouWantToPass' };

[self.navigationController pushViewController:yourViewController animated:YES];

这样您仍然可以使用Storyboard视图控制器而无需segue。

Swift版本

糟糕,刚刚注意到了swift标记。所以这里是Swift中的相同代码:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let vc = storyboard.instantiateViewControllerWithIdentifier("yourViewControllerStoryboardID") as! UIViewController
    vc.setYourProperty(someDictionaryHere) // I'm not so sure about this, I'm new in Swift too :)
    self.presentViewController(vc, animated: true, completion: nil)
}

然后在视图控制器中覆盖此方法:

init(coder aDecoder: NSCoder!)
{
    super.init(coder: aDecoder)
}

(因为在Swift -initWithCoder:中是必需的:))

相关问题