-3

I'm creating a project so that I have two view controllers, connected by a modal segue with an identifier "login_success".

In the primary view controller, I have a text field that takes the input of whatever the user types, and a button to perform the segue.

In the next controller, I have a label that is supposed to print out whatever the user typed.

My code:

DICViewController.h (First View Controller):

#import <UIKit/UIKit.h>

@interface DICViewController : UIViewController <UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *txtUsername;

- (IBAction)sigininClicked:(id)sender;
- (IBAction)backgroundTap:(id)sender;

@end

DICViewController.m:

#import "NewViewController.h"

@interface DICViewController ()

@end

@implementation DICViewController


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

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

- (IBAction)sigininClicked:(id)sender {
{
    [self performSegueWithIdentifier:@"login_success" sender:self];
}
}

- (IBAction)backgroundTap:(id)sender {
    [self.view endEditing:YES];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

@end

NewsViewController.h (The other view controller):

#import <UIKit/UIKit.h>

@interface NewViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *steamId; //my label

@end

NewsViewController.m:

No code was added here.

Thanks in advance to anyone that can help.

Again, I would like to be able to set the text in the label equal to the text the user types in the text field.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Varun Iyer
  • 523
  • 2
  • 10
  • 25
  • 1
    Because this same question gets asked 2-3 times a day. And the the answer should be obvious, if you really understood the principles of object-oriented programming (or even just basic C-like pointers). – Hot Licks May 21 '14 at 00:28

2 Answers2

4

When performing a segue the preferred way to pass data from one view controller to another is to make use of the method -prepareForSegue:sender:.

In your case the following lines of code should work:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    NewsViewController *newsVC = segue.destinationViewController;
    [newsVC view]; // this loads the view so that its subviews (the label) are not nil
    newsVC.steamID.text = self.txtUsername.text;
}

(Place this method anywhere in your DICViewController.m.)

Mischa
  • 15,816
  • 8
  • 59
  • 117
  • thanks for the answer! I will try this and hopefully it will work. – Varun Iyer May 20 '14 at 22:24
  • I'm sorry but your method didn't work. The label says "label," it doesn't change in relation to what I type in the text field. – Varun Iyer May 20 '14 at 23:24
  • First, put an NSLog at the beginning of your prepareForSegue: method and check if self.txtUsername.text is empty. Second: Do you have a Navigation Controller between your DICViewController and your NewsViewController? – Imanou Petit May 20 '14 at 23:35
  • @user1966109 I do not have a navigation controller. I am using a modal segue, not a push segue. Does this mean I would have to implement something different? – Varun Iyer May 20 '14 at 23:44
  • 1
    @VarunIyer: You are right. I updated my answer accordingly. The problem is that the destination view controller's view is not yet loaded when the method `prepareForSegue:sender:` is called. But you can preload it by adding the line `[newsVC view];` and that should do the job. – Mischa May 20 '14 at 23:45
  • 1
    (Another solution would be to create a property in your `NewsViewController` of type `NSString`, then set that variable in the `prepareForSegue:sender:` method instead and in your `NewsViewController`'s `viewDidLoad` method you set the label's text.) – Mischa May 20 '14 at 23:48
1

I think The better way is to set global variables. Just make normal class

variables.h

#import <Foundation/Foundation.h>

@interface variables : NSObject {
    float VariableYouWant;
}

+ (_tuVariables *)sharedInstance;

@property (nonatomic, assign, readwrite) float VariableYouWant;

and variables.m

#import "variables.h"

@implementation variables

@synthesize VariableYouWant = _VariableYouWant;


+ (_tuVariables *)sharedInstance {
    static dispatch_once_t onceToken;
    static variables *instance = nil;
    dispatch_once(&onceToken, ^{
        instance = [[variables alloc] init];
    });
    return instance;
}

- (id)init {
    self = [super init];
    if (self) {

    }
    return self;
}

@end

Way to use: import header file of variables and

variables  *globals = [variables sharedInstance];

and simply access variables with

globals.VariableYouWant = 
  • 1
    thanks for the alternative answer, but I think this makes it much more complicated than it needs to be. up voted. – Varun Iyer May 20 '14 at 23:21