Getting NSGeneric Exception while updating constraints programmatically? - objective-c

Here is my code:
int x= 17;
int y = 27;
int width = 287;
int height = 181;
int mainview_height = 568;
int x_spacer = 0;
int y_spacer = 0;
int width_spacer = 320;
int height_spacer = 27;
UIView *view_spacer = [[UIView alloc]initWithFrame:CGRectMake(x_spacer, y_spacer, width_spacer, height_spacer)];
[view_spacer setBackgroundColor:[UIColor clearColor]];
[view_spacer setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.mainView addSubview:view_spacer];
UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
[view1 setBackgroundColor:[UIColor whiteColor]];
[view1 setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.mainView addSubview:view1];
[view_spacer addConstraint:[NSLayoutConstraint constraintWithItem:view_spacer attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual toItem:view_spacer attribute:NSLayoutAttributeHeight multiplier:width_spacer/height_spacer constant:0.0f]];
[self.mainView addConstraint:[NSLayoutConstraint constraintWithItem:view_spacer attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.mainView attribute:NSLayoutAttributeTop multiplier:1 constant:0.0f]];
[self.mainView addConstraint:[NSLayoutConstraint constraintWithItem:view_spacer attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual toItem:self.mainView attribute:NSLayoutAttributeHeight multiplier:width_spacer/mainview_height constant:0.0f]];
[self.mainView addConstraint:[NSLayoutConstraint constraintWithItem:self.mainView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:view_spacer attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
[view_spacer addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:view_spacer attribute:NSLayoutAttributeBottom multiplier:1 constant:0.0f]];
[view1 addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual toItem:view1 attribute:NSLayoutAttributeHeight
multiplier:width/height constant:0.0f]];
[self.mainView addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual toItem:self.mainView attribute:NSLayoutAttributeHeight multiplier:width/mainview_height constant:0.0f]];
[self.mainView addConstraint:[NSLayoutConstraint constraintWithItem:self.mainView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:view1 attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
I got the following error while updating the auto layout constraints
The view hierarchy is not prepared for the constraint:
When added to
a view, the constraint's items must be descendants of that view (or
the view itself). This will crash if the constraint needs to be
resolved before the view hierarchy is assembled. Break on -[UIView
_viewHierarchyUnpreparedForConstraint:] to debug. 2015-07-18 12:54:02.469 LandCorp Cleaning App[2022:64687] View hierarchy
unprepared for constraint. Constraint:
Container
hierarchy: > View not found in container hierarchy:
> That view's superview: > 2015-07-18 12:54:02.472 LandCorp Cleaning
App[2022:64687] * Terminating app due to uncaught exception
'NSGenericException', reason: 'Unable to install constraint on view.
Does the constraint reference something from outside the subtree of
the view? That's illegal.
constraint: view:>'
* First throw call stack: ( 0 CoreFoundation 0x000000010799dc65 exceptionPreprocess + 165 1 libobjc.A.dylib
0x0000000107636bb7 objc_exception_throw + 45 2 CoreFoundation
0x000000010799db9d +[NSException raise:format:] + 205 3 Foundation
0x00000001071c8479 -[NSLayoutConstraint
_addToEngine:integralizationAdjustment:mutuallyExclusiveConstraints:] + 187 4 UIKit 0x0000000108406786 -[UIView(UIConstraintBasedLayout) _layoutEngine_didAddLayoutConstraint:roundingAdjustment:mutuallyExclusiveConstraints:]
+ 411 5 UIKit 0x0000000108406a14 -[UIView(UIConstraintBasedLayout) _tryToAddConstraintWithoutUpdatingConstraintsArray:roundingAdjustment:mutuallyExclusiveConstraints:]
+ 30 6 UIKit 0x0000000108406b3c -[UIView(UIConstraintBasedLayout) _tryToAddConstraint:roundingAdjustment:mutuallyExclusiveConstraints:] + 243 7 LandCorp Cleaning App 0x00000001070e7e47 -[InspectionDetails populateDataViews] + 2007 8 LandCorp Cleaning App 0x00000001070e748c -[InspectionDetails
connection:didReceiveData:] + 1404 9 CFNetwork
0x00000001097dbd6c __65-[NSURLConnectionInternal
_withConnectionAndDelegate:onlyActive:]_block_invoke + 69 10 CFNetwork 0x00000001097dbd10
-[NSURLConnectionInternal _withConnectionAndDelegate:onlyActive:] + 199 11 CFNetwork 0x00000001097dbe77
-[NSURLConnectionInternal _withActiveConnectionAndDelegate:] + 48 12 CFNetwork 0x00000001097c6089
_ZL30_NSURLConnectionDidReceiveDataP16_CFURLConnectionPK8__CFDatalPKv + 79 13 CFNetwork 0x00000001096a85e0 ___ZN27URLConnectionClient_Classic29_delegate_didReceiveDataArrayEv_block_invoke
+ 302 14 CFNetwork 0x0000000109778a71 ___ZN27URLConnectionClient_Classic18_withDelegateAsyncEPKcU13block_pointerFvP16_CFURLConnectionPK33CFURLConnectionClientCurrent_VMaxE_block_invoke_2
+ 273 15 CFNetwork 0x00000001096965d6 _ZN19RunloopBlockContext13_invoke_blockEPKvPv + 72 16 CoreFoundation 0x00000001078a4354 CFArrayApplyFunction + 68 17 CFNetwork
0x0000000109696497 _ZN19RunloopBlockContext7performEv + 133 18
CFNetwork 0x00000001096962d6
_ZN17MultiplexerSource7performEv + 256 19 CFNetwork 0x00000001096960ec _ZN17MultiplexerSource8_performEPv + 72 20
CoreFoundation 0x00000001078d1431
__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17 21 CoreFoundation 0x00000001078c72fd
__CFRunLoopDoSources0 + 269 22 CoreFoundation 0x00000001078c6934 __CFRunLoopRun + 868 23 CoreFoundation
0x00000001078c6366 CFRunLoopRunSpecific + 470 24 GraphicsServices
0x000000010aec3a3e GSEventRunModal + 161 25 UIKit
0x0000000107d968c0 UIApplicationMain + 1282 26 LandCorp Cleaning App
0x00000001070e897f main + 111 27 libdyld.dylib
0x0000000109f41145 start + 1 ) libc++abi.dylib: terminating with
uncaught exception of type NSException

Related

Xcode version to 9, iOS11 and works fine except iPhone x. UI got collapsed

I have Created Two UIView one headerview and second is UICollectionView both subview of UIScrollView and I have hidden my NavigationBar entire app.
Now I have added below code for set "SafeAreaLayoutGuides" in iOS11 but here application crass due to superview nil found.
I have added below code.
UIView *parentView = self.view.superview;
UIView *childView = scrollViewMain.superview;
childView.translatesAutoresizingMaskIntoConstraints = NO;
NSLayoutConstraint *topConstraint;
NSLayoutConstraint *bottomConstraint;
if (#available(iOS 11, *)) {
topConstraint = [NSLayoutConstraint constraintWithItem:childView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:parentView.safeAreaLayoutGuide attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
bottomConstraint = [NSLayoutConstraint constraintWithItem:childView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:parentView.safeAreaLayoutGuide attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
} else {
topConstraint = [NSLayoutConstraint constraintWithItem:childView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:parentView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
bottomConstraint = [NSLayoutConstraint constraintWithItem:childView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:parentView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
}
[parentView addConstraint:topConstraint];
[parentView addConstraint:bottomConstraint];
If parentView is nil (because self.view.superview is nil), then that means that self.view hasn't been added to the view hierarchy yet.
Make sure that whatever view contains this one has called self.addSubview(childView); that'll fix the "nil superview" issue.

iOS8 app getting crash while opening popover

This is the code that I have written and it is in the method when the user selects the popover:
- (IBAction) onSelectingDefect:(id)sender
{
if ([description isFirstResponder]) {
[self hideKeyBoard];
return;
}
UIButton *button = (UIButton*)(sender);
UIButton *doneSelectingStatusButton = [UIButton buttonWithType:UIButtonTypeCustom];
doneSelectingStatusButton.frame = CGRectMake(85, 223, 149, 32);
[doneSelectingStatusButton setTintColor:[UIColor blackColor]];
[doneSelectingStatusButton setTitle:#"Done" forState:UIControlStateNormal];
[doneSelectingStatusButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[doneSelectingStatusButton setBackgroundImage:[UIImage imageNamed:#"cancel_button02.png"] forState:UIControlStateNormal];
[doneSelectingStatusButton addTarget:self action:#selector(onPickerSelection:) forControlEvents:UIControlEventTouchUpInside];
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];
pickerView.showsSelectionIndicator = YES;
pickerView.delegate = self;
pickerView.dataSource = self;
BOOL isNill = TRUE;
int i;
for(i=0; i<[tableData count]; i++)
{
if([selectedPickerText isEqualToString:[tableData objectAtIndex:i]])
{
isNill = FALSE;
break;
}
}
if(isNill)
{
[pickerView selectRow:0 inComponent:0 animated:YES]; //Orig
//self.selectedPickerText = [tableData objectAtIndex:0];
self.selectedPickerText = [tableData objectAtIndex:0];
}
else
[pickerView selectRow:i inComponent:0 animated:YES];//Orig
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0,0, 320, 255)];
[containerView addSubview:doneSelectingStatusButton];
[containerView addSubview:pickerView];
UIViewController *pickerViewController = [[UIViewController alloc] init];
CGRect myRect = button.frame;
myRect.origin.x += 10;
myRect.origin.y += 10;
selectionPopover = [[UIPopoverController alloc] initWithContentViewController:pickerViewController];
((AppDelegate*) [UIApplication sharedApplication].delegate).secondPopOver = selectionPopover;
selectionPopover.delegate =self;
selectionPopover.popoverContentSize = CGSizeMake(320, 255);
[selectionPopover presentPopoverFromRect:selectDefect.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; //Error iOS8
pickerViewController.view = containerView;
[containerView release];
[pickerView release];
[pickerViewController release];
}
This is the error I am geting:
2014-11-04 20:01:30.740 Service360[447:5288] -[UITextField length]: unrecognized selector sent to instance 0x7f0d6520
2014-11-04 20:01:30.743 Service360[447:5288] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITextField length]: unrecognized selector sent to instance 0x7f0d6520'
*** First throw call stack:
(
0 CoreFoundation 0x0321f946 __exceptionPreprocess + 182
1 libobjc.A.dylib 0x02d01a97 objc_exception_throw + 44
2 CoreFoundation 0x032275c5 -[NSObject(NSObject) doesNotRecognizeSelector:] + 277
3 CoreFoundation 0x031703e7 ___forwarding___ + 1047
4 CoreFoundation 0x0316ffae _CF_forwarding_prep_0 + 14
5 CoreFoundation 0x03108d26 CFStringAppend + 374
6 CoreFoundation 0x0310684a __CFStringAppendFormatCore + 11754
7 CoreFoundation 0x031fc5f5 _CFStringCreateWithFormatAndArgumentsAux2 + 245
8 Foundation 0x028b7697 -[NSPlaceholderString initWithFormat:locale:arguments:] + 159
9 Foundation 0x028baf42 +[NSString stringWithFormat:] + 89
10 UIKit 0x0183dd34 -[UIViewController _presentViewController:withAnimationController:completion:] + 2825
11 UIKit 0x018405d2 __62-[UIViewController presentViewController:animated:completion:]_block_invoke + 345
12 UIKit 0x01840424 -[UIViewController presentViewController:animated:completion:] + 224
13 UIKit 0x01db0791 -[UIPopoverController _presentShimmedPopoverFromRect:inView:permittedArrowDirections:animated:] + 217
14 UIKit 0x01db0991 -[UIPopoverController presentPopoverFromRect:inView:permittedArrowDirections:animated:] + 355
15 Service360 0x00369b52 -[POVendorFeedbackViewController onSelectingDefect:] + 2770
16 libobjc.A.dylib 0x02d177cd -[NSObject performSelector:withObject:withObject:] + 84
17 UIKit 0x016da23d -[UIApplication sendAction:to:from:forEvent:] + 99
18 UIKit 0x016da1cf -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 64
19 UIKit 0x0180de86 -[UIControl sendAction:to:forEvent:] + 69
20 UIKit 0x0180e2a3 -[UIControl _sendActionsForEvents:withEvent:] + 598
21 UIKit 0x0180d50d -[UIControl touchesEnded:withEvent:] + 660
22 UIKit 0x0172a60a -[UIWindow _sendTouchesForEvent:] + 874
23 UIKit 0x0172b0e5 -[UIWindow sendEvent:] + 791
24 UIKit 0x016f0549 -[UIApplication sendEvent:] + 242
25 Service360 0x00068f22 -[MyApplication sendEvent:] + 178
26 UIKit 0x0170037e _UIApplicationHandleEventFromQueueEvent + 20690
27 UIKit 0x016d4b19 _UIApplicationHandleEventQueue + 2206
28 CoreFoundation 0x031431df __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
29 CoreFoundation 0x03138ced __CFRunLoopDoSources0 + 253
30 CoreFoundation 0x03138248 __CFRunLoopRun + 952
31 CoreFoundation 0x03137bcb CFRunLoopRunSpecific + 443
32 CoreFoundation 0x031379fb CFRunLoopRunInMode + 123
33 GraphicsServices 0x044b424f GSEventRunModal + 192
34 GraphicsServices 0x044b408c GSEventRun + 104
35 UIKit 0x016d88b6 UIApplicationMain + 1526
36 Service360 0x0006793f main + 159
37 libdyld.dylib 0x077a7ac9 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
THe line which is causing the app to crash is:
[selectionPopover presentPopoverFromRect:selectDefect.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; //Error iOS8
What can be the issue which is causing this error. The app works fine in earlier or previous releases of iOS.
Can you pls help me?
Thanks
As of ios8 you need a UIPopoverPresentationController For the popovercontroller.
if ( NSClassFromString(#"UIPopoverPresentationController") ) {
selectionPopover.popoverPresentationController.sourceView = button; //view the popover arrow points to...
}
/// then present popover...

Adding subview to tableview using autolayout

I was trying to add a subview to tableview using autolayout, but I get this Assertion failure when I do so.
" * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Auto Layout still required after executing -layoutSubviews. UITableView's implementation of -layoutSubviews needs to call super.'"
My code
UIView *view = [[UIView alloc] init];
[view setBackgroundColor:[UIColor redColor]];
[parentView addSubview:view];
[view setTranslatesAutoresizingMaskIntoConstraints:NO];
NSLayoutConstraint *c1 = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:parentView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.0];
NSLayoutConstraint *c2 = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:parentView attribute:NSLayoutAttributeLeft multiplier:0.0 constant:10.0];
NSLayoutConstraint *c3 = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:parentView attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0.0];
NSLayoutConstraint *c4 = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:parentView attribute:NSLayoutAttributeTop multiplier:1.0 constant:50.0];
[parentView addConstraint:c1];
[parentView addConstraint:c3];
[parentView addConstraint:c2];
[parentView addConstraint:c1];
but if I set frame to the view without any autolayout constraint, it works fine.
I couldn't figure out what I am missing.

UIView Element Layout Constraints

I'm fairly new to iOS development but have reached the point where I want to create my own composite UIView as a custom UIButton. I would like to layout a UILabel and 2x UIImageViews as follows;
But I would also like to anchor the controls (subviews) in such a way that as the label expands, due to translations say, the view automatically handles the extra real estate. For example;
Ideally -
the right hand side UIView is anchored to the right; and remains a fixed width/hight (right aligned).
the label and bottom image divide the remaining left hand space
the label is vertically centered in the top half of the upper remaining space
the bottom image remains centered (both vertically and horizontally) in the lower remaining space
if the label is wider than the bottom image then the view should expand
I'm happy to construct this in pure code if required. I used a XIB in the above images to play with attributes and to visualize my question.
I'm from a C#/XAML background so I would typically use grid layouts with fixed/auto/* columns and rows but I'm guessing I need to use something like NSLayoutConstraints here - unfortunately I don't know where to start or how to search for the solution. Any help appreciated!
in your UIButton
(code not tested)
//put the code below in your button's init method
UILabel *label = [[UILabel alloc] init];
UIImageView *bottomImageView = [[UIImageView alloc] init];
UIImageView *rightImageView = [[UIImageView alloc] init];
// we define our own contraints,
// we don't want the system to fall-back on UIView's autoresizingMask property (pre-iOS 6)
self.translatesAutoresizingMaskIntoConstraints = NO;
label.translatesAutoresizingMaskIntoConstraints = NO;
bottomImageView.translatesAutoresizingMaskIntoConstraints = NO;
rightImageView.translatesAutoresizingMaskIntoConstraints = NO;
//fixed sizes => SET THESE as you want
CGFloat labelHeight, bottomWidth, rightImageWidth;
// 1 - Label constraints :
// labelWidth = 1 *self.width - rightImageWidth
NSLayoutConstraint *labelWidth = [NSLayoutConstraint constraintWithItem:label
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeWidth
multiplier:1
constant:(- rightImageWidth)];
// label must be at the top :
NSLayoutConstraint *labelTop = [NSLayoutConstraint constraintWithItem:label
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeTop
multiplier:1
constant:0];
//label must be on the left border
NSLayoutConstraint *labelLeft = [NSLayoutConstraint constraintWithItem:label
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeLeft
multiplier:1
constant:0];
//label must be of 1/2 height
NSLayoutConstraint *labelHeight = [NSLayoutConstraint constraintWithItem:label
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeHeight
multiplier:0.5
constant:0];
[self addSubview:label];
[self addConstraints:#[labelWidth, labelTop, labelLeft, labelHeight]];
//2 - botom view
// width constant
NSLayoutContraint *bottomWidth = [NSLayoutConstraint constraintWithItem:bottomImageView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:0
constant:bottomWidth];
// same height constraint as label
NSLayoutConstraint *bottomHeight = [NSLayoutConstraint constraintWithItem:bottomImageView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeHeight
multiplier:0.5
constant:0];
// sticks at container's bottom (pun intended)
NSLayoutConstraint *bottomBottom = [NSLayoutConstraint constraintWithItem:bottomImageView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeBottom
multiplier:1
constant:0];
//we have height, width, y contraints, just x remains
// NOTE : this one is between bottom view and label
NSLayoutConstraint *bottomCenteredXAsLabel = [NSLayoutConstraint constraintWithItem:bottomImageView
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:label
attribute:NSLayoutAttributeCenterX
multiplier:1
constant:0];
[self addSubview:bottomImageView];
[self addConstraints:#[bottomWidth, bottomHeight, bottomBottom, bottomCenteredXAsLabel]];
// 3 - last one !
NSLayoutConstraint *rightAligned = [NSLayoutConstraint constraintWithItem:rightImageView
attribute:NSLayoutAttributeRight
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeRight
multiplier:1
constant:0];
//right height
NSLayoutConstraint *rightHeight = [NSLayoutConstraint constraintWithItem:rightImageView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeHeight
multiplier:1
constant:0];
//constant width...
NSLayoutConstraint *rightWidth = [NSLayoutConstraint constraintWithItem:rightImageView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:0
constant:rightImageWidth];
//width, height, x constraints...
//we still need one on y, let's say it sticks at the top
NSLayoutConstraint *rightTop = [NSLayoutConstraint constraintWithItem:rightImageView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeTop
multiplier:1
constant:0];
[self addSubview:rightImageView];
[self addConstraints:#[rightAligned, rightHeight, rightWidth, rightTop]];
Et voilĂ  !
The method is always the same : you need at least 4 constraints on each view, setting width, height, x, and y (CGRect 4 dimensions).
Think of a constraint as a relation :
item1.layoutAttribute1 >= a*item2.layoutAttribute2 + b
to translate in this form
[NSLayoutConstraint constraintWithItem:item1
attribute:layoutAttribute1
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:item2
attribute:layoutAttribute2
multiplier:a
constant:b];
Note that using visual format, you might express all that with less code. (but I've not played with it yet).

MPMoviePlayer crashing app on device but not in simulator, but only when trying to play some files

I am writing an app that displays videos using MPMoviePlayer which are stored locally on the device. When I try to play most of the videos, the app crashes on the device, but not on the simulator where they play fine. I am using the same code to play all of the videos, I have tried deleting and reinstalling the app, cleaning then building again, with no luck.
The simulator is version 4.3 and the iPad I am testing the app on is version 4.3.1. I am using Xcode 4
This is the code I am using to play one of the videos that crashes:
#implementation SinglePendulumControl
-(IBAction)playMovie:(id)sender
{
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"SinglePendulumControl" ofType:#"mp4"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[self.view addSubview:moviePlayerController.view];
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:YES];
[[self view] setBounds:CGRectMake(0, 0, 1024, 768)];
[[self view] setTransform:CGAffineTransformMakeRotation(M_PI / 2)];
moviePlayerController.fullscreen = YES;
[moviePlayerController play];
}
- (void)moviePlaybackComplete:(NSNotification *)notification
{
MPMoviePlayerController *moviePlayerController = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[moviePlayerController.view removeFromSuperview];
[moviePlayerController release];
}
This is the code I am using to play one of the videos that doesn't crash:
#implementation GlueSettings
-(IBAction)playMovie:(id)sender
{
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"gluesettings" ofType:#"mp4"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[self.view addSubview:moviePlayerController.view];
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:YES];
[[self view] setBounds:CGRectMake(0, 0, 1024, 768)];
[[self view] setTransform:CGAffineTransformMakeRotation(M_PI / 2)];
moviePlayerController.fullscreen = YES;
[moviePlayerController play];
}
- (void)moviePlaybackComplete:(NSNotification *)notification
{
MPMoviePlayerController *moviePlayerController = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[moviePlayerController.view removeFromSuperview];
[moviePlayerController release];
}
The console log of the crash:
May 16 17:55:31 unknown Vector[3590] <Error>: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter'
*** Call stack at first throw:
(
0 CoreFoundation 0x33c3d64f __exceptionPreprocess + 114
1 libobjc.A.dylib 0x33f01c5d objc_exception_throw + 24
2 CoreFoundation 0x33c3d491 +[NSException raise:format:arguments:] + 68
3 CoreFoundation 0x33c3d4cb +[NSException raise:format:] + 34
4 Foundation 0x36da018f -[NSURL(NSURL) initFileURLWithPath:] + 70
5 Foundation 0x36da012f +[NSURL(NSURL) fileURLWithPath:] + 30
6 Vector 0x000eb7e5 -[BumpBarBumpSwitch playMovie:] + 80
7 CoreFoundation 0x33bad571 -[NSObject(NSObject) performSelector:withObject:withObject:] + 24
8 UIKit 0x31f43ec9 -[UIApplication sendAction:to:from:forEvent:] + 84
9 UIKit 0x31f43e69 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 32
10 UIKit 0x31f43e3b -[UIControl sendAction:to:forEvent:] + 38
11 UIKit 0x31f43b8d -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 356
12 UIKit 0x31f44423 -[UIControl touchesEnded:withEvent:] + 342
13 UIKit 0x31f42bf5 -[UIWindow _sendTouchesForEvent:] + 368
14 UIKit 0x31f4256f -[UIWindow sendEvent:] + 262
15 UIKit 0x31f2b313 -[UIApplication sendEvent:] + 298
16 UIKit 0x31f2ac53 _UIApplicationHandleEvent + 5090
17 GraphicsServices 0x3257ee77 PurpleEventCallback + 666
18 CoreFoundation 0x33c14a97 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 26
19 CoreFoundation 0x33c1683f __CFRunLoopDoSource1 + 166
20 CoreFoundation 0x33c1760d __CFRunLoopRun + 520
21 CoreFoundation 0x33ba7ec3 CFRunLoopRunSpecific + 230
22 CoreFoundation 0x33ba7dcb CFRunLoopRunInMode + 58
23 GraphicsServices 0x3257e41f GSEventRunModal + 114
24 GraphicsServices 0x3257e4cb GSEventRun + 62
25 UIKit 0x31f55d69 -[UIApplication _run] + 404
26 UIKit 0x31f53807 UIApplicationMain + 670
27 Vector 0x000e9f31 main + 48
28 Vector 0x000e9efc start + 40
)
May 16 17:55:31 unknown UIKitApplication:Vector-Systems.Vector[0xc457][3590] <Notice>: terminate called after throwing an instance of 'NSException'
May 16 17:55:31 unknown ReportCrash[3591] <Notice>: Formulating crash report for process Vector[3590]
May 16 17:55:31 unknown com.apple.launchd[1] <Warning>: (UIKitApplication:Vector-Systems.Vector[0xc457]) Job appears to have crashed: Abort trap: 6
May 16 17:55:31 unknown SpringBoard[29] <Warning>: Application 'Vector' exited abnormally with signal 6: Abort trap: 6
May 16 17:55:31 unknown ReportCrash[3591] <Error>: Saved crashreport to /var/mobile/Library/Logs/CrashReporter/Vector_2011-05-16-175531_Duncans-iPad.plist using uid: 0 gid: 0, synthetic_euid: 501 egid: 0
May 16 17:55:34 unknown DTPower[3080] <Warning>: Task info failed for task Vector-Systems.Vector