I have looked all over the internet for an answer to this question and I have tried multiple different suggested ways but it still won't work.
I have a single UIViewController
with a UITextField
and two UIButtons
. One to save the input as a string and another to segue into another view. In other view I just have a UILabel
. All I want to do is take the text the user input, save it as a string in the second UIViewController
and then be able to display it. The trick is that I don't want to have to use segues.
This is my code:
ViewController.m
#import "ViewController.h"
#import "SecondViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (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)save:(id)sender {
SecondViewController *detailViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
detailViewController.str = _TextField.text;
[self.TextField resignFirstResponder];
}
@end
SecondViewController.h
@interface SecondViewController : UIViewController
@property (nonatomic, retain) NSString *str;
@property (weak, nonatomic) IBOutlet UILabel *Label;
@end
SecondViewController.m
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[_Label setText:_str];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
If someone could tell me what is wrong with the code that would be really helpful. When I run the app and type in text, then go to SecondView the label will show up empty.