0

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.

Kanan Vora
  • 2,124
  • 1
  • 16
  • 26

3 Answers3

0

In this function you also do pushView Controller

- (IBAction)save:(id)sender {

     SecondViewController *detailViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
    detailViewController.str = _TextField.text;
    [self.navigationController pushViewController:detailViewController animated:NO];
    [self.TextField resignFirstResponder];
}

try this may be helpfull..

Jogendra.Com
  • 6,394
  • 2
  • 28
  • 35
  • I added the pushViewController to my code and it didn't make any difference. When I push the save button the keyboard is dismissed, and then when I press the button to go to the next view it still showed up empty. – user3863053 Jul 22 '14 at 04:35
  • So I added this in ViewController: NSLog(@"%@", detailViewController.str); And this in SecondViewController: NSLog(@"%@", self.str); In ViewController it came up as my string but in SecondViewController it came up null – user3863053 Jul 22 '14 at 04:44
  • try to clean project and build again .. Because code is correct. – Jogendra.Com Jul 22 '14 at 04:47
  • I did a clean build and it didn't work. Does it matter if my views are embedded in a Navigation controller? And does it matter how I set up the segue? – user3863053 Jul 22 '14 at 04:54
  • does matter of segue only – Kumar KL Jul 22 '14 at 04:56
0

The problem is with your segue,

No need to assign in one of the action, which is not responsible for the segue.

Just override this method

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Make sure your segue name in storyboard is the same as this line
    if ([[segue identifier] isEqualToString:@"YOUR_SEGUE_NAME_HERE"])
    {
        SecondViewController *detailViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
    detailViewController.str = _TextField.text;
    }
}

The trick is that I don't want to have to use segues.

Then go with the NSUserDefaults -- Save string to the NSUserDefaults?

Community
  • 1
  • 1
Kumar KL
  • 15,315
  • 9
  • 38
  • 60
0
- (IBAction)save:(id)sender {

 SecondViewController *detailViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
detailViewController.str = _TextField.text;

[self.TextField resignFirstResponder];
}

I deleted this code [self.navigationController pushViewController:detailViewController animated:NO]; as this code prompts to push the view controller.

Hope this helps :)

Saheb Roy
  • 5,899
  • 3
  • 23
  • 35