【问题标题】:viewDidLoad: Checking if from a segue?viewDidLoad:检查是否来自 segue?
【发布时间】:2013-06-25 14:15:26
【问题描述】:

好的,基本上我有一个主视图控制器 - 我的应用程序的主菜单。我有一个按钮,可以将用户带到表格视图,在那里他们选择需要应用于主视图的内容。问题是,我不知道如何判断主视图控制器是从 segue 还是从应用程序开始创建的。有没有办法检查这个?我应该为 viewDidLoad 方法设置一个布尔值或字符串来检查,然后在 prepareForSegue 中修改它吗?

【问题讨论】:

  • 所以要验证,你想去你的mainViewControllertableController 然后回到你原来的mainViewController,对吗?如果是这种情况,您将 1. 不想创建新的 mainViewController 以返回它 2. 查看委托/协议。有大量关于那时的信息,这就是你想要的。
  • 是的,但整个事情都封装在一个导航控制器中。通过回到主视图。我怎么知道它是否正在制作一个新的,如果是的话,有没有办法“继续”回到主根视图并随身携带数据?我知道popToRootViewControllerAnimated: 可以到达堆栈的底部,但我不知道如何将数据带入它?

标签: ios objective-c delegates storyboard segue


【解决方案1】:

所以现在我可以更好地了解你是什么之后我可以给你一个更彻底的答案。您真正要寻找的是与委托和协议相关的模式。这是一种在 viewController 之间发送数据的方法,无需了解父(或委托)控制器的任何真实细节。这就是你想要做的。

为了清楚起见,我将为您的控制器使用两个名称,mainViewController 用于您的根控制器,tableViewController 用于您的 UITableViewController 子类的实例。

.h。你的tableViewController 你会想要设置一个协议:

@protocol SingleSelectionDelegate
- (void)selectionHasBeenChosenWithOption:(NSString *)selection;
@end

@interface MyTableViewControllerSubclass : UITableViewController
// properties and method declarations

// this is your property you will use to send data back to your delegate (in this case your mainViewController)
@property (weak, nonatomic) id<SingleSelectionDelegate> selectionDelegate;
@end

然后在mainViewController.m(或.h)中,您需要添加以下内容:

// this imports the interface and also the protocol that you want
#import "MyTableViewControllerSubclass.h"

// the <Single...> part says you conform to this protocol and implement the methods it requires (in this case selectionHasBeenChosenWithOption:)
@interface MainViewController <SingleSelectionDelegate>
@end

@implementation MainViewController

// here is where you need to implement the required method
- (void)selectionHasBeenChosenWithOption:(NSString *)selection {
    // do what you want with the selection, assign it to a property, call other methods, pass it to other delegates, or whatever else.

    // now that you have your information you want the focus to come back to you so you
    [self.navigationController popToViewController:self animated:YES]; // works even if not the root
}


// you also need to set yourself as tableViewController's selectionDelegate, if you are using Storyboards you will do this in the prepareForSegue.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.destinationViewController isKindOfClass:[MyTableViewControllerSubclass class]]) {
    // you could also check a specific segue's identifier but this makes sense because if it is this subclass then it will have a selectionDelegate property

    // assign yourself as the delegate
    MyTableViewControllerSubclass *destination = (MyTableViewControllerSubclass *)segue.destinationViewController
    destination.selectionDelegate = self;
}

@end

最后一步是在您获得 selectionDelegate 的信息时调用委托方法。这是在您的tableViewController.m 中完成的。在这种情况下,我会在tableView:didSelectRowAtIndexPath:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // getting the data you want to send back
    NSString dataToSendBack = [self.myArray[indexPath.row] description];

    // sending the data to your delegate (in this case mainViewController)
    [self.selectionDelegate selectionHasBeenChosenWithOption:dataToSendBack];

    // mainViewController is handling the popping so we do not need to do anything with the navigation stack
}

所以这基本上就是你所追求的。我在这里的文本编辑器中输入了几乎所有这些内容,因此可能存在一些语法错误。让我知道,我可以修复它们。这是 iOS 开发的一个重要概念,因为您最终会在所有地方使用它。学好它,你也可以寻找一些其他的教程。就像我说的,你会发现很多!我记得当我第一次学习它时,我自己感到不知所措,但现在它只是第二天性。

【讨论】:

  • 为了简单明了,我省略了这些信息,但实际上我的主视图中有 3 个相同子类的表视图。我将如何处理destination.selectionDelegate 是这样吗?这就是为什么我将 pop 带到 root 的原因,因为我必须从两个表钻取中跳回来。因此,prepareForSegue 方法有点傻,考虑到我不会继续使用我需要代理的表视图,直到之后的几个视图。
  • 也可能有一些我误解的东西,因为我将协议放在我的表视图控制器中,当我尝试将我的主视图控制器设置为其委托时,它说协议未定义..
  • @Rob 您只需将委托传递给每个 tableViewController (destination.selectionDelegate = self.selectionDelegate)。它应该仍然可以正常工作,如果您想继续使用情节提要,您仍然需要使用 prepareForSegue 来传递委托。确保你 #import TableViewSuclass 在你的 mainViewController 我也会更新 viewController 弹出来处理你的情况。
  • 啊,所以实际发生的事情是(也不知道为什么,但是)我让我的tableViewController.h 导入了我的mainViewController.h,反之亦然。显然循环导入会导致这个问题。
  • @Rob 是的,这将是一个问题。有办法解决这个问题,但 tableViewController 没有真正的理由导入 mainVC
【解决方案2】:

您也可以查看[UIApplication sharedApplication] delegate] window] rootViewController],但我可能只会选择属性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-06
    • 2014-05-25
    • 1970-01-01
    • 2023-02-14
    • 2019-09-18
    • 1970-01-01
    • 2014-04-19
    • 2023-03-11
    相关资源
    最近更新 更多