ios 更新可查看数据

hgtggwj0  于 12个月前  发布在  iOS
关注(0)|答案(4)|浏览(90)

我有三个ViewController:

RootViewController
FirstViewController
SecondViewController

字符串
在RootViewController中,我创建了一个TabBarController和另外两个ViewController。所以我需要做一些类似的事情:

FirstViewController *viewController1 = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];

 SecondViewController *viewController2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];


然后将控制器添加到TabBarController中。此时,我的两个ViewController已示例化。
为了更新数据,我在FirstViewController.m中尝试了以下操作:

SecondViewController *test = [[SecondViewController alloc] init];
 [test.tableView reloadData];


但是什么也没有发生,我想是因为我的SecondViewController之前已经被分配了,并且正在创建它的一个新示例。
如何从FirstViewController更新SecondViewController中的Table数据?

tez616oj

tez616oj1#

让根视图控制器在创建viewController2时将其值传递给viewController1。将属性描述为弱属性,因为您希望rootController拥有它们,而不是其他viewController。

qv7cva1a

qv7cva1a2#

如果你想更新第二个视图控制器中使用的数据而不需要切换,我会使用委托模式。
在FirstViewController中创建一个协议声明,如

@class FirstViewController;

@protocol FirstViewControllerDelegate

-(void) updateDataFromFirstViewController: (NSMutableArray*)newArray;

@end

@property (strong, nonatomic) id<FirstViewControllerDelegate>delegate;

字符串
然后在你的firstViewController中,当你有新的数据要更新时,

[self.delegate updateDataFromFirstViewController:yourNewData];


在SecondViewController.m中实现此方法,并将委托添加到SecondViewController. h

SecondViewController: UIViewController <FirstViewControllerDelegate>


然后在

-(void) viewWillAppear:(BOOL)animated


重新加载你的表数据,这样当你真正需要看到更新的数据时,当你切换的时候,它就在那里了。同样不要忘记在SecondViewController中将FirstViewController委托设置为self。

kognpnkq

kognpnkq3#

您的TabBarController是否包含FirstViewControllerSecondViewController?在TabBarController中,有一个属性-viewControllers。它是一个数组,因此您可以使用它来访问您的viewController。

[tabBarController.viewControllers objectAtIndex:0];//This is your First viewController
[tabBarController.viewControllers objectAtIndex:1];//This is your Second viewController

字符串
然后访问sencondViewController的tableView并重新加载它。
希望这是有帮助的。

dfuffjeb

dfuffjeb4#

您可以通过以下方式访问secondViewController:

UITabBarController *tabController = (UITabBarController *)[self parentViewController];

[tabController.viewControllers enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

    if ([obj isKindOfClass:[SecondViewController class]]) {
        SecondViewController *secondController = obj;
        [secondController.tableView reloadData];
       *stop = YES;

    }
    
}];

字符串

相关问题