问题
I have a strange situation in one view controller where this line crashes in the iOS 5 simulator:
// myTextField is created in a NIB
myTextField.borderStyle = UITextBorderStyleNone;
It's okay in the device (and both device and simulator on iOS 4.3). I've checked the connections in the NIB (even deleted and reconnected). myTextField has a retainCount of 2 at this point in the code. I'm setting other attributes (e.g., "text" and "userInteractionEnabled" prior to this point, and those do not cause a crash.
However, if I use this sequence, it does not crash:
myTextField.borderStyle = 4; // not a defined border style
myTextField.borderStyle = UITextBorderStyleNone;
If I use 1, 2, or 3 (defined styles) instead of "4" (an undefined border style), it crashes. "7" works.
In my other view controllers, I have similar textViews, and no problem setting the borderStyle to UITextBorderStyleNone.
Edit: Here's the backtrace:
Thread 1, Queue : (null)
#0 0x01e0609b in objc_msgSend ()
#1 0x005c1c22 in -[UIView(Hierarchy) _setBackgroundColor:] ()
#2 0x005c3a06 in -[UIView(Rendering) setBackgroundColor:] ()
#3 0x0063eab7 in -[UITextField setBackgroundColor:] ()
#4 0x0063e1b6 in -[UITextField setBorderStyle:] ()
#5 0x000c23e6 in -[DutyEditViewController viewWillAppear:] at /Users/jeff/Applications/iPhone/MyApp/Classes/DutyEditViewController.m:197
#6 0x00651fbf in -[UIViewController _setViewAppearState:isAnimating:] ()
#7 0x0065221b in -[UIViewController __viewWillAppear:] ()
#8 0x006524c3 in -[UIViewController beginAppearanceTransition:animated:] ()
#9 0x00662b71 in -[UINavigationController _startTransition:fromViewController:toViewController:] ()
#10 0x006633df in -[UINavigationController _startDeferredTransitionIfNeeded] ()
#11 0x00663986 in -[UINavigationController pushViewController:transition:forceImmediate:] ()
#12 0x0c894dbd in -[UINavigationControllerAccessibility(SafeCategory) pushViewController:transition:forceImmediate:] ()
#13 0x006635a4 in -[UINavigationController pushViewController:animated:] ()
#14 0x000bf99a in -[DutiesTableViewController tableView:didSelectRowAtIndexPath:] ()
#15 0x0061a71d in -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] ()
#16 0x0061a952 in -[UITableView _userSelectRowAtPendingSelectionIndexPath:] ()
#17 0x0025386d in __NSFireDelayedPerform ()
#18 0x020dc966 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ ()
#19 0x020dc407 in __CFRunLoopDoTimer ()
#20 0x0203f7c0 in __CFRunLoopRun ()
#21 0x0203edb4 in CFRunLoopRunSpecific ()
#22 0x0203eccb in CFRunLoopRunInMode ()
#23 0x0258a879 in GSEventRunModal ()
#24 0x0258a93e in GSEventRun ()
#25 0x0058aa9b in UIApplicationMain ()
#26 0x0006117d in main ()
#27 0x00002c65 in start ()
I notice that the setBorderStyle: is doing a setBackgroundColor:, so changed my code to this:
myTextField.backgroundColor = [UIColor clearColor];
myTextField.borderStyle = UITextBorderStyleNone;
and the crash no longer happens. But, the background color is being set to "Clear Color" in the NIB.
So, the crash is gone, but the mystery of why I must set the background color (even though the NIB is already doing that) remains.
回答1:
Solved. It's an iOS 5.0 bug, and it's in iOS 5 on devices, too, not just the simulator. A crash will occur when setting the UITextField borderStyle property if the backgroundColor has been set using UIColor's colorWithRed:green:blue:alpha:.
There is no crash if the text field's backgroundColor has been set using a preset color, e.g., clearColor. There is no crash if borderStyle is set to its current value.
I've submitted a but report to Apple (Bug ID 10381834).
回答2:
First, the retainCount
is irrelevant; meaningless, even.
Secondly, if you have a crash, then you have a backtrace and/or crash report. Post it.
Finally, if it crashes in simulator but not on the device it might be a simulator bug. Or not. Could be a bug in your app that only fails catastrophically in the simulator.
Can't say more without more clues.
来源:https://stackoverflow.com/questions/7783330/uitextview-setborderstyle-crashes-in-ios-5-simulator