babon
babon

Reputation: 13

How to change UITextField text of View2 from View1?

I'm trying to change UITextView.text from SecondViewController in ViewController. I can do it if it was NSString type in SecondViewController by doing:

SVC.string = @"test";

The problem is that:

One of the methods in ViewController.m:

    SecondViewController *SVC = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];
    SVC.messageBox.text = @"changed";
    NSLog(@"SVC: %@", SVC.messageBox.text); // result = (null)

    [self presentViewController:SVC animated:YES completion:NULL];

SecondViewController.h:

#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController

@property (nonatomic) IBOutlet UITextView *messageBox;


@end

SecondViewController.m:

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

    self.messageBox.text = @"first";
}

How to get over with this?

Upvotes: 0

Views: 136

Answers (5)

Ganapathy
Ganapathy

Reputation: 4614

The problem with your code is your accessing the uicontrols which are not yet created in the memory. You have to change the order of the execution of your statements like this...

 SecondViewController *SVC = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];

    [self presentViewController:SVC animated:YES completion:NULL];


    SVC.messageBox.text = @"changed";
    NSLog(@"SVC: %@", SVC.messageBox.text);

Upvotes: 0

Paras Joshi
Paras Joshi

Reputation: 20551

just property-synthesize string andf just assign this string to yourTextView.text. For example see bellow..

@interface SecondViewController : UIViewController{
       NSString *strMsgText;
}
@property(nonatomic, retain)    NSString *strMsgText;
@end

@implementation SecondViewController
@synthesize strMsgText;

after that when you present SecondViewController at that time assign your value like bellow..

    SecondViewController *SVC = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];
    SVC.strMsgText = @"changed";
    [SVC.strMsgText retain];


    [self presentViewController:SVC animated:YES completion:NULL];

and in viewDidLoad: method of SecondViewController just assign that string to messageBox(TextView), like bellow...

- (void)viewDidLoad
{
     messageBox.text = strMsgText;
}

Upvotes: 0

lakshmen
lakshmen

Reputation: 29094

You can use delegates to do it as well.

Example:

Viewcontroller2nd.h

@protocol SecondViewControllerDelegate <NSObject>
@optional
-(void) changeLabel:(NSString*)str;
@end

@interface ViewController2nd : UIViewController{

    IBOutlet UIButton *bttn;
    id <SecondViewControllerDelegate> delegate;

}

@property (retain) id delegate;

@end

Viewcontrollerone.h

@interface ViewController : UIViewController <SecondViewControllerDelegate>
{
    IBOutlet UILabel *lbl;

}
-(IBAction)passdata:(id)sender;

@end

Viewcontrollerone.m

-(void) changeLabel:(NSString*)str{
    lbl.text = str;
}

More about delegates:http://developer.apple.com/library/ios/#documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html

Hope this helps...

Upvotes: 1

Ravindhiran
Ravindhiran

Reputation: 5384

Try this, One of the methods in ViewController.m:

SecondViewController *SVC = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];
SVC.strMessage= @"changed";
[self presentViewController:SVC animated:YES completion:NULL];

SecondViewController.h:

#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController
{
  IBOutlet UITextView *messageBox;
}
@property (nonatomic) NSString *strMessage;
@end

SecondViewController.m: @synthesize strMessage;

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

self.messageBox.text = strMessage;
}

Upvotes: 2

rptwsthi
rptwsthi

Reputation: 10182

Set @property (nonatomic) IBOutlet UITextView *messageBox; to @property (nonatomic, strong) IBOutlet UITextView *messageBox; and check if you have connected it with UItextView. And don't reset it in viewDidLoad. Another thing you can try is:

SecondViewController *SVC = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];
[self presentViewController:SVC animated:YES completion:NULL];
SVC.messageBox.text = @"changed";
NSLog(@"SVC: %@", SVC.messageBox.text); // result = (null)

Along with what mentioned above.

Upvotes: 0

Related Questions