2013-07-15 99 views
0

如何获取此方法中的textfield以调用textfieldshould返回? 我猜它不工作,因为UIView没有一个委托属性,但我不知道如何使一个UIView在一个方法中创建符合协议。 我见过几个关于代表团的帖子,但我试过的一切都没有奏效。 我确定它很简单。 在此先感谢。TextFieldShoudReturn未被调用

- (void) launchCreateNewListActionSheet{ 
    self.listActionSheet= [[UIActionSheet alloc] initWithTitle:@"Create A New List" 
                delegate:self 
             cancelButtonTitle:nil 
            destructiveButtonTitle:nil 
             otherButtonTitles:nil]; 

    UIView *addToNewList = [[UIView alloc] initWithFrame:CGRectMake(0, 44, 320, 464)]; 
    addToNewList.backgroundColor = [UIColor whiteColor]; 

    UILabel *addToNewListLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 23, 100, 20)]; 
    addToNewListLabel.text = @"List Name: "; 

    UITextField *enterNewListName = [[UITextField alloc] initWithFrame:CGRectMake(100, 20, 180, 30)]; 
    enterNewListName.layer.borderWidth = 1; 
    enterNewListName.layer.borderColor = [[UIColor blackColor] CGColor]; 
    enterNewListName.delegate = self; 

    [addToNewList addSubview:addToNewListLabel]; 
    [addToNewList addSubview:enterNewListName]; 


    UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc]  initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(addToExistingList)]; 

    UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelListPicker)]; 

    UIBarButtonItem *fixedCenter = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; 
fixedCenter.width = 180.0f; 

    UIToolbar* toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; 
    toolbar.barStyle = UIBarStyleBlackOpaque; 
    [toolbar sizeToFit]; 

    [toolbar setItems:@[cancelBtn, fixedCenter, doneBtn] animated:YES]; 

    [self.listActionSheet addSubview:toolbar]; 
    [self.listActionSheet addSubview:addToNewList]; 
    [self.listActionSheet showInView:self.view]; 
    [self.listActionSheet setBounds:CGRectMake(0,0,320, 464)]; 
} 

这是我的.h文件。我已经实现了,因为它在其他方法的文本字段中工作。

#import <UIKit/UIKit.h> 
#import <Foundation/Foundation.h> 
#import "ZBarSDK.h" 
#import "ProgressView.h" 

@class ScannerViewController; 

@protocol ScanViewDelegate <NSObject>; 
@required 
-(void)postURL:(NSURL*)url selectedAction:(NSString*)action; 
@end 

@interface ScannerViewController : UIViewController <ZBarReaderViewDelegate,  UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate, UIWebViewDelegate, UIPickerViewDelegate, UIActionSheetDelegate> 

    id <ScanViewDelegate> delegate; 
} 
@property (weak, nonatomic) IBOutlet ZBarReaderView *readerView; 
@property (weak, nonatomic) IBOutlet UITableView *scannerList; 
@property (retain) id delegate; 

@property (strong) NSMutableArray* barCodeArray; 
@property (strong) NSMutableArray* quantityArray; 
@property (strong) NSURL* url; 
@property (strong) NSString* action; 
@property NSString *listItems; 
@property NSString *listItemNames; 
@property NSArray *listItemNamesArray; 
@property NSArray *hrefArray; 
@property int pickerViewRow; 
@property (strong) UIWebView *webView; 
@property (strong) ProgressView* loading; 
@property BOOL isLoading; 
@property (strong) UITextField *enterNewListName; 
@property (strong) UITextField *addToNewListDescription; 
@property (strong) NSMutableArray *textFieldArray; 

-(IBAction)sendToCart:(id)sender; 
-(IBAction)sendToList:(id)sender; 
-(void)startLoadingURL; 
@end 

这里是textfieldshouldreturn方法:

-(BOOL)textFieldShouldReturn:(UITextField *)textField 
{ 
    if([self.textFieldArray containsObject:textField]) 
    { 
     NSUInteger index = [self.textFieldArray indexOfObject:textField]; 
     NSNumber *newQuantity = [NSNumber numberWithInteger:[textField.text integerValue]]; 
     [self.quantityArray replaceObjectAtIndex:index withObject:newQuantity]; 
     NSLog(@"Your new quantity is: %@", newQuantity); 
    } 
    [textField resignFirstResponder]; 
    return YES; 
} 
+0

