使用预构建的 UITabBarController 将数据发送到另一个视图?

发布于 2024-10-06 04:51:45 字数 721 浏览 3 评论 0原文

我使用 Xcode 创建了一个选项卡栏应用程序作为两个视图。

第二个视图是 UITableViewController。

我正在努力做的是当按下第二个选项卡时将数据发送到该视图。我已将选项卡栏委托给我的 AppDelegate 类并实现了此功能:

-(void)tabBarController:(UITabBarController*)tabBarController didSelectViewController:(UIViewController*)viewController
{
    // Override point for customization after application launch.
    statisticsViewController* assignmentListcont = [statisticsViewController alloc];

    NSManagedObjectContext* context = [self managedObjectContext];

    assignmentListcont.managedObjectContext = context;

    [assignmentListcont release];

}

第二个视图显示正常,但数据尚未传递。我想这是因为我还没有编写第二个视图转换,但如果我已经有一个 .xib 文件为我做这件事,我不确定如何做到这一点?有没有什么方法可以毫无问题地传递数据,甚至在视图内部检索数据?

I have created a Tab Bar Application using Xcode that as two views.

The secound view is a UITableViewController.

What I am struggling to do is send data to this view, when the second tab is pressed. I have delegated the Tab Bar to my AppDelegate class and implemented this function:

-(void)tabBarController:(UITabBarController*)tabBarController didSelectViewController:(UIViewController*)viewController
{
    // Override point for customization after application launch.
    statisticsViewController* assignmentListcont = [statisticsViewController alloc];

    NSManagedObjectContext* context = [self managedObjectContext];

    assignmentListcont.managedObjectContext = context;

    [assignmentListcont release];

}

The second view is displaying fine but the data hasn't been passed. I imagine its because I haven't programmed the second views transition but I'm unsure of how to do this if I already have a .xib file doing it for me? Is there some way to just pass the data without problems or even retrieve the data once inside the view?

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

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

发布评论

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

评论(2

°如果伤别离去 2024-10-13 04:51:45

您可以使用通知。

在您想要接收数据的视图中,将其放入 viewDidLoad 中:

[[NSNotificationCenter defaultCenter] addObserver:self 
                                      selector:@selector(yourSelector:) 
                                      name:@"addedData" 
                                      object:nil];

然后实现接收该数据的方法:

- (void)yourSelector:(NSNotification *)notification {
         Foo *foo;
     foo = [notification object];
    //do something else
}

现在,在数据源自的类中,您发布一条通知,通知新数据被创建。此外,您还可以传递希望其他方法接收的数据。

[[NSNotificationCenter defaultCenter] postNotificationName:@"addedData" 
                                      object:foo];

You could use notifications.

In the view that you want to receive the data, put this in viewDidLoad:

[[NSNotificationCenter defaultCenter] addObserver:self 
                                      selector:@selector(yourSelector:) 
                                      name:@"addedData" 
                                      object:nil];

Then implement the method that receives that data:

- (void)yourSelector:(NSNotification *)notification {
         Foo *foo;
     foo = [notification object];
    //do something else
}

Now in the class where the data is originated from, you post a notification that new data was created. Also, you pass along the data that you want to have the other method receive.

[[NSNotificationCenter defaultCenter] postNotificationName:@"addedData" 
                                      object:foo];
迷离° 2024-10-13 04:51:45

我这样做是为了将值从一个视图控制器发送到另一个视图控制器,希望它有帮助

UITabBarViewController *var = [self.storyboard   instantiateViewControllerWithIdentifier:@"name"]; //I select the UITabBarController
otherViewController *var2 = [var.childViewControllers objectAtIndex:0]; //I Select the first ViewController from that UITabBarController
var2.variable = @"value";
[self.navigationController pushViewController:var animated:YES];

I've done this to send a value from one view controller to another, hope it helps

UITabBarViewController *var = [self.storyboard   instantiateViewControllerWithIdentifier:@"name"]; //I select the UITabBarController
otherViewController *var2 = [var.childViewControllers objectAtIndex:0]; //I Select the first ViewController from that UITabBarController
var2.variable = @"value";
[self.navigationController pushViewController:var animated:YES];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文