Why does [NSTextStorage setAttributedString] crash with NSMutableAttributedString? - objective-c

When I run the following Code it crashes at the last line. I don't have any idea why. This function gets called in awakeFromNib.
- (void)setMotdText:(NSString *)text
{
NSString *boldFontName = [[NSFont boldSystemFontOfSize:12] fontName];
NSMutableAttributedString *attrstr = [[NSMutableAttributedString alloc] initWithString:text];
[attrstr beginEditing];
[attrstr addAttribute:NSFontAttributeName value:boldFontName range:NSMakeRange(0, 16)];
[attrstr endEditing];
[self.motdTextView.textStorage setAttributedString:attrstr];
}
I get this crash log:
2012-06-22 11:32:22.348 MSM-Plan[20785:403] -[__NSCFConstantString _isDefaultFace]: unrecognized selector sent to instance 0x7fff79b9f380
2012-06-22 11:32:22.349 MSM-Plan[20785:403] An uncaught exception was raised
2012-06-22 11:32:22.349 MSM-Plan[20785:403] -[__NSCFConstantString _isDefaultFace]: unrecognized selector sent to instance 0x7fff79b9f380
2012-06-22 11:32:22.351 MSM-Plan[20785:403] (
0 CoreFoundation 0x00007fff8a5bef56 __exceptionPreprocess + 198
1 libobjc.A.dylib 0x00007fff8ae19d5e objc_exception_throw + 43
2 CoreFoundation 0x00007fff8a64b1be -[NSObject doesNotRecognizeSelector:] + 190
3 CoreFoundation 0x00007fff8a5abe23 ___forwarding___ + 371
4 CoreFoundation 0x00007fff8a5abc38 _CF_forwarding_prep_0 + 232
5 AppKit 0x00007fff8e0b060c -[NSMutableAttributedString(NSMutableAttributedStringKitAdditions) fixFontAttributeInRange:] + 1249
6 AppKit 0x00007fff8e0afeb7 -[NSMutableAttributedString(NSMutableAttributedStringKitAdditions) fixAttributesInRange:] + 64
7 AppKit 0x00007fff8e19b521 -[NSTextStorage processEditing] + 107
8 AppKit 0x00007fff8e0c2564 -[NSTextStorage edited:range:changeInLength:] + 385
9 Foundation 0x00007fff8d6f4497 -[NSConcreteMutableAttributedString replaceCharactersInRange:withAttributedString:] + 328
10 AppKit 0x00007fff8e1dfd19 -[NSConcreteTextStorage replaceCharactersInRange:withAttributedString:] + 81
11 MSM-Plan 0x000000010206af5a -[MSM_PlanAppDelegate setMotdText:] + 314
12 MSM-Plan 0x000000010206c455 -[MSM_PlanAppDelegate awakeFromNib] + 1701
13 CoreFoundation 0x00007fff8a5b5fb1 -[NSObject performSelector:] + 49
14 CoreFoundation 0x00007fff8a5b5f32 -[NSSet makeObjectsPerformSelector:] + 274
15 AppKit 0x00007fff8e0369ff -[NSIBObjectData nibInstantiateWithOwner:topLevelObjects:] + 1245
16 AppKit 0x00007fff8e02cf73 loadNib + 322
17 AppKit 0x00007fff8e02c470 +[NSBundle(NSNibLoading) _loadNibFile:nameTable:withZone:ownerBundle:] + 217
18 AppKit 0x00007fff8e02c38b +[NSBundle(NSNibLoading) loadNibFile:externalNameTable:withZone:] + 141
19 AppKit 0x00007fff8e02c2ce +[NSBundle(NSNibLoading) loadNibNamed:owner:] + 364
20 AppKit 0x00007fff8e29d06f NSApplicationMain + 398
21 MSM-Plan 0x000000010206adf2 main + 34
22 MSM-Plan 0x000000010206adc4 start + 52
)
2012-06-22 11:32:22.416 MSM-Plan[20785:403] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString _isDefaultFace]: unrecognized selector sent to instance 0x7fff79b9f380'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff8a5bef56 __exceptionPreprocess + 198
1 libobjc.A.dylib 0x00007fff8ae19d5e objc_exception_throw + 43
2 CoreFoundation 0x00007fff8a64b1be -[NSObject doesNotRecognizeSelector:] + 190
3 CoreFoundation 0x00007fff8a5abe23 ___forwarding___ + 371
4 CoreFoundation 0x00007fff8a5abc38 _CF_forwarding_prep_0 + 232
5 AppKit 0x00007fff8e0b060c -[NSMutableAttributedString(NSMutableAttributedStringKitAdditions) fixFontAttributeInRange:] + 1249
6 AppKit 0x00007fff8e0afeb7 -[NSMutableAttributedString(NSMutableAttributedStringKitAdditions) fixAttributesInRange:] + 64
7 AppKit 0x00007fff8e19b521 -[NSTextStorage processEditing] + 107
8 AppKit 0x00007fff8e0c2564 -[NSTextStorage edited:range:changeInLength:] + 385
9 Foundation 0x00007fff8d6f4497 -[NSConcreteMutableAttributedString replaceCharactersInRange:withAttributedString:] + 328
10 AppKit 0x00007fff8e1dfd19 -[NSConcreteTextStorage replaceCharactersInRange:withAttributedString:] + 81
11 MSM-Plan 0x000000010206af5a -[MSM_PlanAppDelegate setMotdText:] + 314
12 MSM-Plan 0x000000010206c455 -[MSM_PlanAppDelegate awakeFromNib] + 1701
13 CoreFoundation 0x00007fff8a5b5fb1 -[NSObject performSelector:] + 49
14 CoreFoundation 0x00007fff8a5b5f32 -[NSSet makeObjectsPerformSelector:] + 274
15 AppKit 0x00007fff8e0369ff -[NSIBObjectData nibInstantiateWithOwner:topLevelObjects:] + 1245
16 AppKit 0x00007fff8e02cf73 loadNib + 322
17 AppKit 0x00007fff8e02c470 +[NSBundle(NSNibLoading) _loadNibFile:nameTable:withZone:ownerBundle:] + 217
18 AppKit 0x00007fff8e02c38b +[NSBundle(NSNibLoading) loadNibFile:externalNameTable:withZone:] + 141
19 AppKit 0x00007fff8e02c2ce +[NSBundle(NSNibLoading) loadNibNamed:owner:] + 364
20 AppKit 0x00007fff8e29d06f NSApplicationMain + 398
21 MSM-Plan 0x000000010206adf2 main + 34
22 MSM-Plan 0x000000010206adc4 start + 52
)
terminate called throwing an exception(lldb)
Has anybody an idea? If I call it with a NSAttributedString-Object thereĀ“s no error.

