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

Ios 如何在2选项卡式应用程序中传递数据?,ios,objective-c,delegates,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 // UILabe

我有一个申请,我正在做,但这里有一个问题,我花了几个小时才弄清楚。 我有一个基于选项卡的应用程序,第一个选项卡有一个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;
 }
我使用了很多方法,包括单例,但它们不起作用,因为我在某个地方读到,单例意味着将数据从父级传递到子级,子级不能将数据发送到父级控制器。在这种情况下,唯一的方法是使用协议,但我对使用这种方法有点迷茫。这是我的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
#导入
#导入“singletonObj.h”
#导入“AppDelegate.h”
//使用协议将数据传递回父视图
@协议passStringDelegate
-(void)输入字符串:(NSString*)字符串;
@结束
@界面SecondViewController:UIViewController
{
singletonObj*object;//这个singleton没有被使用,只是以防万一
}
@属性(强,非原子)IBOutlet UITextField*addDataTextField;
-(iAction)保存:(id)发送方;
@属性(保留)id委托;
@结束
所以我的问题是,我有没有办法使用@protocol从这个secondViewController传递数据,或者至少有人能告诉我如何在代码中使用NSNotificationCenter传递数据?我觉得使用AppDelegate类传递数据不方便,因为这似乎与苹果的方式或prepareforSegue背道而驰,而这对我来说并不适用


我一直在四处搜索,但我发现大部分都是从父对象发送到子对象的数据,但我没有看到基于选项卡的示例,其中ChildViewController可以向ParentViewController发送NSString以在UILAbel上显示该数据。

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

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

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

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
#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
FirstViewController.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
#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协议并从其中一个委托方法发送消息来实现其他一些解决方案。即使如此,所有这些技术仍然会带来一些问题,即您何时真正想要检索textfield的文本


一旦你确定了方向,我强烈建议你调查一下。这是一个专门为解决像您这样的问题而设计的库。

有人吗?……我尝试了这种方法,但没有成功,我将您显示的代码放在iAction save按钮中,并且没有将数据传递到第一个选项卡,它链接在一起,甚至合成了要传递的字符串。你应该记录self.tabViewController和self.tabViewController.viewControllers[0],看看它们是否能满足你的期望。你有什么简单的基于选项卡的app xcode项目可以发送给我吗?我已经想了好几天了,我尝试的每一种方法要么将UIlabel留空,要么在发送NSString时不会更改文本。@user2994008,您可以从这里下载一个简单的示例,任何方法都可以使用字符串:@“传递给第一个控制器”,实现一个文本字段以从uitextfield传递nsstring?我试图放:self.stringToPass=[self.textField text];但我并没有努力传递我要设置的字符串。