I have a UITabBarController, which has 3 UIViewControllers. Each controller has a few UITextFields. In UItabBarController is a button "save". Please, how can I get data from UITextFields in UIViewControllores? I need the data from all UITextFields and from all UIViewControllers in UITabBarController (in IBAction of save button). How can I do it? Thanks.

Here is picture of my TabBarController on Storyboard.

enter image description here

The first ViewController has a class StudentVC. The class has one text field. In StudentVC.m is this code:

#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

The studentNameTextField is connected with field in Storyboard. Then I have a class for TabBar the class name is StudentTBC. In file StudentTBC.m I have this code:

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

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

So, when I go on TabBar I will see in studentNameTextField the text "asad", but the field is clear... So I added also in IBAction:save this code:

- (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);

But when I insert the field and press "ulozit" (its save in czech), I get a "string: null" instead of a "string: textWhichIWriteToField". I tried indexes 0,1,2 but nothing works. :/

Edited 3.4 15:20

I did a move of my application. In first part is my storyboard - UITabBarController and 3UIViewControllers. I deleted a Navagition Controllers. In second part I do again a Outlets. Third part is code of save function (the code could be write a content of textfield) and code of ViewDidLoad of TabBarController (code could be set a string of textfield). The last part is run application (the result is in console - null string and clear textfield). I also tried print this:

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

It has to be three? or? Because my result was 0.. Why? :/

My movie: https://www.youtube.com/watch?v=j6p__e48lLI&feature=youtu.be

EDITED 2.4 20:58 -------------------

Hello. I tried all options but nothing worked.. So I did a new SIMPLE application, where is only one UITabBarController with two UIViewControllers and one UITableViewController (because of navigation bar). Please download the project and try it yourself. Only run, then click on button "TabBar" and you are on the UITabBarController. When you press a Save button, it should display a count of UIViewsControllers and the value of textfields. Could you please try and tell me what's wrong? Thank you. Here is the project:

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

有帮助吗?

解决方案

To access to one of text field you can use code below:

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

EDIT. You have me how your controllers are connected and now i realized you got UINavigaitonController after UIViewController. Modified code should look like:

- (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);
    }
}

Updated

I solved the problem finally... The right code for TabBarController

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

其他提示

Please check outlets for UITextFields,

Also check output for

NSLog(@"StudentVC textField -- %@",studentVC.studentNameTextField); in save()

if it is also null, then there is definitely problem with your outlets.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top