【问题标题】:Passing data between ViewControllers is working, but not with Tab Bar Controller在 ViewControllers 之间传递数据是有效的,但不能使用 Tab Bar Controller
【发布时间】:2013-12-20 12:05:30
【问题描述】:

谁能帮我解决以下问题?

我是 Xcode 的新手,我还在学习它,但我可以弄清楚如何在 ViewController 之间传递数据。

这是我的工作代码:

FirstViewController.h:

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController
- (IBAction)displayView:(id)sender;
@property (weak, nonatomic) IBOutlet UILabel *lblFirst;


@end

FirstViewController.m:

#import "FirstViewController.h"
#import "SecondViewController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController
@synthesize lblFirst;
SecondViewController *secondViewController;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];

    secondViewController.forwarded_lblFirst = lblFirst;

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)displayView:(id)sender {


    [self.view addSubview:secondViewController.view];
}
@end

SecondViewController.h:

#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController

@property (nonatomic, retain) UILabel *forwarded_lblFirst;
@property (weak, nonatomic) IBOutlet UITextField *txtSecond;
- (IBAction)btnReturn:(id)sender;
- (IBAction)txtSecond_DidEndOnExit:(id)sender;
- (IBAction)btnSecond:(id)sender;

@end

SecondViewController.m:

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController
@synthesize forwarded_lblFirst;
@synthesize txtSecond;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)btnReturn:(id)sender {
    [self.view removeFromSuperview];
}
- (IBAction)txtSecond_DidEndOnExit:(id)sender {
    forwarded_lblFirst.text = txtSecond.text;
    [txtSecond resignFirstResponder];
    [self.view removeFromSuperview];
}

- (IBAction)btnSecond:(id)sender {
    forwarded_lblFirst.text = txtSecond.text;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [txtSecond resignFirstResponder];
}

@end

这段代码运行良好,我在 SecondViewController 的文本框中输入的文本(在按下 btnSecond 或只是键盘上的 Return 键之后)显示为 FirstViewController 上的标签文本。

我的问题是,当我做完全相同的事情,但使用选项卡栏应用程序时,第一个选项卡上的标签不会改变。

我可以使用完全相同的代码(除了更改视图部分,因为我处理带有编程按钮的第一个应用程序,但对于 Tab-Bar-App 已经有按钮),还有两个 ViewControllers 用于标签栏应用程序也是如此(每个标签一个)。 所以代码是相同的,逐行,逐个字符,文件也是相同的,对于两个应用程序(基于视图,标签栏)。

为什么我的代码不起作用?我怎么解决这个问题? 谢谢!

【问题讨论】:

  • 您的 firstviewcontroller 和 secondviewcontroller 是标签栏的不同标签,还是您在显示时从第一个视图调用 secondview?
  • 我是这样使用的:link

标签: ios objective-c tabbar


【解决方案1】:

你需要阅读一些关于在视图之间传输数据的文章:

谈到你的问题,试试这个方法:

将属性添加到您的应用程序委托。

在分配属性时,请执行以下操作:

MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];

delegate.myProperty = @"My Value";

然后,在您的不同选项卡中,您可以以相同的方式检索此属性:

MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
NSString *valueInTab = delegate.myProperty; 

【讨论】:

    【解决方案2】:

    解决此问题的另一种方法是使用独立的数据模型。

    创建一个对象,为您的应用程序需要以任何重要方式保存、加载、共享或使用的所有数据提供访问和操作方法。使对象可作为单例或作为应用程序委托的属性使用。发生更改时,让数据模型更新应用程序状态。当需要显示某些内容时,让控制器从数据模型中获取它并将其发送到视图(MVC!)。

    如果您不将实际属于模型的内容存储在控制器中,则无需担心从控制器传递信息到控制器。

    【讨论】:

      【解决方案3】:

      你需要在你的 secondview 控制器中使用 tabbardelegate,假设你使用这个schema

      FirstViewController.h

      #import <UIKit/UIKit.h>
      
      @interface FirstViewController : UIViewController
      
      @property (strong, nonatomic) NSString *content;
      
      @end
      

      SecondViewController.m

      #import "SecondViewController.h"
      #import "FirstViewController.h"
      
      @interface SecondViewController ()
      
      @end
      
      @implementation SecondViewController
      
      - (void)viewDidLoad
      {
          [super viewDidLoad];
          // Do any additional setup after loading the view, typically from a nib.
          self.tabBarController.delegate = self;
      
      }
      
      - (void)didReceiveMemoryWarning
      {
          [super didReceiveMemoryWarning];
          // Dispose of any resources that can be recreated.
      }
      
      
      -(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
      
          if ([viewController isKindOfClass:[FirstViewController class]]){
              FirstViewController *vc =( FirstViewController *) viewController;
              vc.content = @"your content";
          }
      
      
          return TRUE;
      }
      

      【讨论】:

        猜你喜欢
        • 2017-12-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-09
        • 2021-07-05
        • 1970-01-01
        • 2012-09-30
        • 2019-10-20
        相关资源
        最近更新 更多