在 Storyboard 中推送视图

发布于 2025-01-04 14:26:12 字数 1060 浏览 0 评论 0原文

我正在尝试使用表视图和导航控制器。如果我使用 NIB 文件,这是我将使用的代码。如果我使用 NIB 文件,这段代码可以正常工作,但我决定尝试故事板 -

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

if (_webViewController == nil) {
    self.webViewController = [[[WebViewController alloc] initWithNibName:@"WebViewController" bundle:[NSBundle mainBundle]] autorelease];
}
RSSEntry *entry = [_allEntries objectAtIndex:indexPath.row];
_webViewController.entry = entry;
[self.navigationController pushViewController:_webViewController animated:YES];
}

我在故事板中创建了另一个场景,并在那里设置了所有内容。当用户单击此表视图中的单元格时,他们将被带到该场景。这是我为代码编写的内容,但它不正确,我陷入了绝望的困境:( -

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"articleView"]){
    UINavigationController *navigationController = segue.destinationViewController;
    WebViewController *controller = (WebViewController *)navigationController.topViewController;
    controller.webView = self;
 }

答案 - 答案 2 和 3 都是正确的

I am trying to use Table Views and navigation controllers. This is the code I would use if I was using NIB files. This piece of code would work fine if I was using NIB files, but I decided to try storyboard -

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

if (_webViewController == nil) {
    self.webViewController = [[[WebViewController alloc] initWithNibName:@"WebViewController" bundle:[NSBundle mainBundle]] autorelease];
}
RSSEntry *entry = [_allEntries objectAtIndex:indexPath.row];
_webViewController.entry = entry;
[self.navigationController pushViewController:_webViewController animated:YES];
}

I have created another scene in storyboard and have everything set up out there. When a user clicks a cell in this Table View they will be taken to that scene. This is what I wrote for the code but it is not right and I'm hopelessly stuck :( -

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"articleView"]){
    UINavigationController *navigationController = segue.destinationViewController;
    WebViewController *controller = (WebViewController *)navigationController.topViewController;
    controller.webView = self;
 }

Answer - Answers 2 and 3 are both right

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清旖 2025-01-11 14:26:12

两天前我遇到了一个非常类似的问题,用户连续选择披露附件并想要调用 segue。对我有用的:

  1. 在故事板中,创建从整个 tableView 控制器(而不是单元格)到下一个 viewController 的 segue“articleView”。
  2. 将以下方法添加到您的 tableViewController 中:
- (void)tableView:(UITableView *)tableViewaccessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {  
    [self PerformSegueWithIdentifier:@"articleView" 发件人:[self.tableView cellForRowAtIndexPath:indexPath]];
}

为了你,我还用 tableView:didSelectRowAtIndexPath: 进行了测试,它有效。

享受吧,

达米安

I had a very similar problem 2 days ago, with the user selecting the disclosure accessory on a row and wanting to invoke a segue. What worked for me:

  1. In storyboard, create segue "articleView" from the entire tableView controller (not the cell) to the next viewController.
  2. Add the following method to your tableViewController:
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {  
    [self performSegueWithIdentifier:@"articleView" sender:[self.tableView cellForRowAtIndexPath:indexPath]];
}

Just for you I also tested with tableView:didSelectRowAtIndexPath: and it works.

Enjoy,

Damien

那支青花 2025-01-11 14:26:12

如果您的表格视图已经嵌入到导航控制器中(从您的第一个代码片段来看就是这样),那么您的 segue 的目标视图控制器将是 WebViewController,而不是导航控制器。

因此,您可以让prepareForSegue方法直接传递项目:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"articleView"])
    {
        WebViewController *controller = (WebViewController *)segue.destinationViewController;
        RSSEntry *entry = [_allEntries objectAtIndex:[self.tableView indexPathForSelectedRow].row];
        controller.entry = entry;
    }
}

If your table view is already embedded in a navigation controller (looks that way, from your first code snippet) then the destination view controller of your segue will be a WebViewController, not a navigation controller.

You can therefore have your prepareForSegue method pass the item directly across :

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"articleView"])
    {
        WebViewController *controller = (WebViewController *)segue.destinationViewController;
        RSSEntry *entry = [_allEntries objectAtIndex:[self.tableView indexPathForSelectedRow].row];
        controller.entry = entry;
    }
}
溺ぐ爱和你が 2025-01-11 14:26:12

您可能会发现这个优秀的两部分教程很有帮助

http:// /www.raywenderlich.com/5138/beginning-storyboards-in-ios-5-part-1

You may find this excellent two-part tutorial helpful

http://www.raywenderlich.com/5138/beginning-storyboards-in-ios-5-part-1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文