Reputation: 1800
I have a program with multiple views, one of them has labels (LabelView for this question), the other has text fields(TextView for this question). I would like to take the info from the text fields, and put it on the label, I think the easiest way to do this by calling a method LabelView from TextView.
I have tried a couple things:
I tried putting a method in LabelView that changed the label's values with parameters. Then calling the method in TextView and passing the data through the parameters.
I tried making a delegate, using this question. Unfortunately the last step (add settingView.viewControllerDelegate=self
to the viewDidLoad
method) failed. settingView
was an undeclared identifier.
Does anyone know what I should do?
EDIT: Here is some I have done, using Xman's Parent/Child view idea. This is in TextView.m:
NSInteger curScore = [self.ToP1Score.text integerValue];
NSInteger curPhase = [self.ToP1Phase.text integerValue];
self.ToP1Score.text = [NSString stringWithFormat:@"%d", (curScore += [self.player1Txt.text integerValue])];
if ([self.player1Txt.text integerValue] < 50) {
self.ToP1Phase.text = [NSString stringWithFormat:@"%d", (curPhase += 1)];
}
Upvotes: 2
Views: 1342
Reputation: 2251
Two ways to achieve this.
1) Make ChildView
as a childView of ParentView
so that you can directly access ParentView
properties in ChildView
as following
ParentView.h
@interface ParentView : UIView
@property (nonatomic,strong) UITextField *mytextField;
@end
ChildView.h
#import "ParentView.h"
@interface ChildView : ParentView
@property (nonatomic,strong) UILabel *myLabel;
@end
2) Use Notification
Put this in FirstView
where the textField
is :
[[NSNotificationCenter defaultCenter] postNotificationName:@"sendData" object:yourTextFiels userInfo:nil];
Put this in SecondView
where your Label
is.
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeLabelValue:) name:@"sendData" object:nil];
and also this method
-(void)changeLabelValue(NSNotification *)iRecognizer {
UITextField *myTextField = iRecognizer object];
//Here You can change the value of label
}
Hope this will help you.
Upvotes: 2
Reputation: 1662
suppose you are passing some text from firststViewController to secondViewController, then in firststviewcontroller add these lines of code
-(IBAction)goToNextScreen:(id)sender
{
secondViewController *newVc = [[secondViewController alloc]initWithNibName:@"secondViewController" bundle:nil];
// or if you are using storyboard
// NewViewController *newVc = [self.storyboard instantiateViewControllerWithIdentifier:@"identifier"];
newVc.text = @" Your text ";
[self.navigationController pushViewController:newVc animated:YES];
}
in 2ndViewController.h file
#import <UIKit/UIKit.h>
@interface secondViewController : UIViewController
{
NSString text;
}
@property(nonatomic, retain)NSString text;
@property (weak, nonatomic) IBOutlet UILabel *lbl;
@end
in .m file
#import "secondViewController.h"
@interface secondViewController ()
@end
@implementation secondViewController
@synthesize text,lbl;
- (void)viewDidLoad
{
lbl.text = text;
}
@end
hope this will help you.. Dont forget to up vote...
Upvotes: 0