您是否使用断点来查看TextFieldShoudReturn方法是否未被调用? –

+0

它的NSlog检查它是否被调用,而不是。 –

+0

你可以编辑TextFieldShoudReturn方法到你的问题? –

回答

2

这里的关键是Andres想在UIActionSheet上使用UiTextField。 UIActionSheet有轻微不同的行为吃水龙头,实际上它阻止了UiTextField健康的使用添加到行动表。

请参阅本SO回答它被发现,使用自定义的解决方案是更好的: keyboard in UIActionSheet does not type anything

基本上它是更好地使用 UIAlertView中的一个模式的文本输入视图形式( please see this link自定义视图模仿UIActionSheet有更多的空间用于定制(例如搜索github as a starting point),但UIActionSheet也有一个解决方案。

为了放置的UITextField在UIActionSheet不钢推压显示UIActionSheet此代码段后使用视图层次:

[self.listActionSheet addSubview:toolbar]; 
    // [self.listActionSheet addSubview:addToNewList]; // please comment-delete this line 
    [self.listActionSheet showInView:self.view]; 
    [self.listActionSheet setBounds:CGRectMake(0,0,320, 464)]; 
    // this is the 2 new lines below 
    UIWindow *appWindow = [UIApplication sharedApplication].keyWindow; 
    [appWindow insertSubview:addToNewList aboveSubview:listActionSheet]; // you add custom view here 
+0

所以 - 海报的问题 - 代码是否生成实际接受选择/成为第一响应者的UITextField。如果不是,那么它不会给它的代表发送任何消息并不奇怪。可以原创的海报评论。 – Obliquely

+0

嗯......这可能是问题所在。那么你的意思是我的enterNewListName.becomeFirstResponder = YES?不,我没有那样做。我只是试了一下,它告诉我没有setBecomeFirstResponder的setter方法来分配属性。 –

+0

我现在试过你的代码并找到了解决方案。为了将你的texfield放置在视图层次结构中,以便强制uiactionsheet不会被偷,你必须在显示uialerview之后这么做:\t UIWindow * appWindow = [UIApplication sharedApplication] .keyWindow; \t [appWindow insertSubview:addToNewList aboveSubview:listActionSheet]; – nzs

0

是否启用了相应的类与UITextFieldDelegate兼容?

请转到当前类的.h文件,并将其与UITextFieldDelegate兼容。

例如:如果你的类名是HomeController

我假设你得到一个类似的警告的东西:

Assigning to id<UITextFieldDelegate> from incompatible type "HomeController" 

解决方案:

@interface HomeController : UIViewController <UITextFieldDelegate> 
    // YOur Class Variable. 
@end 
0

首先要是您的课程不符合具有所需方法的协议,它将在设置该课程的线路上生成警告代表是班级。如果你想让自定义类成为UITextField的委托,你必须声明并实现所需的方法。你的情况,你必须做到以下几点:

YourCustomView.h

@interface YourCustomView : UIView (or other class) <UITextFieldDelegate> // this will show that this class conforms to UITextFieldDelegate protocol 
//other methods or properties here 
@end 

YourCustomView.m

@implementation YourCustomView 

-(void)launchCreateNewListActionSheet { 
    //some code here 
    UITextField *textField = //alloc init methd 
    textField.delegate = self; 
} 

#pragma mark - UITextFieldDelegate //this is optional only if you want to structure the code 
- (BOOL)textFieldShouldReturn:(UITextField *)textField { 
    //method called when the text field resign first responder 
} 

//你可以添加更多地依赖委托方法根据您的需求。 @end

+0

UITextField委托协议中的所有方法都是可选的,因此您不会收到编译器投诉。 – Obliquely

+0

@明显地真实,thx,我编辑了答案。 (速度是原因) – danypata

0

你的UIView不需要委托财产。它只需要执行UITextField delegate methods。它们都是可选的,所以如果你没有实现它们,编译器不会抱怨。

您的代码看起来不错。但是要查看是否正确连接了UIView子类中的textFieldDidBeginEditing:以生成日志并查看在选择文本字段时是否出现日志。

- (void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
    NSLog(@"textFieldDidBeginEditing: %@ (delegate: %@)", textField, [textField delegate]); 
} 

如果工作正常,然后登录textFieldShouldReturn:等,如果还是不行,那么事情已经歪在其他地方。你发布的代码看起来很快就可以看到。