I had to change the first line to:
NSFont *boldFont = [NSFont boldSystemFontOfSize:12];
Now it works, because the value attribute requires a NSFont-Object and not a NSString with the fonts name.
This is the working Code:
- (void)setMotdText:(NSString *)text
{
NSFont *boldFont = [NSFont boldSystemFontOfSize:12];
NSMutableAttributedString *attrstr = [[NSMutableAttributedString alloc] initWithString:text];
[attrstr beginEditing];
[attrstr addAttribute:NSFontAttributeName value:boldFont range:NSMakeRange(0, 16)];
[attrstr endEditing];
[self.motdTextView.textStorage setAttributedString:attrstr];
}

Related

issue with UISplitViewController in iOS 8.1: [UISplitViewController navigationBar]: unrecognized selector sent to instance

Im trying to create a New UISPlitViewController and display it instead of current ViewController, but for some reason it's not working, it says:
-[UISplitViewController navigationBar]: unrecognized selector sent to instance
this is the code
- (IBAction)openDocuments:(id)sender {
UINavigationController *leftNavController;
UINavigationController *rightNavController;
MenuDocumentsVC *masterVC = [self.storyboard instantiateViewControllerWithIdentifier:#"MenuDocumentsVC"];
masterVC.title = #" ";
ContentDocumentsVC *detailVC = [self.storyboard instantiateViewControllerWithIdentifier:#"ContentDocumentsVC"];
detailVC.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:LOGO_COMPANY]];
masterVC.delegate = detailVC;
leftNavController = [[UINavigationController alloc] initWithRootViewController:masterVC];
rightNavController = [[UINavigationController alloc] initWithRootViewController:detailVC];
leftNavController.toolbarHidden = FALSE;
rightNavController.toolbarHidden = FALSE;
leftNavController.navigationBar.translucent = FALSE;
rightNavController.navigationBar.translucent = FALSE;
leftNavController.toolbar.translucent = FALSE;
rightNavController.toolbar.translucent = FALSE;
UISplitViewController *splitViewController = [[UISplitViewController alloc] init];
splitViewController.viewControllers = [NSArray arrayWithObjects:leftNavController, rightNavController, nil];
splitViewController.delegate = (id)detailVC;
if ([splitViewController respondsToSelector:#selector(setPresentsWithGesture:)]) {
[splitViewController setPresentsWithGesture:NO];
}
splitViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[UIView transitionWithView:self.view.window
duration:0.5
options:UIViewAnimationOptionCurveEaseOut
animations:^{
self.view.window.rootViewController = splitViewController;
} completion:^(BOOL finished) {
}];
}
how to make it work properly??
Exception
2015-01-05 11:01:47.402 myapp[3454:607] -[UISplitViewController navigationBar]: unrecognized selector sent to instance 0x7b961f00
2015-01-05 11:01:47.406 myapp[3454:607] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UISplitViewController navigationBar]: unrecognized selector sent to instance 0x7b961f00'
*** First throw call stack:
(
0 CoreFoundation 0x010a21e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x00a718e5 objc_exception_throw + 44
2 CoreFoundation 0x0113f243 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
3 CoreFoundation 0x0109250b ___forwarding___ + 1019
4 CoreFoundation 0x010920ee _CF_forwarding_prep_0 + 14
5 eOpenRoad 0x00056d7d +[Functions setNavigationBarControllerToHexColor:] + 333
6 eOpenRoad 0x0004fa15 -[ContentDocumentsVC viewWillAppear:] + 69
7 UIKit 0x0163e04b -[UIViewController _setViewAppearState:isAnimating:] + 448
8 UIKit 0x0163e548 -[UIViewController __viewWillAppear:] + 114
9 UIKit 0x01660c25 -[UINavigationController _startTransition:fromViewController:toViewController:] + 800
10 UIKit 0x016618cc -[UINavigationController _startDeferredTransitionIfNeeded:] + 645
11 UIKit 0x016624e9 -[UINavigationController __viewWillLayoutSubviews] + 57
12 UIKit 0x017a30d1 -[UILayoutContainerView layoutSubviews] + 213
13 UIKit 0x0158a964 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 355
14 libobjc.A.dylib 0x00a8382b -[NSObject performSelector:withObject:] + 70
15 QuartzCore 0x001ea45a -[CALayer layoutSublayers] + 148
16 QuartzCore 0x001de244 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
17 QuartzCore 0x001ea3a5 -[CALayer layoutIfNeeded] + 160
18 UIKit 0x0164cae3 -[UIViewController window:setupWithInterfaceOrientation:] + 304
19 UIKit 0x01562aa7 -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:isRotating:] + 5212
20 UIKit 0x01561646 -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:] + 82
21 UIKit 0x01561518 -[UIWindow _setRotatableViewOrientation:updateStatusBar:duration:force:] + 117
22 UIKit 0x015615a0 -[UIWindow _setRotatableViewOrientation:duration:force:] + 67
23 UIKit 0x0156063a __57-[UIWindow _updateToInterfaceOrientation:duration:force:]_block_invoke + 120
24 UIKit 0x0156059c -[UIWindow _updateToInterfaceOrientation:duration:force:] + 400
25 UIKit 0x015612f3 -[UIWindow setAutorotates:forceUpdateInterfaceOrientation:] + 870
26 UIKit 0x015648e6 -[UIWindow setDelegate:] + 449
27 UIKit 0x0163eb77 -[UIViewController _tryBecomeRootViewControllerInWindow:] + 180
28 UIKit 0x0155a474 -[UIWindow addRootViewControllerViewIfPossible] + 591
29 UIKit 0x0155ac84 -[UIWindow setRootViewController:] + 960
30 eOpenRoad 0x0005c5ec +[Functions openContentInWindowWithParameters:insideOfNavigationController:] + 7804
31 eOpenRoad 0x0001f42b -[ViewController openDocuments:] + 555
32 libobjc.A.dylib 0x00a83880 -[NSObject performSelector:withObject:withObject:] + 77
33 UIKit 0x0151d3b9 -[UIApplication sendAction:to:from:forEvent:] + 108
34 UIKit 0x0151d345 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
35 UIKit 0x0161ebd1 -[UIControl sendAction:to:forEvent:] + 66
36 UIKit 0x0161efc6 -[UIControl _sendActionsForEvents:withEvent:] + 577
37 UIKit 0x0161e243 -[UIControl touchesEnded:withEvent:] + 641
38 UIKit 0x0155cddd -[UIWindow _sendTouchesForEvent:] + 852
39 UIKit 0x0155d9d1 -[UIWindow sendEvent:] + 1117
40 UIKit 0x0152f5f2 -[UIApplication sendEvent:] + 242
41 UIKit 0x01519353 _UIApplicationHandleEventQueue + 11455
42 CoreFoundation 0x0102b77f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
43 CoreFoundation 0x0102b10b __CFRunLoopDoSources0 + 235
44 CoreFoundation 0x010481ae __CFRunLoopRun + 910
45 CoreFoundation 0x010479d3 CFRunLoopRunSpecific + 467
46 CoreFoundation 0x010477eb CFRunLoopRunInMode + 123
47 GraphicsServices 0x03ae75ee GSEventRunModal + 192
48 GraphicsServices 0x03ae742b GSEventRun + 104
49 UIKit 0x0151bf9b UIApplicationMain + 1225
50 eOpenRoad 0x00051e3d main + 141
51 libdyld.dylib 0x036b76d9 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
From provided log I can say that problem lie in [ContentDocumentsVC viewWillAppear:] where you set color to navigation bar with category method setNavigationBarControllerToHexColor: but looks like you call this method on wrong controller.

