ios - viewDidLoad : Checking if from a segue?

标签 ios objective-c delegates storyboard segue

好吧,基本上我有一个主视图 Controller - 我的应用程序的主菜单。我有一个按钮,可将用户带到 TableView ,他们可以在其中选择需要应用于主视图的内容。问题是,我不知道有什么方法可以判断主视图 Controller 是从 segue 创建还是从应用程序启动创建。有办法检查吗?我应该为 viewDidLoad 方法设置一个 bool 值或字符串来检查,然后在prepareForSegue中修改它吗?

最佳答案

现在我更好地了解了您的需求,我可以给您更彻底的答案。您真正需要的是与委托(delegate)和协议(protocol)相关的模式。这是一种在 viewController 之间发送数据的方法,无需了解有关父(或委托(delegate)) Controller 的任何实际细节。这就是您想要做的。

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

.h中。您需要为您的 tableViewController 设置一个协议(protocol):

@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

最后一步是当您有选择委托(delegate)的信息时调用委托(delegate)方法。这是在 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 开发中一个被大量教授的概念,因为你最终会在任何地方使用它。好好学习它,你也可以寻找一些其他的教程。就像我说的,你会发现很多!我记得当我第一次学习它时,我感到不知所措,但现在它只是第二天性。

关于ios - viewDidLoad : Checking if from a segue?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17353984/

相关文章:

swift - 从 tableView 中删除 StructItem

ios - 我该如何修复 "Data(contentsOf: url) crashing app"?

ios - UITableViewCell 中的 ScrollView 不会保存位置

ios - UIPrintInteractionController 和 UIAppearance 的 navigationBar

ios - Cocoapod 库问题 : DTCoreText

swift - TableView 单元格中的委托(delegate)错误

ios - 如何在用户单击 ios 中的 "more "按钮时展开 UITextview

ios - 发布到 "me/myapp:myaction"会导致我的开放图故事仅显示在我的 'recent activity' 部分

iOS、MPNowPlayingInfoCenter 暂停/播放按钮不起作用?

mvvm - 为什么 ViewModel 不在 MVVM 中实现 ICommand