Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 是否在不相关(表)视图控制器之间交换数据?_Objective C_Data Binding_Delegates_Uitabbarcontroller_Uitableview - Fatal编程技术网

Objective c 是否在不相关(表)视图控制器之间交换数据?

Objective c 是否在不相关(表)视图控制器之间交换数据?,objective-c,data-binding,delegates,uitabbarcontroller,uitableview,Objective C,Data Binding,Delegates,Uitabbarcontroller,Uitableview,我有一个带有TabBar的应用程序,在主TabBar下有导航控制器,在这些控制器下有TableViewController的“分支” 如何在两个不同的选项卡图标下使两个不相关、非连续连接的ViewController在它们之间交换信息 尝试过委托,但我无法从被委托人处获取实例变量给委托人(它们之间没有关系,没有segue.destinationviewcontroller等) 有什么想法吗 实用: 该应用程序显示子项列表(父表视图中有这些项),在另一个选项卡上,将添加最近选定的项(但最大值为10

我有一个带有TabBar的应用程序,在主TabBar下有导航控制器,在这些控制器下有TableViewController的“分支”

如何在两个不同的选项卡图标下使两个不相关、非连续连接的ViewController在它们之间交换信息

尝试过委托,但我无法从被委托人处获取实例变量给委托人(它们之间没有关系,没有segue.destinationviewcontroller等)

有什么想法吗

实用: 该应用程序显示子项列表(父表视图中有这些项),在另一个选项卡上,将添加最近选定的项(但最大值为10,并按最近的项排序)。。在这件事上破坏了我的**


谢谢

您是否考虑过发布
NSNotification
,将要传输的数据传递到
userInfo

从:

发送(或发布)通知的对象不必知道 这些观察员是什么。因此,通知是一种强大的机制 为了在项目中实现协调和凝聚力。它降低了成本 程序中对象之间需要强依赖关系(如 依赖关系会降低这些对象的可重用性)

发送通知的类按如下方式执行:

[[NSNotificationCenter defaultCenter] postNotificationName: <notification-name> 
                                                    object: self
                                                  userInfo: <your-user-info>]; 

您可以使用@property和@synthesis创建默认的getter和setter。您可以手动创建自己的getter和setter。

我发现
NSNotification
这不是最好的方法,因为您将不相关的TableViewController耦合在一起。我认为应用程序中的信息流是至关重要的

我个人喜欢的解决方案是有一个中心类,用于管理应用程序的全局数据。这可以是
AppDelegate
类,也可以是任意的管理器类,用于管理数据流和“拉动字符串”,并在不同的独立ViewController之间进行调解

示例:
使用此代码,您可以获得
UITabBarController
,并将您的类设置为ViewController等的代理:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UITabBarController *rootTabBarController = (UITabBarController *)self.window.rootViewController;

    MyFirstTableViewController *firstVC = [rootTabBarController.viewControllers objectAtIndex:0];
   firstVC.delegate = self; // Get informed about events in the first ViewController

    MySecondTableViewController *secondVC = [rootTabBarController.viewControllers lastObject]; // Assuming you only have two Tabs

    // Once you have the rootTabBarController you can cast it to the corresponding ViewController and access any nested UIViewControllers

    return YES;
}

#pragma mark - MyFirstTableViewControllerDelegate
-(void)firstTableViewController:(MyFirstTableViewController *)sender didSomethingFancy:(MyFancyObject *)fancy{
    // Do stuff like fetching some data, based on the event from the first ViewController
    // Maybe tell the secondVC to refresh its data etc. etc.
}

我知道。我不明白这有什么用不。。。你能描述一下这个过程吗?使用NSNotification的正确方法是什么?但是我可以使用此方法将NSDictionary之类的对象作为数据传递吗?是的,您可以将任何NSObject子类作为参数传递(使用-postNotificationName:object:method的对象参数),因此NSDictionary或NSObject的任何其他(自定义)子类都可以正常工作。只需记住在receiver中将对象类型转换为正确的类。就个人而言,我想补充一点,就个人而言,我尽量避免使用NSNotificationCenter,虽然这可能意味着更改程序的逻辑流,但通常也可以使用其他“更干净”的解决方案。postNotificationName:方法的object:参数通常是发布通知的对象。userInfo:参数的类型是NSDictionary,因此可以满足您的需要,但是根据Wolfgang的评论,您可以根据需要对其进行类型转换(或者更好的方法是,将任何对象添加到字典中并作为userInfo:)这里的内容很棒!非常感谢!!我会调查的。只是想知道:一个非活动的不可见的ViewController可以充当一个通知观察者吗?是这样的,我应该在哪里(在控制器生命周期功能中)注册它?很好,唯一的问题是需要数据交换的一个ViewController不在根选项卡栏ViewController上,而是“隐藏”在导航控制器的下面(选项卡栏下)。另外,在另一个viewcontroller中“注册”delegate=self的最佳位置/功能在哪里?这将解决问题:尝试将“顶级照片”连接到“最近的照片”
- (void) someMethod: (NSNotification *) notification
{
    NSDictionary * myInfo = notification.userInfo;

    //do something with myInfo
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UITabBarController *rootTabBarController = (UITabBarController *)self.window.rootViewController;

    MyFirstTableViewController *firstVC = [rootTabBarController.viewControllers objectAtIndex:0];
   firstVC.delegate = self; // Get informed about events in the first ViewController

    MySecondTableViewController *secondVC = [rootTabBarController.viewControllers lastObject]; // Assuming you only have two Tabs

    // Once you have the rootTabBarController you can cast it to the corresponding ViewController and access any nested UIViewControllers

    return YES;
}

#pragma mark - MyFirstTableViewControllerDelegate
-(void)firstTableViewController:(MyFirstTableViewController *)sender didSomethingFancy:(MyFancyObject *)fancy{
    // Do stuff like fetching some data, based on the event from the first ViewController
    // Maybe tell the secondVC to refresh its data etc. etc.
}