2013-07-15 5 views
0

이 메서드에서 textfield를 가져 와서 textfieldshouldreturn을 호출 할 수 있습니까? 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 파일이 있습니다. 나는 다른 메소드에서 텍스트 필드를 위해 작업하면서 구현했다.

-(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의 건강한 사용을 방해합니다. keyboard in UIActionSheet does not type anything

기본적으로 모달 텍스트 입력보기 양식에 대한 UIAlertView를 사용하는 것이 좋습니다 ( please see this link) 사용자 지정보기 모방 :

SO가 사용자 지정 솔루션을 사용하는 것이 낫다는 것을 발견 된 곳 대답이를 참조하십시오 UIActionSheet (맞춤 검색을위한 더 많은 공간 (예 : github as a starting point 검색))가 있지만 UIActionSheet에도 하나의 솔루션이 있습니다.UIActionSheet 강철은 UIActionSheet이 코드를 표시 한 후 사용 진행시키고하지 않는 뷰 계층 구조에서의 UITextField를 배치하기 위해

:

[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

나는 지금 당신의 코드를 시험해보고 해결책을 찾았다. UI를 푸시하지 않고 강제로 밀어 넣기를 시도하기 위해 뷰 계층 구조에 texfield를 배치하려면 uialerview를 표시 한 후에해야합니다. \t UIWindow * appWindow = [UIApplication sharedApplication] .keyWindow; \t [appWindow insertSubview : addToNewList aboveSubview : listActionSheet]; – nzs

0

당신이 UITextFieldDelegate와 호환되도록 각각의 클래스를 활성화 한 다음은

#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 방법입니다?

현재 클래스의 .h 파일을 이동하여 UITextFieldDelegate와 호환되도록 만드십시오.

예 : 당신의 클래스 이름은 당신이 유사한 경고 무언가를 얻고있는 가정입니다 HomeController

경우 :

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

해결 방법 : 모든 경우의

@interface HomeController : UIViewController <UITextFieldDelegate> 
    // YOur Class Variable. 
@end 
0

우선 클래스가 필요한 메소드가있는 프로토콜을 따르지 않으면 클래스를 설정하는 행에 경고가 생성됩니다. 클래스가되도록 위임하십시오.

Nwo, 사용자 지정 클래스를 UITextField의 대리자로 만들려면이를 선언하고 필요한 메서드를 구현해야합니다. 귀하의 경우에는 다음을 수행해야합니다 : 에서 YourCustomView.m을

에서 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 

@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

@ Obliquely true, thx, 나는 대답을 편집했다. (속도가 원인이었습니다) – danypata

0

귀하의 UIView는 위임 속성을 필요로하지 않는다. UITextField delegate methods을 구현하면됩니다. 그것들은 모두 선택 사항이므로 컴파일러는 구현하지 않으면 불평하지 않습니다.

코드가 정상적으로 보입니다. 하지만 상황이 제대로 연결되었는지 확인하려면 textFieldDidBeginEditing:을 UIView 하위 클래스에 구현하여 로그를 생성하고 텍스트 필드를 선택할 때 로그가 나타나는지 확인하십시오. 그 다음 textFieldShouldReturn에 로그인 잘 작동하는 경우

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

: 문제가 해결되지 않는 경우 등, 무언가가 틀려서 다른 곳에서왔다. 게시 한 코드는 빠르게 보입니다.

관련 문제