如何通过Tab bar Controller传递数据viewcontroller
我想通过选项卡栏控制器将数据从视图控制器传递到视图控制器。
我的意思是,我的故事板是这样的。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在选项卡栏控制器的子视图控制器中,您可以通过使用轻松传递数据
,并且可以在选项卡栏的其他子视图控制器中轻松访问此数据
,或者您可以在 nsuserdefaults 中为某个键添加一个对象,并将该键附加到parentviewcontroller.view 中。
在下一个视图控制器中使用 nsuserdefaults 中的 objectforkey 获取该对象
In childviewcontrollers of tab bar controllers you can easily pass data by using
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
我有同样的问题。
我有 NSString,我希望像这样发送:
View Controller → Tab Bar Controller → VC1/VC2/VC3
在创建 ViewController-s 和 之前,我创建了 ViewController-s 和 TabBarController 类并将它们与故事板 连接起来。
这对我有用:
首先让我们从ViewController发送数据到TabBarController:
toMyTabController
在TabBarController中添加您想要更改的属性:
@property (strong, nonatomic) NSString *name;
在 ViewController.m 中的某处添加此方法:
-(void)prepareForSegue:(UIStoryboardSegue *)segue 发件人:(id)发件人
{
if([segue.identifier isEqualToString:@"toMyTabController"])
{
MyTabController *tabController = (MyTabController *)segue.destinationViewController;
tabController.name = @"一些很棒的名字";
}
}
================================================
=====================================
现在让我们将数据从 TabBarController 发送到 ViewController
向要发送信息的每个 ViewController 添加属性,并为其命名(很有创意)
将其添加到 **ViewController1.h、ViewController2.h、ViewController3.h
@property (strong, nonatomic) NSString *name;
将其添加到 ViewController1.m 、ViewController2.m、ViewController3.m
@synthesize name;
转到您的 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:
toMyTabController
add property u want to change in TabBarController:
@property (strong, nonatomic) NSString *name;
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
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;
add this to ViewController1.m, ViewController2.m, ViewController3.m
@synthesize name;
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