2

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:

  1. 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.

  2. 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)];
}
Community
  • 1
  • 1
DonyorM
  • 1,760
  • 1
  • 20
  • 34

2 Answers2

2

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.

Prashant Nikam
  • 2,253
  • 4
  • 17
  • 29
  • How do I make a view a child view of a different view? – DonyorM Sep 04 '13 at 05:28
  • use addSubview method – Amitabha Sep 04 '13 at 05:33
  • @Xman, it's weird. I tried the parent/child view thing, but the labels wouldn't change. – DonyorM Sep 05 '13 at 10:09
  • so self.ToP1Score is the TextView whose value you want to pass into UILabel which is another class ? – Prashant Nikam Sep 05 '13 at 10:44
  • Sorry, self.ToP1Score is the label I want to change. I want to add the value of self.player1Txt to the value of self.ToP1Score and then but that value into the label. I know how to do that, but I don't know how to change the label. I can take this to chat sometime if you want. – DonyorM Sep 06 '13 at 07:35
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/36912/discussion-between-donyorm-and-xman) – DonyorM Sep 06 '13 at 08:45
  • @Xman here's a link to the chat: http://chat.stackoverflow.com/rooms/36912/discussion-between-donyorm-and-xman – DonyorM Sep 06 '13 at 08:46
0

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...

Amitabha
  • 1,664
  • 1
  • 12
  • 30