Question

I have 2 UIView subclasses which are added in a UIViewController. 1 of the subclasses sets an integer and when when an IBAction gets called the other one should use it.

View1 .h:

#import <UIKit/UIKit.h>

@interface View1 : UIView

@property(nonatomic) int concentration;

View1 .m:

@synthesize concentration;

concentration = 10-stepper.value; // in an IBAction called by a UIStepper

View2 .h:

#import <UIKit/UIKit.h>
#import "View1.h"

@interface View2 : UIView {

View1 *options;

}

View2 .m:

options = [[View1 alloc] init];
NSLog(@"%i", options.concentration); // always return 0

The integer "concentration", I have checked if View1 even set the value and it does, but when View2 tries to use it its always 0.

Why is it 0? Im allocating the View1 and init and everything :S

Was it helpful?

Solution

This doesn't wok because you're Alloc init'ing a different instance of view1 from the one you have on screen. You need to get a reference to that one. However, it would be better to do this at the view controller level rather than the views themselves. The action methods should be in the view controller, and it should have an outlet to each subview.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top