2013-07-15 100 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:等,如果還是不行,那麼事情已經歪在其他地方。你發佈的代碼看起來很快就可以看到。