I've managed to setup a custom UIView class with a nib.
My .h looks like
@interface MyView : UIView <UITextFieldDelegate>
@property (nonatomic, weak) IBOutlet UITextField *textField;
@property (nonatomic, strong) MyView *topView;
And .m
@implementation MyView
NSString *_detail;
-(id)initWithCoder:(NSCoder *)aDecoder{
if ((self = [super initWithCoder:aDecoder])&&self.subviews.count==0){
MyView *v = [[[NSBundle mainBundle] loadNibNamed:@"MyView" owner:self options:nil] objectAtIndex:0];
self.textField = v.textField;
if (self.topView == nil)self.topView = self;
v.topView = self.topView;
[self addSubview:v];
}
return self;
}
-(NSString *)topDetail{
return _detail;
}
-(NSString *)detail{
return [self.topView topDetail];
}
-(void)setTopDetail:(NSString *)detail{
_detail = detail;
}
-(void)setDetail:(NSString *)detail{
[self.topView setTopDetail:detail];
}
- (BOOL)textFieldShouldReturn{
//here I show an UIAlertView using self.detail for the message
}
Note: The setup I have works exactly how I want it to.
The problem
What I would like to do is remove my manual detail
methods and turn NSString *_detail
into @property (...)NSString *detail
When I try it with the @property
, then within my ViewController
if i call
myView.detail = someString
, myView will be referring to the top most view. Then if textFieldShouldReturn
gets called because of user interaction, then it calls the nested MyView
s _detail
which has not been set.
What I want:
To not have to write extra code for access to _detail
regardless of where I'm accessing it from. I want to merely declare the property and go on with my usual coding.