このメソッドで textfield を取得して 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;
}