Call to UIWebView crashing my app in iOS

I have a WebView in my storyboard. I created an outlet property to that webview in my view controller file. Then in my code in I'm calling it like this:
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"news1" ofType:#"html" inDirectory:#"/HTML"]];
[self.webView loadRequest:[NSURLRequest requestWithURL:url]];
}
The problem is my app is crashing when I run this and I don't really know why. I get this error, but I need help deciphering what the issue is.
2014-05-20 20:18:10.214 MyApp[1789:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter'
*** First throw call stack:
(
0 CoreFoundation 0x01f7b1e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x019fe8e5 objc_exception_throw + 44
2 CoreFoundation 0x01f7afbb +[NSException raise:format:] + 139
3 Foundation 0x0161a92b -[NSURL(NSURL) initFileURLWithPath:] + 123
4 Foundation 0x0161a7fd +[NSURL(NSURL) fileURLWithPath:] + 67
5 MyApp 0x00011c3d -[LVGTutorialVC viewDidLoad] + 205
6 UIKit 0x007dd33d -[UIViewController loadViewIfRequired] + 696
7 UIKit 0x00803345 -[UINavigationController _layoutViewController:] + 39
8 UIKit 0x0080385b -[UINavigationController _updateScrollViewFromViewController:toViewController:] + 235
9 UIKit 0x00803953 -[UINavigationController _startTransition:fromViewController:toViewController:] + 78
10 UIKit 0x008048cc -[UINavigationController _startDeferredTransitionIfNeeded:] + 645
11 UIKit 0x008054e9 -[UINavigationController __viewWillLayoutSubviews] + 57
12 UIKit 0x009460d1 -[UILayoutContainerView layoutSubviews] + 213
13 UIKit 0x0072d964 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 355
14 libobjc.A.dylib 0x01a1082b -[NSObject performSelector:withObject:] + 70
15 QuartzCore 0x01de245a -[CALayer layoutSublayers] + 148
16 QuartzCore 0x01dd6244 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
17 QuartzCore 0x01dd60b0 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 26
18 QuartzCore 0x01d3c7fa _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 294
19 QuartzCore 0x01d3db85 _ZN2CA11Transaction6commitEv + 393
20 QuartzCore 0x01d3e258 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92
21 CoreFoundation 0x01f4336e __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
22 CoreFoundation 0x01f432bf __CFRunLoopDoObservers + 399
23 CoreFoundation 0x01f21254 __CFRunLoopRun + 1076
24 CoreFoundation 0x01f209d3 CFRunLoopRunSpecific + 467
25 CoreFoundation 0x01f207eb CFRunLoopRunInMode + 123
26 GraphicsServices 0x03eab5ee GSEventRunModal + 192
27 GraphicsServices 0x03eab42b GSEventRun + 104
28 UIKit 0x006bef9b UIApplicationMain + 1225
29 MyApp 0x0002158d main + 141
30 libdyld.dylib 0x0262c701 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
The HTML file isn't in your bundle or isn't at the path specified. Check the case of the file and folder name. If you build/run in the simulator, you can look through the bundle inside of ~/Library/Application Support/iPhone Simulator/7.1/Applications.
Don't use loadRequest if you want to load html locally. use this
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"sample" ofType:#"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
[webView loadHTMLString:htmlString baseURL:nil];

The contentViewController ((null)) or contentViewController.view is nil

I get the following error and stacktrace however the view is not nil (as shown by the logging down right before it)
Stacktrace
2014-02-05 08:29:47.338 MenuTimer[74522:303] An uncaught exception was raised
2014-02-05 08:29:47.338 MenuTimer[74522:303] The contentViewController ((null)) or contentViewController.view is nil.
2014-02-05 08:29:47.340 MenuTimer[74522:303] (
0 CoreFoundation 0x00007fff8e39341c __exceptionPreprocess + 172
1 libobjc.A.dylib 0x00007fff87b1ee75 objc_exception_throw + 43
2 CoreFoundation 0x00007fff8e3932cc +[NSException raise:format:] + 204
3 AppKit 0x00007fff8157e3da -[NSPopover showRelativeToRect:ofView:preferredEdge:] + 228
4 MenuTimer 0x0000000100002a72 -[MenuTimer onSetTime:] + 818
5 AppKit 0x00007fff814f83d0 -[NSApplication sendAction:to:from:] + 327
6 AppKit 0x00007fff81513348 -[NSMenuItem _corePerformAction] + 394
7 AppKit 0x00007fff81513084 -[NSCarbonMenuImpl performActionWithHighlightingForItemAtIndex:] + 117
8 AppKit 0x00007fff815624dd -[NSMenu _internalPerformActionForItemAtIndex:] + 35
9 AppKit 0x00007fff81562359 -[NSCarbonMenuImpl _carbonCommandProcessEvent:handlerCallRef:] + 104
10 AppKit 0x00007fff81509176 NSSLMMenuEventHandler + 716
11 HIToolbox 0x00007fff8acff6d4 _ZL23DispatchEventToHandlersP14EventTargetRecP14OpaqueEventRefP14HandlerCallRec + 892
12 HIToolbox 0x00007fff8acfec87 _ZL30SendEventToEventTargetInternalP14OpaqueEventRefP20OpaqueEventTargetRefP14HandlerCallRec + 385
13 HIToolbox 0x00007fff8ad12d90 SendEventToEventTarget + 40
14 HIToolbox 0x00007fff8ad48a30 _ZL18SendHICommandEventjPK9HICommandjjhPKvP20OpaqueEventTargetRefS5_PP14OpaqueEventRef + 420
15 HIToolbox 0x00007fff8ad7b618 SendMenuCommandWithContextAndModifiers + 59
16 HIToolbox 0x00007fff8ad7b5c4 SendMenuItemSelectedEvent + 178
17 HIToolbox 0x00007fff8ad7b4a5 _ZL19FinishMenuSelectionP13SelectionDataP10MenuResultS2_ + 94
18 HIToolbox 0x00007fff8ad5956e _ZL19PopUpMenuSelectCoreP8MenuData5PointdS1_tjPK4RecttjS4_S4_PK10__CFStringPP13OpaqueMenuRefPt + 1726
19 HIToolbox 0x00007fff8ad586c6 _HandlePopUpMenuSelection7 + 596
20 AppKit 0x00007fff81561221 _NSSLMPopUpCarbonMenu3 + 4153
21 AppKit 0x00007fff815601d6 _NSPopUpCarbonMenu3 + 39
22 AppKit 0x00007fff8155fe96 -[NSCarbonMenuImpl popUpMenu:atLocation:width:forView:withSelectedItem:withFont:withFlags:withOptions:] + 350
23 AppKit 0x00007fff81950959 +[NSStatusBarButtonCell popupStatusBarMenu:inRect:ofView:withEvent:] + 699
24 AppKit 0x00007fff81951ade -[NSStatusItem popUpStatusItemMenu:] + 173
25 MenuTimer 0x0000000100001779 -[StatusBarView mouseDown:] + 633
26 AppKit 0x00007fff814ded08 -[NSWindow sendEvent:] + 11296
27 AppKit 0x00007fff815da11b -[NSStatusBarWindow sendEvent:] + 247
28 AppKit 0x00007fff8147d744 -[NSApplication sendEvent:] + 2021
29 AppKit 0x00007fff812cda29 -[NSApplication run] + 646
30 MenuTimer 0x0000000100002500 -[MenuTimer init] + 2112
31 MenuTimer 0x00000001000031b5 main + 101
32 libdyld.dylib 0x00007fff87fcc5fd start + 1
)
However the following code and output indicates differently. Is it because I don't have an explicit controller?
Code
// create set time window
NSLog(#"Set Time...");
NSLog(#"View %#", self.statusTime.view);
NSLog(#"Frame %#", self.statusTime.view.frame);
NSPopover *popover = [[NSPopover alloc] init];
NSLog(#"Showing popover...");
[popover showRelativeToRect:[self.statusTime.view frame]
ofView:self.statusTime.view
preferredEdge:NSMinYEdge];
Output
2014-02-05 08:29:47.237 MenuTimer[74522:303] Set Time...
2014-02-05 08:29:47.237 MenuTimer[74522:303] View <StatusBarView: 0x6080001b05a0>
2014-02-05 08:29:47.238 MenuTimer[74522:303] Frame <StatusBarView: 0x6080001b05a0>
2014-02-05 08:29:47.238 MenuTimer[74522:303] Showing popover...
You need to set the contentViewController (what the popover is actually showing) before showing it.
i.e.
NSViewController *viewController; // some view controller created programmatically or from a nib
NSPopover *popover = [[NSPopover alloc] init];
popover.contentViewController = viewController; // Missing this.
NSLog(#"Showing popover...");
[popover showRelativeToRect:[self.statusTime.view frame]
ofView:self.statusTime.view
preferredEdge:NSMinYEdge];
The ofView: parameter is just where the popover is being shown from.

UIImagePickerController source type photo library crash in Ipad iOS7

I try to present a UIImagePickerController from rect using this code:
UIImagePickerController* imgPickerController = [[UIImagePickerController alloc] init];
imgPickerController.delegate = self;
imgPickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imgPickerController.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeImage, nil];
imgPickerController.allowsEditing = NO;
_popover = [[UIPopoverController alloc] initWithContentViewController:imgPickerController];
[_popover setDelegate:self];
[_popover presentPopoverFromRect:CGRectMake(195, 55, 70, 64) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:NO];
But sometimes crash happends... NO always, sometimes run, sometimes crash...
This is the crash LOG:
Thread 0: Crashed: com.apple.main-thread
0 CoreText 0x2e4c0d1e TComponentFont::GetRawBoundingBox() const + 13
1 CoreText 0x2e4b3ba7 TBaseFont::CalculateFontMetrics(bool) const + 110
2 CoreText 0x2e4b3ba7 TBaseFont::CalculateFontMetrics(bool) const + 110
3 CoreText 0x2e4c0d0b TComponentFont::CalculateFontMetrics(bool) const + 34
4 CoreText 0x2e4b39fd TBaseFont::InitFontMetrics() const + 44
5 CoreText 0x2e4b3839 TBaseFont::GetStrikeMetrics(float, CGAffineTransform const*, bool) const + 104
6 CoreText 0x2e4b3603 TFont::InitStrikeMetrics() const + 38
7 CoreText 0x2e4b35b9 CTFontGetAscent + 92
8 GraphicsServices 0x32a17245 GSFontEnsureExtraData + 228
9 GraphicsServices 0x32a19361 GSFontGetLatin1LayoutInfo + 44
10 UIFoundation 0x359a252d -[UIFont(UIFont_AttributedStringDrawing) _getLatin1GlyphMapping:andAdvanceMapping:] + 12
11 UIFoundation 0x35978e61 __NSStringDrawingEngine + 3784
12 UIFoundation 0x3597df7f -[NSAttributedString(NSExtendedStringDrawing) boundingRectWithSize:options:context:] + 898
13 UIKit 0x3085dcab -[UILabel _updateScaledMetricsForRect:] + 594
14 UIKit 0x306023d3 -[UILabel setAdjustsFontSizeToFitWidth:] + 206
15 PhotosUI 0x3466608b -[PUFlatWhiteInterfaceTheme configureAlbumListSubtitleLabel:]
16 PhotosUI 0x345bc141 +[PUAlbumListCellContentView _updateLabelGlobalCachedSizes]
17 libobjc.A.dylib 0x38588231 _class_initialize + 572
18 libobjc.A.dylib 0x3858f7a7 lookUpImpOrForward + 130
19 libobjc.A.dylib 0x38587feb _class_lookupMethodAndLoadCache3 + 34
20 libobjc.A.dylib 0x38587db9 _objc_msgSend_uncached + 24
21 PhotosUI 0x345bb059 -[PUAlbumListTableViewController tableView:cellForRowAtIndexPath:]
22 UIKit 0x3069aa5b -[UITableView _createPreparedCellForGlobalRow:withIndexPath:] + 410
23 UIKit 0x30642e7d -[UITableView _updateVisibleCellsNow:] + 1804
24 UIKit 0x30642699 -[UITableView layoutSubviews] + 184
25 UIKit 0x30568da3 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 346
26 QuartzCore 0x301efc6b -[CALayer layoutSublayers] + 142
27 QuartzCore 0x301eb47b CA::Layer::layout_if_needed(CA::Transaction*) + 350
28 UIKit 0x3057dc1b -[UIView(Hierarchy) layoutBelowIfNeeded] + 530
29 PhotosUI 0x3467ec55 -[PUAbstractAlbumListViewController viewDidLayoutSubviews]
30 UIKit 0x30568e79 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 560
31 QuartzCore 0x301efc6b -[CALayer layoutSublayers] + 142
32 QuartzCore 0x301eb47b CA::Layer::layout_if_needed(CA::Transaction*) + 350
33 QuartzCore 0x301eb30d CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 16
34 QuartzCore 0x301ead1f CA::Context::commit_transaction(CA::Transaction*) + 230
35 QuartzCore 0x301eab2f CA::Transaction::commit() + 314
36 UIKit 0x305610c3 _afterCACommitHandler + 126
37 CoreFoundation 0x2ddc11cd __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 20
38 CoreFoundation 0x2ddbeb71 __CFRunLoopDoObservers + 284
39 CoreFoundation 0x2ddbeeb3 __CFRunLoopRun + 730
40 CoreFoundation 0x2dd29c27 CFRunLoopRunSpecific + 522
41 CoreFoundation 0x2dd29a0b CFRunLoopRunInMode + 106
42 GraphicsServices 0x32a1d283 GSEventRunModal + 138
43 UIKit 0x305cd049 UIApplicationMain + 1136
44 Puzz! 0x00032665 main (main.m:16)
Thanks!
EDIT
Finally, I decided use a AssetsLibrary framework to create a custom UIImagePickerController using this MyImagePicker sample:
https://developer.apple.com/library/ios/samplecode/myimagepicker/Introduction/Intro.html

Getting error after execute NSMutableArray

Here is the code. I tried to create a object which includes the name, size and image(get from the resource) and then I add that object into NSMutableArray in order to show those object in NSTableView. However, after I run the code, I did get the correct counts of object in NSMutableArray and correct information in each object(name,size,image), but it still gives me error. Could you guys help me. I am a new Mac developer. Thanks !!
Here is the code:
#implementation AppDelegate{
NSMutableArray* _tableContents;
}
-(void)awakeFromNib{
NSString* imagePath = #"USB.png";
NSString* mountPath = #"/Volumes";
_tableContents = [[NSMutableArray alloc]init];
for(mountPath in [[NSWorkspace sharedWorkspace]mountedLocalVolumePaths])
{
if(0==[mountPath rangeOfString:#"/Volumes/"].location)
{
NSString* USBpath = [[[NSWorkspace sharedWorkspace]mountedLocalVolumePaths]objectAtIndex:count];
//get the name of USB drive
NSString* nameOfUSB = [USBpath substringFromIndex:9];
//get the size of USB drive
NSNumber *volumeSize;
NSURL *mountPathPicked = [NSURL fileURLWithPath:mountPath];
if([mountPathPicked getResourceValue:&volumeSize forKey:NSURLVolumeTotalCapacityKey error:nil])
{
NSDictionary *obj = #{#"name":nameOfUSB,
#"image":[NSImage imageNamed:imagePath],
#"size":volumeSize
};
//after I call the following line, it will give me error !!
[_tableContents addObject:obj];
}
}
count ++;
}
}
//I also use the following two methods in order to show the name, and image in NSTableView
-(NSInteger)numberOfRowsInTableView:(NSTableView *)tableView{
return [_tableCountents count];
}
-(NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row{
NSDictionary *flag = _tableContents[row];
NSString *identifier = [tableColumn identifier];//call different col we need identifier
if([identifier isEqualToString:#"MainCell"])
{
NSTableCellView *cellView = [tableView makeViewWithIdentifier:#"MainCell" owner:self];//give back specific cell named mainCell
[cellView.textField setStringValue:flag[#"name"]];
[cellView.imageView setImage:flag[#"image"]];
return cellView;
}
return nil;
}
There is no connection problems in GUI. And here is the error message:
2013-04-25 01:16:41.321 NAMEOFPROJECT[8243:303] An uncaught exception was raised
2013-04-25 01:16:41.322 NAMEOFPROJECT[8243:303] *** -[__NSArrayI objectAtIndex:]: index 7 beyond bounds [0 .. 3]
2013-04-25 01:16:41.329 NAMEOFPROJECT[8243:303] (
0 CoreFoundation 0x00007fff9147cb06 __exceptionPreprocess + 198
1 libobjc.A.dylib 0x00007fff900c03f0 objc_exception_throw + 43
2 CoreFoundation 0x00007fff9142fb53 -[__NSArrayI objectAtIndex:] + 163
3 CDTOANYDRIVE 0x00000001000011d6 -[AppDelegate DetectingUSBDrives] + 710
4 CDTOANYDRIVE 0x0000000100000efb -[AppDelegate awakeFromNib] + 43
5 AppKit 0x00007fff899631a8 -[NSIBObjectData nibInstantiateWithOwner:topLevelObjects:] + 1234
6 AppKit 0x00007fff89b79976 -[NSNib _instantiateNibWithExternalNameTable:] + 610
7 AppKit 0x00007fff89b7a41a -[NSNib instantiateNibWithExternalNameTable:] + 17
8 AppKit 0x00007fff89b262b4 -[NSTableRowData _unarchiveViewWithIdentifier:owner:] + 520
9 CDTOANYDRIVE 0x000000010000174b -[AppDelegate tableView:viewForTableColumn:row:] + 219
10 AppKit 0x00007fff89b279a2 -[NSTableRowData _addViewToRowView:atColumn:row:] + 324
11 AppKit 0x00007fff89b2766f -[NSTableRowData _addViewsToRowView:atRow:] + 151
12 AppKit 0x00007fff89b25c05 -[NSTableRowData _addRowViewForVisibleRow:withPriorView:] + 415
13 AppKit 0x00007fff89b2598a -[NSTableRowData _addRowViewForVisibleRow:withPriorRowIndex:inDictionary:withRowAnimation:] + 272
14 AppKit 0x00007fff89b24c59 -[NSTableRowData _unsafeUpdateVisibleRowEntries] + 740
15 AppKit 0x00007fff89b247f1 -[NSTableRowData updateVisibleRowViews] + 119
16 AppKit 0x00007fff89afc5d7 -[NSTableView layout] + 165
17 AppKit 0x00007fff89aafe95 -[NSView _layoutSubtreeHeedingRecursionGuard:] + 112
18 CoreFoundation 0x00007fff914754a6 __NSArrayEnumerate + 582
19 AppKit 0x00007fff89aafff6 -[NSView _layoutSubtreeHeedingRecursionGuard:] + 465
20 CoreFoundation 0x00007fff914754a6 __NSArrayEnumerate + 582
21 AppKit 0x00007fff89aafff6 -[NSView _layoutSubtreeHeedingRecursionGuard:] + 465
22 CoreFoundation 0x00007fff914754a6 __NSArrayEnumerate + 582
23 AppKit 0x00007fff89aafff6 -[NSView _layoutSubtreeHeedingRecursionGuard:] + 465
24 CoreFoundation 0x00007fff914754a6 __NSArrayEnumerate + 582
25 AppKit 0x00007fff89aafff6 -[NSView _layoutSubtreeHeedingRecursionGuard:] + 465
26 AppKit 0x00007fff89aafd2e -[NSView layoutSubtreeIfNeeded] + 615
27 AppKit 0x00007fff89aab4dc -[NSWindow(NSConstraintBasedLayout) layoutIfNeeded] + 201
28 AppKit 0x00007fff899d96a8 -[NSView _layoutAtWindowLevelIfNeeded] + 99
29 AppKit 0x00007fff899d8fe6 -[NSView _sendViewWillDrawInRect:clipRootView:] + 87
30 AppKit 0x00007fff899a59b1 -[NSView displayIfNeeded] + 1044
31 AppKit 0x00007fff89a62a48 -[NSWindow _reallyDoOrderWindow:relativeTo:findKey:forCounter:force:isModal:] + 1377
32 AppKit 0x00007fff89a62068 -[NSWindow _doOrderWindow:relativeTo:findKey:forCounter:force:isModal:] + 940
33 AppKit 0x00007fff89a61c4f -[NSWindow orderWindow:relativeTo:] + 159
34 AppKit 0x00007fff89963266 -[NSIBObjectData nibInstantiateWithOwner:topLevelObjects:] + 1424
35 AppKit 0x00007fff8994214d loadNib + 317
36 AppKit 0x00007fff89941679 +[NSBundle(NSNibLoading) _loadNibFile:nameTable:withZone:ownerBundle:] + 219
37 AppKit 0x00007fff899414ae -[NSBundle(NSNibLoading) loadNibNamed:owner:topLevelObjects:] + 200
38 AppKit 0x00007fff8994128e +[NSBundle(NSNibLoading) loadNibNamed:owner:] + 360
39 AppKit 0x00007fff8993da2f NSApplicationMain + 398
40 CDTOANYDRIVE 0x0000000100000e82 main + 34
41 libdyld.dylib 0x00007fff9385a7e1 start + 0
42 ??? 0x0000000000000003 0x0 + 3
)
And the above error shows twice !!
I have tried your code in a mini-app and It works fine. There are few things that I might highlight:
Your exception occurs in the method DetectingUSBDrives, for which we don't have any code. I'm assuming that you have pasted the code in the awakeFromNib method.
You are using count as an index. Is this varibale initialized at zero?
The fact that the error appears twice suggests that your cell views hold a reference to your app delegate, which means that awakeFromNib is called once for each view creation. This can create many problem since you are initializing _tableOfContents there. You might want to move all that code to the init method instead.