0

如何在此方法中获取文本字段以调用 textfieldshouldreturn?我猜它不起作用,因为 UIView 没有委托属性,但我不确定如何使我在方法中创建的 UIView 符合协议。我看过几篇关于委派的帖子,但我尝试过的一切都没有奏效。我敢肯定它很简单。提前致谢。

- (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;
}
4

4 回答 4

2

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

请参阅此 SO 答案,发现使用自定义解决方案更好: UIActionSheet 中的键盘不输入任何内容

基本上,最好将UIAlertView 用于模态文本输入视图表单(请参阅此链接模仿 UIActionSheet 的自定义视图,具有更多自定义空间(搜索例如github 作为起点),但 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
于 2013-07-15T17:44:26.323 回答
0

您是否启用了相应的类以兼容UITextFieldDelegate

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

例如:如果您的 ClassName 是HomeController

我假设您收到类似于以下内容的警告:

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

解决方案:

@interface HomeController : UIViewController <UITextFieldDelegate>
    // YOur Class Variable.
@end
于 2013-07-15T17:37:17.827 回答
0

First of all if your class doesn't conform to a protocol that has methods required it will generate a warning at the line that sets the delegate to be the class.

Nwo, if you want to make a custom class a delegate of a UITextField you will have to declare that and implement the required method. In your case you have to do the following:

In 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

In 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
}

//and you can add more delegate methods depending on your needs. @end

于 2013-07-15T17:39:25.043 回答
0

您的 UIView 不需要委托属性。它只需要实现UITextField 委托方法。它们都是可选的,所以如果你不实现它们,编译器不会抱怨。

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

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

如果这工作正常,然后登录 textFieldShouldReturn: 等。如果这不起作用,那么其他地方出了问题。您发布的代码一目了然。

于 2013-07-15T17:45:59.040 回答