2010-05-31 81 views
2

我是iPhone編程的新手。我有一個問題。我需要創建一個自定義用戶控件,我將添加到我的UIScrollView dinamically。該控件有一個UITextField和一個UIButton。請參見下面的代碼:iPhone應用程序。創建一個包含UITextField和UIButton的自定義UIView

#import <UIKit/UIKit.h> 
@interface FieldWithValueControl : UIView { 
    UITextField *txtTagName; 
    UIButton *addButton; 
} 
@property (nonatomic, readonly) UITextField *txtTagName; 
@property (nonatomic, readonly) UIButton *addButton; 
@end 

#import "FieldWithValueControl.h" 
#define ITEM_SPACING 10 
#define ITEM_HEIGHT 20 
#define SWITCHBOX_WIDTH 100 
#define SCREEN_WIDTH 320 
#define ITEM_FONT_SIZE 14 
#define TEXTBOX_WIDTH 150 
@implementation FieldWithValueControl 

@synthesize txtTagName; 
@synthesize addButton; 

- (id)initWithFrame:(CGRect)frame { 
    if (self = [super initWithFrame:frame]) { 
     // Initialization code 
     txtTagName = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, TEXTBOX_WIDTH, ITEM_HEIGHT)]; 
     txtTagName.borderStyle = UITextBorderStyleRoundedRect; 

     addButton = [UIButton buttonWithType:UIButtonTypeContactAdd]; 
     [addButton setFrame:CGRectMake(ITEM_SPACING + TEXTBOX_WIDTH, 0, ITEM_HEIGHT, ITEM_HEIGHT)]; 
     [addButton addTarget:self action:@selector(addButtonTouched:) 
      forControlEvents:UIControlEventTouchUpInside]; 
      [self addSubview:txtTagName]; 
     [self addSubview:addButton]; 
    } 
    return self; 
} 
- (void)addButtonTouched:sender 
{ 
    UIButton *button = (UIButton*)sender; 
    NSString *title = [button titleLabel].text; 
} 
- (void)drawRect:(CGRect)rect { 
    // Drawing code 
} 
- (void)dealloc { 
    [txtTagName release]; 
    [addButton release]; 
     [super dealloc]; 
} 
@end 

在我的代碼創建一個類的對象,並將其添加到滾動視圖的形式。

FieldWithValueControl *newTagControl = (FieldWithValueControl*)[[FieldWithValueControl alloc] initWithFrame:CGRectMake(ITEM_SPACING, currentOffset + ITEM_SPACING, 0, 0)]; 
[scrollView addSubview:newTagControl]; 

該控件看起來不錯,但如果我點擊文本框或按鈕沒有任何反應。鍵盤不出現,按鈕不可點擊等。

回答

-1

嘗試將delegate設置爲self

更多的則0
0

設置widthheight值的控制:

FieldWithValueControl *newTagControl = [[FieldWithValueControl alloc] initWithFrame: 
    CGRectMake(ITEM_SPACING, currentOffset + ITEM_SPACING, 100, 200)];