如何在此方法中获取文本字段来调用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;
}
答案 0 :(得分: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后显示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
答案 1 :(得分:0)
您是否已启用相应的课程与UITextFieldDelegate
兼容?
请转到当前类的.h文件,并使其与UITextFieldDelegate兼容。
例如:如果您的ClassName是HomeController
我假设您收到类似的警告:
Assigning to id<UITextFieldDelegate> from incompatible type "HomeController"
<强>解决方案:强>
@interface HomeController : UIViewController <UITextFieldDelegate>
// YOur Class Variable.
@end
答案 2 :(得分:0)
首先,如果您的类不符合具有所需方法的协议,则会在将委托设置为类的行中生成警告。
Nwo,如果你想让自定义类成为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
答案 3 :(得分:0)
您的UIView不需要委托属性。它只需要实现UITextField delegate methods。它们都是可选的,因此如果你不实现它们,编译器就不会抱怨。
您的代码看起来不错。但是要查看是否正确连接,请在UIView子类中实现textFieldDidBeginEditing:
以生成日志,并在选择文本字段时查看日志是否出现。
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
NSLog(@"textFieldDidBeginEditing: %@ (delegate: %@)", textField, [textField delegate]);
}
如果工作正常,那么登录textFieldShouldReturn:等等。如果这不起作用,那么其他地方就出现了问题。您发布的代码在快速浏览时看起来不错。