-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.

1
  • 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). Commented May 21, 2014 at 0:28

2 Answers 2

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.)

Sign up to request clarification or add additional context in comments.

6 Comments

thanks for the answer! I will try this and hopefully it will work.
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.
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?
@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.
(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.)
|
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 Comment

thanks for the alternative answer, but I think this makes it much more complicated than it needs to be. up voted.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.