在我的應用程序中,我已經對UIView進行了子類化,並且在.m文件(2UIButton和UITextField)中聲明瞭三個ivars。我通過從對象庫拖動UIView並將它們轉換爲我的子類來使用此對象。下面是我的實現如何在界面構建器(.xib)中顯示子類UIView之前?
@interface Prescriber()<UITextFieldDelegate>
{
UIButton *_addButton;
UIButton *_lessButton;
UITextField *_valueField;
}
@end
@implementation Prescriber
@synthesize value=_value,intValueofStrig=_intValueofStrig;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
NSLog(@"prescriber called");
return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
-(void)layoutSubviews
{
if (!_addButton) {
_addButton=[[UIButton alloc]init];
[_addButton addTarget:self action:@selector(addbuttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[_addButton setBackgroundImage:[UIImage imageNamed:@"add1.png"] forState:UIControlStateNormal];
_addButton.layer.cornerRadius=5;
}
if (!_lessButton) {
_lessButton=[[UIButton alloc]init];
[_lessButton addTarget:self action:@selector(lessButoonClicked:) forControlEvents:UIControlEventTouchUpInside];
[_lessButton setBackgroundImage:[UIImage imageNamed:@"Minus.png"] forState:UIControlStateNormal];
}
if (!_valueField) {
_valueField=[[UITextField alloc]init];
_valueField.delegate=self;
}
///positioning the addbutton on left corner and right corner and position and
textfield in the center
}
我所做的組態碼 - (空)layoutSubviews方法,我-initwithFrame不會被調用,因爲它們已經在廈門國際銀行文件。 我想知道的是,在-layoutSubViews中進行初始化是否正確,因爲這種方法有機會再次調用。或者我能夠調用 -viewDidLoad像delgate方法爲我的子類視圖呢?
編輯: - 這是可能的創建一個IBOutlet,但這種方式是我的一個約束。
不要在__layoutSubviews__中進行初始化,即使在方向改變時也會多次調用它 –