简体   繁体   English

在UITabBarController / UIViewControllers之间移动值

[英]Move a value between UITabBarController /UIViewControllers

I have a project i'm working on which involves 3 tabs in a UITabBarController (all done in a storyboard). 我正在处理一个项目,其中涉及UITabBarController 3个选项卡(全部在情节UITabBarController完成)。

Each tab is running off a different view controller. 每个选项卡都在不同的视图控制器上运行。

I have a button on tab 1 that performs a calculation and returns a result in a text box. 我在选项卡1上有一个按钮,可以执行计算并在文本框中返回结果。 I want it so that when I hit calculate, the result is also returned in a text box in tab 2. 我想要它,以便当我单击“计算”时,结果也会在选项卡2的文本框中返回。

I'm not really sure how to pass data between UIViewController s so any help is appreciated. 我不太确定如何在UIViewController之间传递数据,因此不胜感激。

as per vshall says you can do this stuff like bellow:- 如vshall所说,您可以像下面这样做:-

yourAppdelegate.h yourAppdelegate.h

@interface yourAppdelegate : UIResponder <UIApplicationDelegate,UITabBarControllerDelegate>
{
      NSString *myCalResult;
}
@property (nonatomic,retain) NSString *myCalResult;

yourAppdelegate.m yourAppdelegate.m

@implementation yourAppdelegate
@synthesize myCalResult,

yourCalclass.h yourCalclass.h

#import "yourAppdelegate.h"

@interface yourCalclass : UIViewController
{
yourAppdelegate *objAppdelegate;
}

yourCalclass.m yourCalclass.m

- (void)viewDidLoad
{
    objAppdelegate = (yourAppdelegate *) [[UIApplication sharedApplication]delegate];
    [super viewDidLoad];
}

-(IBAction)ActionTotal
{
objAppdelegate.myCalResult=result;
}

Now you result stored in objAppdelegate.myCalResult you can use this variable in your another tab with creating object of yourAppdelegat. 现在,您将结果存储在objAppdelegate.myCalResult ,可以在另一个选项卡中使用此变量来创建yourAppdelegat的对象。 Hope its helps you 希望它可以帮助您

You can define a variable in app delegate and store the result in that variable for class one. 您可以在应用程序委托中定义一个变量,并将结果存储在该变量中,用于类1。 And once you switch the class you can fetch that value in your class two by creating an instance of your appDelegate and assign it to your textfield. 一旦切换了类,就可以通过创建appDelegate的实例并将其分配给文本字段来在第二个类中获取该值。

As Sanjit has suggested, NSUserDefaults is also a very convenient and clean way to achieve this. 正如Sanjit所建议的那样,NSUserDefaults也是实现此目的的一种非常方便且干净的方法。

Thanks. 谢谢。

If you don't really need to store the computed value but just notify the other controller in tab2 that the value changed, you can use NSNotificationCenter to post an NSNotification . 如果您实际上不需要存储计算的值,而只是在tab2中通知其他控制器该值已更改,则可以使用NSNotificationCenter发布NSNotification
When you initialize the controller in tab2 you'll need to add it as an observer of the notification. 在tab2中初始化控制器时,需要将其添加为通知的观察者。

Something like that: 像这样:
in tab1: 在tab1中:

NSNumber *value = nil; // the computed value
[[NSNotificationCenter defaultCenter]
 postNotificationName:@"com.company.app:ValueChangedNotification"
 object:self
 userInfo:@{@"value" : value}];

in tab2: register as an observer (in init or viewDidLoad methods) 在tab2中:注册为观察者(在init或viewDidLoad方法中)

[[NSNotificationCenter defaultCenter]
 addObserver:self
 selector:@selector(valueChanged:)
 name:@"com.company.app:ValueChangedNotification"
 object:nil];

the method that will be called when the notification is posted: 发布通知时将调用的方法:

- (void)valueChanged:(NSNotification *)note
{
    NSDictionary *userInfo = note.userInfo;
    NSNumber *value = userInfo[@"value"];
    // do something with value
}

Don't forget to remove the controller from the observers in viewDidUnload or sooner: 不要忘记从viewDidUnload或更早的观察者中删除控制器:

[[NSNotificationCenter defaultCenter] removeObserver:self];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM