how to solve run time error or Terminating app due to uncaught exception NSInvalidArgumentException? - objective-c

I am a beginner with iPhone development. I have used scrollview with an array of images but in that run time error like Terminating app due to uncaught exception: NSInvalidArgumentException, reason: -[UIView setContentSize:]: unrecognized selector sent to instance
My code:
- (void)viewDidLoad
{
[self.view addSubview:imagedisplay];
self.view.frame = CGRectMake(0, 0, 320, 480);
[(UIScrollView*)self.view setContentSize:[imagedisplay size]];
[(UIScrollView*)self.view setMaximumZoomScale:5.0];
}
-(UIView *)viewForZoomingInScrollView:(UIScrollView*)scrollView
{
return imagedisplay;
}
Give any suggestions and source code which applies.

Your problem line is:
[(UIScrollView*)self.view setContentSize:[imagedisplay size]];
It's saying that self.view does not contain the method ContentSize where self is a UIView and not a UIScrolLView.

Related

Getting an EXC_BAD_ACCESS / KERN_INVALID_ADDRESS when creating a view

I'm creating a custom view that will be responsible for displaying some text.
BOOL DisplayBiggerString(Item *feedItem, CGFloat contentWidth) {
StyledString *const tryBiggerString = [feedItem buildBiggerString:[feedItem myStyle]];
CoreTextView *testTextView = [[CoreTextView alloc] initWithFrame:CGRectMake(0, 0, contentWidth, 0)];
[testTextView setStyledString:tryBiggerString];
return testTextView.numberOfLines > 1;
}
It seems that it's crashing on the line:
CoreTextView *testTextView = [[CoreTextView alloc] initWithFrame:CGRectMake(0, 0, contentWidth, 0)];
I'm not sure why it's causing a crashing for that view. The function inside just creates a new view like so:
if (self = [super initWithFrame:CGRectMake(0, 0, width, 1)])
I personally haven't been able to repro this as I can only respond to crash reports I've gotten. I checked the contentWidth and it's just getting the collectionView's width:
CGRectGetWidth(self.collectionView.bounds)
and this is done outside of a dispatch_async call which sizes the string when coming from cache:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
MakeTextCacheWarmup(newObjects, self.dataSource.feedSectionControllerComponents.itemConfiguration, width, width);
Is there some way to pass some width that would cause this to crash? I've tested all float values and they have seemed fine so far. Or is it a problem to create a view inside of the async block? I never add the view but merely use it to decide the number of lines of text
libobjc.A.dylib objc_msgSend
1CoreFoundation -[__NSSetM addObject:]
2UIKitCore UIViewDidSetNeedsDisplay
3UIKitCore -[UIView _createLayerWithFrame:]
4UIKitCore UIViewCommonInitWithFrame
5UIKitCore -[UIView initWithFrame:]
6MyApp __cmp_gen_92064(MYUserSession+MYMediaUploadManager.m:13)
7+MyApp DisplayBiggerString(MYConsumptionHelpers.m:12)
8+MyApp +[MYFeedItemTextCell buildStyledStringWithFeedItem:feedItemRow:pageCellState:configuration:contentWidth:textWidth:combinedContextOptions:userSession:](MYFeedItemTextCell.m:403)
9+MyApp __cmp_gen_8c7c(MYMainAppViewController.m:174)
10+MyApp MakeTextCacheWarmup(MakeTextCacheWarmup.m:47)
11+MyApp __81-[MYMainFeedViewController feedNetworkSource:didFinishLoadingObjects:withConfig:]_block_invoke.818
I figured out the issue with #bbum's help. The problem was with creating an instance of UIView on a non-main thread. This results in a race condition which ultimately crashes the app.

UiImagePickerController doesn't crop when using setShowsCameraControls:NO

