由于UITextField文本输入程序,我的应用程序崩溃了

时间:2015-11-13 15:23:23

标签: objective-c

用户可以点击下一个UITextField输入数据,也可以触摸键盘上的下一个按钮。 我已经使用textFieldShouldReturn方法将文本对象添加到数组中。它工作得很好,直到我触摸下一个UITextfield输入数据而不是在键盘上接下来然后因为没有数据添加到我的数组,稍后在segue中,UITableViewController崩溃。

我是否需要实施这两种方法,或者在这里出错了。

ModalViewController.m

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds for empty array'

*** First throw call stack:
(
    0   CoreFoundation                      0x000000011089df65 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x0000000110315deb objc_exception_throw + 48
    2   CoreFoundation                      0x0000000110781a94 -[__NSArrayM objectAtIndex:] + 212
    3   BookApp                             0x000000010fe0722d -[AddBooksTableViewController tableView:cellForRowAtIndexPath:] + 413
    4   UIKit                               0x0000000110fc06b3 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 782
    5   UIKit                               0x0000000110fc07c8 -[UITableView _createPreparedCellForGlobalRow:willDisplay:] + 74
    6   UIKit                               0x0000000110f96650 -[UITableView _updateVisibleCellsNow:isRecursive:] + 3187
    7   UIKit                               0x0000000110fc9595 -[UITableView _performWithCachedTraitCollection:] + 92
    8   UIKit                               0x0000000110fb19ad -[UITableView layoutSubviews] + 218
    9   UIKit                               0x0000000110f2211c -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 710
    10  QuartzCore                          0x000000011547736a -[CALayer layoutSublayers] + 146
    11  QuartzCore                          0x000000011546bbd0 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
    12  QuartzCore                          0x000000011546ba4e _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
    13  QuartzCore                          0x00000001154601d5 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277
    14  QuartzCore                          0x000000011548d9f0 _ZN2CA11Transaction6commitEv + 508
    15  QuartzCore                          0x000000011548e154 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92
    16  CoreFoundation                      0x00000001107c99d7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
    17  CoreFoundation                      0x00000001107c9947 __CFRunLoopDoObservers + 391
    18  CoreFoundation                      0x00000001107bf59b __CFRunLoopRun + 1147
    19  CoreFoundation                      0x00000001107bee98 CFRunLoopRunSpecific + 488
    20  GraphicsServices                    0x0000000114f2aad2 GSEventRunModal + 161
    21  UIKit                               0x0000000110e71676 UIApplicationMain + 171
    22  BookApp                             0x000000010fe082cf main + 111
    23  libdyld.dylib                       0x000000011316792d start + 1
    24  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

崩溃日志

{{1}}

1 个答案:

答案 0 :(得分:0)

在textField中向数组添加对象后,同时填充了shouldChangeCharactersInRange方法数组,一切正常。

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {


    if (textField == self.bookTextField) {
        [arrayToReturn insertObject:[[self.bookTextField text] stringByReplacingCharactersInRange:range withString:string] atIndex:0];
    }else if (textField == self.pageTextField){
        [arrayToReturn insertObject:[[self.pageTextField text] stringByReplacingCharactersInRange:range withString:string] atIndex:1];
    }


     return YES;
}