2012-12-23 39 views
1

我有一個自定義類這樣的:textFieldShouldBeginEditing不會被調用的自定義類(代表集)

@interface formParser : NSObject <UITextFieldDelegate> { 
.... 

,並在.M我創建這樣一個的UITextField元素:

UITextField *ui = [[UITextField alloc] initWithFrame:CGRectMake(left, top, width, height)]; 
[ui setDelegate:self]; 
[ui setPlaceholder:[dict_elementInfo objectForKey:@"placeholder"]]; 
[ui setBorderStyle:UITextBorderStyleLine]; 
[view addSubview:ui]; 

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField 
{ 
NSLog(@"should begin"); 
return NO; 
} 

我的問題是shouldbegin永遠不會被調用。當我在「正常」的UIViewController類上嘗試這種技術時,它完美地工作,但是在我的自定義對象中這樣做,它永遠不會調用它。任何人都可以找出原因嗎?

我的自定義類被稱爲如下:

formParser *fParse = [[formParser alloc] init]; 
UIView *view_formBackground = [fParse viewOfPlist:@"form" initSize:CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height)]; 
view_formBackground.backgroundColor = [UIColor whiteColor]; 


//add views to main view 
[scrollView addSubview:view_formBackground]; 
[self.view addSubview:scrollView]; 

此外,在formparser.m的viewofplist如下:

-(UIView *)viewOfPlist:(NSString *)filename initSize:(CGRect)size 
{ 
ypos_element_left = 40; ypos_element_right = 40; 

view = [[UIView alloc] initWithFrame:size]; 

//load plist 
NSString *path = [[NSBundle mainBundle] pathForResource:filename ofType:@"plist"]; 
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path]; 
rootArray = [dict objectForKey:@"form"]; 

//loop door alle UI entries in de dict. 
for (NSDictionary *dict_UIElement in rootArray) 
{ 
    NSString *UIType = [dict_UIElement objectForKey:@"type"]; 
    if ([UIType isEqualToString:@"ui_empty"])  [self handle_uiempty:dict_UIElement]; 
    if ([UIType isEqualToString:@"ui_multiselect"]) [self handle_uimultiselect:dict_UIElement]; 
    if ([UIType isEqualToString:@"ui_label"])  [self handle_uilabel:dict_UIElement]; 
    if ([UIType isEqualToString:@"ui_textfield"]) [self handle_uitextfield:dict_UIElement]; 
    if ([UIType isEqualToString:@"ui_choicefield"]) [self handle_uichoicefield:dict_UIElement]; 
    if ([UIType isEqualToString:@"ui_calendar"]) [self handle_uicalendar:dict_UIElement]; 

} 


return (view); 

}

謝謝回答!

+0

當你調用'有用的鏈接[查看addSubview:界面];',什麼是'view' ......,什麼方法是裏面? (另外,不帶'init'調用'alloc'通常是個好主意。) –

+0

「view」是在我自己的「viewofPlist」例程formparser.m中創建的一個uiview。它返回在上面的例程中的視圖... 我已經改變了我的分配例程:formParser * fParse = [[FormParser alloc] init]但無濟於事......但是,謝謝你的提示;) –

+0

編輯我的問題給你在formparser.m例程 –

回答

1

您的撥款是否超出範圍並被ARC清理?

如何響應鏈的作品..

http://developer.apple.com/library/ios/#documentation/general/conceptual/Devpedia-CocoaApp/Responder.html

+0

感謝您的網址。我已經從NSObject繼承更改formparser UIView並添加canBecomeFirstResponder ..但它仍然無法正常工作。 奇怪的事實:50%的時間在控件上的任何事件被觸發得到一個EXC_BAD_ACCESS在調試器(崩潰) –

+1

是你的分配超出範圍和ARC清理? – estobbart

+0

你可能想要做的是不要成爲一個委託,你可以設置你自己的動作和目標的對象。在formParser.m .. [ui setAction:@selector(myOwnAction :)]; [ui setTarget:self]; – estobbart

相關問題