- (IBAction)showImagePickerCamera:(id)sender {
self.picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self.picker setDelegate:self];
self.picker.allowsEditing = YES;
self.customView = [[CustomCameraViewController alloc]initWithNibName:#"CustomCameraViewController" bundle:nil];
[self.customView setCustomCameraDelegate:self];
[self presentViewController:self.picker animated:NO completion:nil];
self.needsToShowImageSource = NO;
[self performSelector:#selector(customCameraTakeAPicture) withObject:nil afterDelay:3];
[self.picker setShowsCameraControls:NO];
}
Hi all! :D I have this piece of code. I'm using setAllowsEditing:YES and setShowsCameraControls:NO. But if I set ShowsCameraControls = NO, the uiimagepickercamera doesn't allow me to crop the image.
I'm using a custom view (CustomCameraViewController) to create the buttons to take a picture, select the camera, the flash, etc. That's why i'm setting ShowsCameraControls to NO.
When I press to take a picture i'm calling
-(void)customCameraTakeAPicture
{
[self.picker setShowsCameraControls:YES];
[self.picker takePicture];
}
That takes me to the crop view but it doesn't show the square. And when i press USE the app crashes with this error :
Aug 26 04:42:50 Chad-DePues-iPod Modabound[13518] <Error>: UIImage *PLCreateCroppedImageFromImageWithQuality(UIImage *, CGRect, CGSize, CGInterpolationQuality): failed to create context
2013-08-26 04:42:50.754 Modabound[13518:861b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** setObjectForKey: object cannot be nil (key: croppedImage)'
*** First throw call stack:
(0x31c1f2a3 0x3989d97f 0x31b81313 0x36d538b7 0x36d79307 0x32535e85 0x39cf4311 0x39cf41d8)
libc++abi.dylib: terminate called throwing an exception
Any suggestions ? I've been stack for hours with this and I run out of ideas.
Thanks!

Unrecognized Selector sent to Instance in another class

I've been trying to figure out something that might be obvious and I'm missing the point or losing it. Is something wrong with my toggleStormButtons function that XCode isn't seeing it?
In my main class I have the following to call a function in another class:
STopLeftMenu *mTopLeft = [[STopLeftMenu alloc]init];
[mTopLeft drawStormToggleButton];
Then in the other class I have 2 functions:
- (void)toggleStormButtons{
[UIButton animateWithDuration:0.50 animations:^{
if (stormToggleBtn.transform.tx == 0){
[stormToggleBtn setTransform:CGAffineTransformMakeTranslation(307, 0)];
UIImage* hideButtonImg = [UIImage imageNamed:#"aiga_right_arrow_mod_hide_resize.png"];
[stormToggleBtn setBackgroundImage:hideButtonImg forState:UIControlStateNormal];
}
else{
[stormToggleBtn setTransform:CGAffineTransformMakeTranslation(0, 0)];
UIImage* showButtonImg = [UIImage imageNamed:#"aiga_right_arrow_mod_show_resize.png"];
[stormToggleBtn setBackgroundImage:showButtonImg forState:UIControlStateNormal];
}
}];
for(UIView* storm in stormButtonSaves){
[UIView animateWithDuration:0.50 animations:^{
if (storm.transform.tx == 0){
[storm setTransform:CGAffineTransformMakeTranslation(307, 0)];
storm.alpha = .65;
}
else{
[storm setTransform:CGAffineTransformMakeTranslation(0, 0)];
storm.alpha = 0;
}
}];
}
}
- (void)drawStormToggleButton{
//Storm Pullout Toggle Button
UIImage *buttonImageNormal = [UIImage imageNamed:#"aiga_right_arrow_mod_show_resize.png"];
stormToggleBtn = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 65, 75) ];
[stormToggleBtn setBackgroundImage:buttonImageNormal forState:UIControlStateNormal];
stormToggleBtn.backgroundColor = [UIColor clearColor];
stormToggleBtn.alpha = 0.5;
[stormToggleBtn addTarget:self action:#selector(toggleStormButtons) forControlEvents:UIControlEventTouchUpInside];
[viewsToRemove addObject:stormToggleBtn];
[mv addSubview:stormToggleBtn];
}
I seem to be getting an unrecognized selector message:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSMallocBlock__ toggleStormButtons]: unrecognized selector sent to instance 0x7bc9ca0'
*** First throw call stack:
(0x1b9e012 0x1966e7e 0x1c294bd 0x1b8dbbc 0x1b8d94e 0x197a705 0x8ae2c0 0x8ae258 0x96f021 0x96f57f 0x96e6e8 0x8ddcef 0x8ddf02 0x8bbd4a 0x8ad698 0x2599df9 0x2599ad0 0x1b13bf5 0x1b13962 0x1b44bb6 0x1b43f44 0x1b43e1b 0x25987e3 0x2598668 0x8aaffc 0x2285 0x2185)
libc++abi.dylib: terminate called throwing an exception
It sounds like your STopLeftMenu is being deallocated too early. The button does not retain its target, so you'll need to keep this object around as long as it needs to respond to the button's messages. If you're not sure how the object is getting deallocated, try debugging with Instruments.
I don't see anything wrong with the code you have shown. I tried it in an existing app of mine and while I had to a) declare a UIButton local variable, b) change the image used for the button and c) comment out the stuff in toggleStormButtons, the method was called every time I tapped the button, no problems.
You don't show your storage for the button. Are you using ARC? Is the button strong? If ARC it should be strong. If not ARC and you are not using a property to assign with a retain, that could cause problems.
What does viewsToRemove do? Looks like an array but it could be something else.
Why don't you use + buttonWithType: and set the frame later?

Use NSLayoutConstraints on a UITableView's header view

I am trying to add a UIView to a UITableView's header, then, using the NSLayoutConstraints I want to give it a height.
I've looked through the Apple Docs and the WWDC 2012 videos but I cannot find this particular error anywhere!
I have the following code:
- (UIImageView *)logoImageView
{
if (!_logoImageView) {
_logoImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"logo"]];
[_logoImageView setTranslatesAutoresizingMaskIntoConstraints:NO];
}
return _logoImageView;
}
... in viewDidLoad
UIView *tableHeaderView = [[UIView alloc] init];
tableHeaderView.translatesAutoresizingMaskIntoConstraints = NO;
[tableHeaderView addSubview:self.logoImageView];
[self.tableView setTableHeaderView:tableHeaderView];
NSDictionary *constraintViews = #{#"tableView" : self.tableView, #"tableHeaderView" : tableHeaderView, #"logoImageView" : self.logoImageView};
[self.tableView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:[tableHeaderView(50)]"
options:0
metrics:nil
views:constraintViews]];
However when I run it I get the following error:
2012-10-08 16:31:34.559 myApp[6934:c07] *** Assertion failure in -[UITableView layoutSublayersOfLayer:], /SourceCache/UIKit_Sim/UIKit-2372/UIView.m:5776
2012-10-08 16:31:34.561 myApp[6934:c07] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Auto Layout still required after executing -layoutSubviews. UITableView's implementation of -layoutSubviews needs to call super.'
*** First throw call stack:
(0x199b012 0x17c0e7e 0x199ae78 0x1256f35 0x7589ef 0x17d46b0 0x622fc0 0x61733c 0x622eaf 0x7f78cd 0x7401a6 0x73ecbf 0x73ebd9 0x73de34 0x73dc6e 0x73ea29 0x741922 0x7ebfec 0x738bc4 0x738dbf 0x738f55 0x741f67 0x705fcc 0x706fab 0x718315 0x71924b 0x70acf8 0x27e8df9 0x27e8ad0 0x1910bf5 0x1910962 0x1941bb6 0x1940f44 0x1940e1b 0x7067da 0x70865c 0x6d5d 0x2395)
libc++abi.dylib: terminate called throwing an exception
I had the same exception when I was trying to set the table header view.
When I noticed that the view had Contraints I just unchecked the "Use Autolayout" option from Storyboard.
Screenshot: http://i.stack.imgur.com/5jWHy.png
This solved for me.
The best way to overcome this is to use Interface Builder to setup the UITableView header, then add an Outlet to the height NSLayoutConstraint.
Try this:
logoImageView.translatesAutoresizingMaskIntoConstraints = YES;
(If not works, try to remove:
tableHeaderView.translatesAutoresizingMaskIntoConstraints = NO; //Remove this line
:)
I didn't get any proper solution for this issue but you can fix it by using frames and not setting translatesAutoresizingMaskIntoConstraints property to No (by default its yes, so don't set it)
CGRect headerViewFrame = CGRectMake(0,0,320,60); //Frame for your header/footer view
UIView *tableHeaderView = [[UIView alloc] initWithFrame:headerViewFrame];
tableHeaderView.translatesAutoresizingMaskIntoConstraints = Yes; //Or don't set it at all
[self.tableView setTableHeaderView:tableHeaderView];

