Reputation: 455
I have an app with two viewcontrollers and I want to show the text from a UILabel in VC1 in a UITextView inside of VC2.
I have been able to access the UILabel from VC1 in the viewDidLoad of VC2 but the actual text does not appear.
In VC2 I created a VC1 object and can access VC1. Here is the VC2 viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.textView.text = vc1Controller.label.text;
}
But when I run the app and the VC2 appears, nothing appears in the UITextView. I do have it wired and can show text by doing an NSLog(@”Yes”); for example in the above method.
When I do an NSLog(@”%@”, vc1Controller.label.text); I get NULL in the output.
Do I need to create a custom getter? Any advice appreciated. I’m relatively new to all programming.
Upvotes: 0
Views: 154
Reputation: 858
This is how you pass a NSString from views:
First View:
@interface ViewController : UIViewController{
IBOutlet UILabel *firstLbl;
NSString *firstString;
}
-(IBAction)labelTouched:(id)sender;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
firstString = @"I'm the first labels text";
firstLbl.text = firstString;
}
-(IBAction)labelTouched:(id)sender{
ViewController2 *view2 = [[ViewController2 alloc] init];
view2.selectedFirstLabelString = firstString;
[self presentModalViewController:view2 animated:YES];
[view2 release];
}
SecondView:
@interface ViewController2 : UIViewController{
IBOutlet UILabel *secondLbl;
}
@property (nonatomic, retain)NSString *selectedFirstLabelString;
-(IBAction)done:(id)sender;
@end
@implementation ViewController2
@synthesize selectedFirstLabelString;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
secondLbl.text = selectedFirstLabelString;
}
Hope this helps.
Upvotes: 1
Reputation: 5812
When you create a VC1 object in VC2, you are obviously not referring to the VC1 that pushed VC2.
Your vc2 is trying to read a label value from the newly created vc1, while you actually want to read the value from the VC1 that pushed VC2.
There are a few ways you can get your code working:
1) accessing the parentViewController (if you are presenting vc2 as modal)
In VC2, you can refer to your parent controller:
VC1* vc = (VC1 *)self.parentViewController; VC2.textView.text = vc.label.text;
2) Accessing the vc1 from the navigationcontroller stack: How to access the stack in UINavigationController This post has it all
3) Use Delegate pattern. Custom delegate This one has it all!
Hope you find this useful.
Upvotes: 1
Reputation: 106
In the line:
self.textView.text = [vc1Controller self.label.text];
You are using the self
keyword in [vc1Controller self.label.text]
, but that is going to reference a nonexistent UILabel in the current view controller (View Controller 2). Make the label in vc1Controller a property, and then use:
vc1Controller.label.text;
to access its text value.
Upvotes: 1