0

I have two UIView contained in a UIViewController - firstView and secondView - that I initialize pragmatically. I have a UILabel in the firstView, and a UIButton in the secondView. I would like the button in the second view to change the label in the first view. In the implementation file of the second view I have the following code:

- (void) changeLabel: (UIButton *) sender{
    firstView *view = [[firstView alloc] init];
    view.label.text = @"Changed Text";
}

However I figured out that the above method just initializes a new class of firstView and does not link to the existing UIView. How can I change properties of firstView from within secondView?

Acey
  • 8,048
  • 4
  • 30
  • 46
Amendale
  • 317
  • 3
  • 19

1 Answers1

2

Create properties in your view controller's header file for the views:

@property (nonatomic, retain) UIView *firstView;
@property (nonatomic, retain) UILabel *label;

When you create the view and label assign them to the properties:

self.firstView = // create your view here

self.label = // create your label here

Create a button property on your UIView object so you can access it later:

@property (nonatomic, retain) UIButton *button;

Then in your view controller file, when you create everything, access the view's button property and add a target, like this:

[firstView.button addTarget:self action:@selector(changeLabel) forControlEvents:UIControlEventTouchUpInside];

Then you can simply have the method your button calls be like this:

- (void)changeLabel {
    self.label.text = @"Changed Text.";
}
Mike
  • 9,765
  • 5
  • 34
  • 59
  • Thanks. Still trying to figure it out though. My method for the button is contained inside secondView, thus self.label.text points to secondView and not the ViewController. Is there a way around this? – Amendale Aug 25 '14 at 21:48