UIButton calling selector crashes the app

In my app I've got a button which is an instance variable declared in the .h file already.
Now after allocating it an assigning at target to it and actually pressing the button, the app crashes giving me following output in the debugger:
-[__NSCFString handleActionSheet:]: unrecognized selector sent to instance 0x9090330
2012-05-04 17:51:02.646 financeAppV2[2595:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString handleActionSheet:]: unrecognized selector sent to instance 0x9090330'
*** First throw call stack:
(0x13d1022 0x1562cd6 0x13d2cbd 0x1337ed0 0x1337cb2 0x13d2e99 0x1e14e 0x1e0e6 0xc4ade 0xc4fa7 0xc3d8a 0x2dfa1a 0x13a599e 0x133c640 0x13084c6 0x1307d84 0x1307c9b 0x12ba7d8 0x12ba88a 0x1b626 0x29dd 0x2945)
terminate called throwing an exceptionCurrent language: auto; currently objective-c
My code looks like following:
saveButton=[UIButton buttonWithType:UIButtonTypeCustom];
[saveButton setBackgroundImage:[UIImage imageNamed:#"intervalbutton"] forState:UIControlStateNormal];
[saveButton setBackgroundImage:[UIImage imageNamed:#"intervalbuttonpressed"] forState:UIControlStateHighlighted];
saveButton.alpha=.8;
saveButton.frame= frame;
UILabel* label=[[UILabel alloc]initWithFrame:CGRectMake(0, 2, saveButton.frame.size.width, 21)];
[saveButton addSubview:label];
[saveButton addTarget:self action:#selector(handleActionSheet:) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:saveButton];
-(void)handleActionSheet:(id)sender{
NSLog(#"working");
}
Any ideas why the app could be crashing?
Without seeing the rest of your code it's difficult to be specific, but self is not correctly being retained.
You set up your button correctly (as far as I can see) and point the target to self. Unfortunately, by the time you press the button self has gone out of scope and the same memory is used by an NSString. iOS gets surprised when you send a string the handleActionSheet: message.
You're sending a message named initAllIVars: to an __NSCFString object (likely an NSString).
Do a search for that message and try to figure out why the object receiving the message is an NSString.
Just tested your code and it works. Maybe the frame it's not properly set
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *saveButton = [[UIButton alloc] init];
saveButton=[UIButton buttonWithType:UIButtonTypeCustom];
[saveButton setBackgroundImage:[UIImage imageNamed:#"intervalbutton"] forState:UIControlStateNormal];
[saveButton setBackgroundImage:[UIImage imageNamed:#"intervalbuttonpressed"] forState:UIControlStateHighlighted];
saveButton.alpha=.8;
saveButton.frame= CGRectMake(5, 5, 200, 40);
UILabel* label=[[UILabel alloc]initWithFrame:CGRectMake(0, 2, saveButton.frame.size.width, 21)];
label.text = #"Hello";
[saveButton addSubview:label];
[saveButton addTarget:self action:#selector(handleActionSheet:) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:saveButton];
}
-(void)handleActionSheet:(id)sender{
NSLog(#"working");
}