ios - 如何从 View 获取数据到 UITabBarController

标签 ios objective-c uitabbarcontroller getter

我有一个 UITabBarController,它有 3 个 UIViewControllers。每个 Controller 都有一些 UITextFields。在 UItabBarController 中有一个“保存”按钮。请问,如何从 UIViewControllores 中的 UITextFields 获取数据?我需要来自所有 UITextFieldsUITabBarController 中所有 UIViewControllers 的数据(在保存按钮的 IBAction 中)。我该怎么做?谢谢。

这是 Storyboard 上我的 TabBarController 的图片。

enter image description here

第一个 ViewController 有一个 StudentVC 类。该类有一个文本字段。在 StudentVC.m 中是这样的代码:

#import <UIKit/UIKit.h>
#import "CoreDataHelper.h"

@interface StudentVC :  UIViewController <UITextFieldDelegate>

@property (strong, nonatomic) NSManagedObjectID *selectedStudentID;
@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
@property (strong, nonatomic) IBOutlet UITextField *studentNameTextField;

@end

studentNameTextField 与 Storyboard 中的字段相关联。然后我有一个 TabBar 类,类名是 StudentTBC。在文件 StudentTBC.m 我有这段代码:

- (void)viewDidLoad
{
    if (debug == 1) {
        NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));
    }
    [super viewDidLoad];

    StudentVC *studentVC = [self.tabBarController.viewControllers objectAtIndex:0];
    studentVC.studentNameTextField.text = @"asad";
}

所以,当我继续使用 TabBar 时,我会在 studentNameTextField 中看到文本“asad”,但是该字段很清楚...所以我还在 IBAction 中添加:保存此代码:

- (IBAction)save:(id)sender {
    if (debug == 1) {
        NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));
    }

    StudentVC *studentVC = [self.tabBarController.viewControllers objectAtIndex:0];
    NSString *string = studentVC.studentNameTextField.text;
    NSLog(@"string:%@", string);

但是,当我插入该字段并按“ulozit”(它以捷克语保存)时,我得到的是“string: null”而不是“string: textWhichIWriteToField”。我尝试了索引 0、1、2,但没有任何效果。 :/

编辑 3.4 15:20

我移动了我的应用程序。第一部分是我的 Storyboard——UITabBarController 和 3UIViewControllers。我删除了一个导航 Controller 。在第二部分,我又做了一个奥特莱斯。第三部分是save函数的代码(代码可以写一个textfield的内容)和TabBarController的ViewDidLoad代码(代码可以设置一个textfield的字符串)。最后一部分是运行应用程序(结果在控制台中 - 空字符串和清除文本字段)。 我也试过打印这个:

NSInteger countOfViews = [self.tabBarController.viewControllers count];
NSLog(@"count:%ld", (long)countOfViews);

一定要三个?或者?因为我的结果是 0.. 为什么? :/

我的电影:https://www.youtube.com/watch?v=j6p__e48lLI&feature=youtu.be

已编辑 2.4 20:58 ------------------

你好。我尝试了所有选项,但没有任何效果。所以我做了一个新的简单应用程序,其中只有一个 UITabBarController 带有两个 UIViewController 和一个 UITableViewController(因为导航栏)。请下载项目并亲自尝试。仅运行,然后单击“TabBar”按钮,您就在 UITabBarController 上。当您按下保存按钮时,它应该显示 UIViewsControllers 的计数和文本字段的值。你能试着告诉我出了什么问题吗?谢谢。这是项目:

https://www.dropbox.com/s/r4jlzadr2us00ad/TBC.zip

最佳答案

要访问其中一个文本字段,您可以使用以下代码:

- (IBAction)save {
    YourUIViewController *vc = [self.tabBarController.viewControllers objectAtIndex:indexOfController];
    NSString *str = vc.yourTextField.text;
}

编辑。你知道你的 Controller 是如何连接的,现在我意识到你在 UIViewController 之后得到了 UINavigaitonController。修改后的代码应如下所示:

- (IBAction)save {
    NSArray *navConArr = [self.tabBarController.viewControllers objectAtIndex:indexOfController];
    for(UINavigationController *nc in nacConArr) {
        UIViewController *vc = [nc.viewControllers objectAtIndex:0];
        NSLog(@"And your text is = %@", vc.studentNameTextField.text);
    }
}

已更新

我终于解决了问题... TabBarController 的正确代码

- (IBAction)save {
    YourUIViewController *vc = [self.viewControllers objectAtIndex:indexOfController];
    NSString *str = vc.yourTextField.text;
}

关于ios - 如何从 View 获取数据到 UITabBarController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22832778/

相关文章:

IOS - Linkedin 将 oauth2.0 与 google oauth 库集成 - 出现 404 错误(找不到页面)

php - 将数据从 php 导入 tableview 的最佳方法

ios - 移动到下一个 UIViewController 时保留 UINavigationController 但隐藏 UITabBarController

ios - 无法将类型 '()' 的值转换为预期的参数类型“[UIViewController]?

objective-c - 在 tabBarView 中管理套接字流

ios - 我如何将数据从 View Controller 传递到容器 View ?

iphone - 如何结合缩放和平移动画

objective-c - 为 XCode4 安装 cocos2d 模板

iOS 7-didReceiveRemoteNotification : not triggering on background

ios - swift 的ios。 fatal error : Unexpectedly found nil while unwrapping an Optional value