gpt4 book ai didi

ios - 未调用 TextFieldShouldReturn

转载 作者:行者123 更新时间:2023-11-28 22:30:13 25 4
gpt4 key购买 nike

如何在这个方法中获取textfield来调用textfieldshouldreturn?我猜它不起作用,因为 UIView 没有委托(delegate)属性,但我不确定如何使我在方法中创建的 UIView 符合协议(protocol)。我看过几篇关于授权的帖子,但我尝试过的一切都没有奏效。我确定它很简单。提前致谢。

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

最佳答案

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

请在发现使用自定义解决方案更好的地方查看此 SO 答案: keyboard in UIActionSheet does not type anything

基本上,最好将 UIAlertView 用于模态文本输入 View 表单 ( please see this link) 一个自定义 View ,它模仿 UIActionSheet,具有更多自定义空间(搜索例如 github as a starting point),但是有一个UIActionSheet 的解决方案也是如此。

为了将 UITextField 放置在 UIActionSheet 不钢推的 View 层次结构中,在显示 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

关于ios - 未调用 TextFieldShouldReturn,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17660222/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com