【问题标题】:Crash attempting to set delegate of textfield尝试设置文本字段的委托时崩溃
【发布时间】:2014-01-19 00:33:43
【问题描述】:

我有很多文本字段,我试图让它们在使用返回键时关闭。为了使其工作,您需要将每个文本字段的委托设置为 self,例如 [textfield setDelegate: self];。我的项目中有超过 50 个文本字段,为了使它们全部消失,我必须为每个文本字段复制该行代码。在下面的示例中,我使用了一个 for 循环来尝试缩小它,但是当我尝试时我的项目崩溃并给了我这个错误。谁能告诉我我做错了什么以及如何解决这个问题?

 //.h 
    @interface InsertScheduleCGPS : UIViewController <UITextFieldDelegate>{
        NSArray *Dayh;
        IBOutlet UITextField *Day11;
    }
    @property(nonatomic, assign) id<UITextFieldDelegate> delegate;
    @property (nonatomic,strong) NSArray *Dayh;

.

//.m
- (void)viewDidLoad
{

    [super viewDidLoad];

    Dayh = [NSArray arrayWithObjects:@"Day11", nil];

    NSLog(@"euf");

    for(int i=0; i<[self.Dayh count]; i++) {
      NSLog(@"dd%@",[self.Dayh objectAtIndex:i]);
       [[self.Dayh objectAtIndex:i] setDelegate: self];
}




 - (BOOL)textFieldShouldReturn:(UITextField *)Day11 {
        [[self view] endEditing:YES];



        return NO;
    }

错误:

    2014-01-18 19:15:26.712 Swepple[64912:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString setDelegate:]: unrecognized selector sent to instance 0x144ec'
*** First throw call stack:
(
    0   CoreFoundation                      0x0183b5e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x015be8b6 objc_exception_throw + 44
    2   CoreFoundation                      0x018d8903 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
    3   CoreFoundation                      0x0182b90b ___forwarding___ + 1019
    4   CoreFoundation                      0x0182b4ee _CF_forwarding_prep_0 + 14
    5   Swepple                             0x0000bbd6 -[InsertScheduleCGPS viewDidLoad] + 4262
    6   UIKit                               0x00440318 -[UIViewController loadViewIfRequired] + 696
    7   UIKit                               0x004405b4 -[UIViewController view] + 35
    8   UIKit                               0x0044f361 -[UIViewController viewControllerForRotation] + 63
    9   UIKit                               0x00446f00 -[UIViewController _visibleView] + 84
    10  UIKit                               0x006d511a -[UIWindowController transition:fromViewController:toViewController:target:didEndSelector:animation:] + 5199
    11  UIKit                               0x0044c0fc -[UIViewController presentViewController:withTransition:completion:] + 6433
    12  UIKit                               0x0044c61f -[UIViewController presentViewController:animated:completion:] + 130
    13  UIKit                               0x0044c65f -[UIViewController presentModalViewController:animated:] + 56
    14  UIKit                               0x00870e16 -[UIStoryboardModalSegue perform] + 271
    15  UIKit                               0x0086107e -[UIStoryboardSegueTemplate _perform:] + 174
    16  UIKit                               0x00442280 -[UIViewController performSegueWithIdentifier:sender:] + 72
    17  Swepple                             0x000052d4 -[SecondViewController insert:] + 244
    18  libobjc.A.dylib                     0x015d0874 -[NSObject performSelector:withObject:withObject:] + 77
    19  UIKit                               0x0032e0c2 -[UIApplication sendAction:to:from:forEvent:] + 108
    20  UIKit                               0x0032e04e -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
    21  UIKit                               0x004260c1 -[UIControl sendAction:to:forEvent:] + 66
    22  UIKit                               0x00426484 -[UIControl _sendActionsForEvents:withEvent:] + 577
    23  UIKit                               0x00425733 -[UIControl touchesEnded:withEvent:] + 641
    24  UIKit                               0x0036b51d -[UIWindow _sendTouchesForEvent:] + 852
    25  UIKit                               0x0036c184 -[UIWindow sendEvent:] + 1232
    26  UIKit                               0x0033fe86 -[UIApplication sendEvent:] + 242
    27  UIKit                               0x0032a18f _UIApplicationHandleEventQueue + 11421
    28  CoreFoundation                      0x017c483f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
    29  CoreFoundation                      0x017c41cb __CFRunLoopDoSources0 + 235
    30  CoreFoundation                      0x017e129e __CFRunLoopRun + 910
    31  CoreFoundation                      0x017e0ac3 CFRunLoopRunSpecific + 467
    32  CoreFoundation                      0x017e08db CFRunLoopRunInMode + 123
    33  GraphicsServices                    0x037e09e2 GSEventRunModal + 192
    34  GraphicsServices                    0x037e0809 GSEventRun + 104
    35  UIKit                               0x0032cd3b UIApplicationMain + 1225
    36  Swepple                             0x0000e04d main + 141
    37  libdyld.dylib                       0x01d7c70d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

【问题讨论】:

  • 我看不到您将委托分配给文本字段的位置。您有一个包含一个 string (“Day11”)的数组,然后尝试将委托分配给该字符串。这导致了异常。
  • 我正在尝试使它等于这行代码[Day11 setDelegate: self];。我试图做的事情是不可能的吗?

标签: ios objective-c


【解决方案1】:

你可能是说

Dayh = [NSArray arrayWithObjects:Day11, nil];

目前,Dayh 是一个包含字符串“Day11”的数组,而不是文本字段

【讨论】:

  • 非常感谢!我刚刚花了最后 2 个小时试图弄清楚这一点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多