如何通过Tab bar Controller传递数据viewcontroller

发布于 2024-12-31 21:46:30 字数 1038 浏览 0 评论 0原文

我想通过选项卡栏控制器将数据从视图控制器传递到视图控制器。

我的意思是,我的故事板是这样的。

View Controller → Tab Bar Controller → VC1/VC2/VC3

为此,我尝试使用 PrepareforSegue,但失败了。
代码在这里。

(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow];
friendsDetailViewController* vc = [[friendsDetailViewController alloc] init];
vc = (friendsDetailViewController*)[segue destinationViewController];
 
vc.candidateid = [[NSString alloc] init];
vc.candidateid = [candidateid objectAtIndex:selectedIndexPath.row];

虽然我试图将数据从 VC 传递到 VC1,但最后一行发生了 SIGABT。


更新:

我已经知道怎么做了。这是脚本:

friendsDetailViewController* vc = [[friendsDetailViewController alloc] init];
UITabBarController* tbc = [segue destinationViewController];
vc = (friendsDetailViewController*)[[tbc customizableViewControllers] objectAtIndex:0];

首先,我必须附加选项卡栏控制器。
其次,检索每个视图控制器的指针。
您可以使用 objectIndex 进行选择。

I want to pass datum from view controller to view controller via tab bar controller.

I mean, my storyboard is like this.

View Controller → Tab Bar Controller → VC1/VC2/VC3

For the purpose of this, I tried to use PrepareforSegue, but it's failed.
The code is here.

(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow];
friendsDetailViewController* vc = [[friendsDetailViewController alloc] init];
vc = (friendsDetailViewController*)[segue destinationViewController];
 
vc.candidateid = [[NSString alloc] init];
vc.candidateid = [candidateid objectAtIndex:selectedIndexPath.row];

Though I was trying to pass data from VC to VC1, SIGABT happened on the last line.


Update:

I've got figured out how. Here is the script:

friendsDetailViewController* vc = [[friendsDetailViewController alloc] init];
UITabBarController* tbc = [segue destinationViewController];
vc = (friendsDetailViewController*)[[tbc customizableViewControllers] objectAtIndex:0];

Firstly, I have to attach Tab Bar Controller.
Secondly, retrieve each View Controller's pointer.
You can select by using objectIndex.

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

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

发布评论

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

评论(2

北陌 2025-01-07 21:46:30

在选项卡栏控制器的子视图控制器中,您可以通过使用轻松传递数据

[self.parentViewController.view setAccessibilityValue:@"your data"];

,并且可以在选项卡栏的其他子视图控制器中轻松访问此数据

,或者您可以在 nsuserdefaults 中为某个键添加一个对象,并将该键附加到parentviewcontroller.view 中。
在下一个视图控制器中使用 nsuserdefaults 中的 objectforkey 获取该对象

In childviewcontrollers of tab bar controllers you can easily pass data by using

[self.parentViewController.view setAccessibilityValue:@"your data"];

and can easily access this data in other child viewcontrollers of tabbar

or you can add an object in nsuserdefaults for some key and attach that key in parentviewcontroller.view ..
get that object by using objectforkey from nsuserdefaults in next viewcontroller

软糖 2025-01-07 21:46:30

我有同样的问题。
我有 NSString,我希望像这样发送:

View Controller → Tab Bar Controller → VC1/VC2/VC3

在创建 ViewController-s之前,我创建了 ViewController-sTabBarController 类并将它们与故事板 连接起来。

这对我有用:

首先让我们从ViewController发送数据到TabBarController

  1. 添加segue来连接ViewControllerTabBarController故事板并命名:
    toMyTabController
  2. TabBarController中添加您想要更改的属性:

    @property (strong, nonatomic) NSString *name;

  3. 在 ViewController.m 中的某处添加此方法:

-(void)prepareForSegue:(UIStoryboardSegue *)segue 发件人:(id)发件人
{
if([segue.identifier isEqualToString:@"toMyTabController"])
{
MyTabController *tabController = (MyTabController *)segue.destinationViewController;
tabController.name = @"一些很棒的名字";
}
}
================================================

​=====================================
现在让我们将数据从 TabBarController 发送到 ViewController

  1. 向要发送信息的每个 ViewController 添加属性,并为其命名(很有创意)
    将其添加到 **ViewController1.h、ViewController2.h、ViewController3.h

    @property (strong, nonatomic) NSString *name;

  2. 将其添加到 ViewController1.mViewController2.mViewController3.m

    @synthesize name;

  3. 转到您的 TabBarController.m 并将其添加到 ViewDidLoad 方法中:

for (UIViewController* myController in self.viewControllers)
{
if([myController respondsToSelector:@selector(setName:)])
{
[myController PerformSelector:@selector(setName:) withObject:self.name];
}
}

这就是全部,
希望有帮助。祝你好运

I had same problem.
I had NSString which i waned to be sent like this:

View Controller → Tab Bar Controller → VC1/VC2/VC3

Before anything else I created ViewController-s and TabBarController classes and connected them with storyboard.

This worked for me:

First lets send data from ViewController to TabBarController:

  1. add segue to connect ViewController and TabBarController in storyboard and name it:
    toMyTabController
  2. add property u want to change in TabBarController:

    @property (strong, nonatomic) NSString *name;

  3. add this method somewhere in ViewController.m:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:@"toMyTabController"])
{
MyTabController *tabController = (MyTabController *)segue.destinationViewController;
tabController.name = @"SOME AWESOME NAME";
}
}

==================================================================================
Now lets send data from TabBarController to ViewController

  1. Add property to every ViewController where you want to send info, and lets name it name (so creative)
    add this to **ViewController1.h, ViewController2.h, ViewController3.h

    @property (strong, nonatomic) NSString *name;

  2. add this to ViewController1.m, ViewController2.m, ViewController3.m

    @synthesize name;

  3. go to your TabBarController.m and add this in ViewDidLoad method:

for (UIViewController* myController in self.viewControllers)
{
if([myController respondsToSelector:@selector(setName:)])
{
[myController performSelector:@selector(setName:) withObject:self.name];
}
}

Thats all,
Hope it helps. Good Luck

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文