如何在2个选项卡式应用程序中传递数据?

时间:2013-11-14 21:51:59

标签: ios objective-c delegates

我有一个我正在制作的应用程序,但这是我在加速数小时计算出来的问题。 我有一个基于选项卡的应用程序,第一个选项卡有一个UILabel对象,它应该显示来自SecondViewController.m的NSString,它有一个方法:

-(IBAction) save: (id) sender{

   // This String holds data from the secondViewController's textfield to be passed to the 
   // UILabel whch is on the firsViewController.m

   NSString *data = self.addDataTextfield.text;
 }

我已经使用了很多方法,包括Singletons,但它们不工作,因为我已经读过某些地方,单身人士想要将数据从父传递给孩子,而且孩子无法在这种情况下将数据发送到父控制器但是唯一的方法就是使用我的协议,但我有点迷失使用这种方法。这是我在secondViewController.h上的内容

#import <UIKit/UIKit.h>
#import "singletonObj.h"
#import "AppDelegate.h"

// Using a Protocol to pass data Back to the parent view
@protocol passStringDelegate <NSObject>

-(void) enteredString: (NSString *)string;

 @end

 @interface SecondViewController : UIViewController <UITextFieldDelegate>
   {
     singletonObj *object; // This singleton isnt being used, Just there incase

    }


    @property (strong, nonatomic)IBOutlet UITextField*addDataTextField;



    - (IBAction)Save:(id)sender;

    @property (retain) id <passStringDelegate> delegate;

    @end

所以我的问题是,有什么方法我可以使用@protocol从这个secondViewController传递数据,或者至少有人能告诉我如何在这段代码中使用NSNotificationCenter传递数据吗?我觉得使用AppDelegate类来传递数据是不可靠的,因为它似乎违背了苹果的方式或准备了对我来说不起作用的猜测。

我一直在寻找,但大多数我发现从父母发送到孩子的数据但是我没有看到基于Tabbed的示例,其中ChildViewController可以将NSString发送到ParentViewController以在UILAbel上显示该数据。

2 个答案:

答案 0 :(得分:0)

有几种方法可以做到这一点。从任何标签栏控制器的内容控制器,您可以使用self.tabBarController.viewControllers [0]等访问另一个控制器。这将在第一个选项卡中引用控制器。因此,如果要将字符串从第二个控制器传递回第一个控制器,请在第一个控制器中创建一个字符串属性(比如passInString),并在第二个控制器中创建如下内容:

FirstViewController *first = self.tabBarController.viewControllers[0];
first.passedInString = self.stringToPass;

答案 1 :(得分:0)

Ok @ user2994008,在UITabBarController应用程序设置中,我会以某种方式在子视图控制器之间强制耦合时使用委托模式。发布通知将完成工作,唯一的问题是应该触发通知的内容。在这种情况下,快速而肮脏的方法是在viewWillDisapper中发布通知:因为当用户切换标签时会调用该通知。

SecondViewController.m

//SecondViewController.m
//this is the view with the UITextField in it
#import "SecondViewController.h

@implementation SecondViewController 

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    NSLog(@"%s", __FUNCTION__);
    //since this gets called when the user switches tabs, i'll post the 
    //notification from this method

    //retrieve our text field's text and store it in a dictionary 
    NSDictionary *dict = @{"text":self.addDataTextField.text};

    //add the dictionary as userInfo: and post notification
    [[NSNotificationCenter defaultCenter] postNotificationName:@"myAwesomeNotification" object:nil userInfo:dict];
}  
//all other second view controller methods here
@end 

FirstViewController.m

//FirstViewController.m
#import "FirstViewController.h"

@implementation FirstViewController

-(void)viewDidLoad {
    [super viewDidLoad];

    //add an observer for the notification and tell it what method to call when it gets the notification
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(actionTriggeredByNotification:) name:@"myAwesomeNotification" object:nil];
}

- (void)actionTriggeredByNotification:(NSNotification*)notification {
    NSLog(@"%s", __FUNCTION__);
    NSDictionary *userInfo = [notification userInfo];
    NSString *data = userInfo[@"text"];

    NSLog(@"Text data is: %@", data);
}

- (void)dealloc {
    //be sure to remove observer 
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    //if using ARC, don't call super dealloc
}
//all other first view controller methods
@end

此外,删除尝试以这种方式传递数据的单例内容。那就是尝试创建本质上是全局变量的东西,并且你并不真的想要传递简单的字符串。

虽然在这里使用通知工作正常,但可以使用键值观察(KVO)或遵守UITextFieldDelegate协议并从其中一个委托方法发送消息来实现其他一些解决方案。即便如此,所有这些技术仍然会在 时真正想要检索文本字段的文本时提出问题。

一旦你掌握了方向,我强烈建议你研究ReactiveCocoa。这是一个专为解决像你这样的问题而设计的图书馆。