iOS/iPhone :UIView not adding elements for NSMutableArray (containing UILabel objects) - uiview

I have a UIView in which i have written the following method:
- (void)drawLabels:(NSMutableArray*)labelArray{
for (int i=0; i<[labelArray count]; i++) {
CGRect labelFrame = CGRectMake(0, 0, 50, 50);
labelFrame = CGRectInset(labelFrame, 5, 5);
UILabel *label = (UILabel*)[labelArray objectAtIndex:i];
[self addSubview:label];
}
The code crashes with the following error :
-[UIDeviceRGBColor superview]: unrecognized selector sent to instance 0x6e397c0
2012-11-27 18:37:33.559 QuartzSample[57621:f803] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIDeviceRGBColor superview]: unrecognized selector sent to instance 0x6e397c0'
* First throw call stack:
(0x13cf022 0x1560cd6 0x13d0cbd 0x1335ed0 0x1335cb2 0x55e4f 0x5014b 0x4398 0x38a1 0xe0a1e 0x3f401 0x3f670 0x3f836 0x4672a 0x292b 0x17386 0x18274 0x27183 0x27c38 0x1b634 0x12b9ef5 0x13a3195 0x1307ff2 0x13068da 0x1305d84 0x1305c9b 0x17c65 0x19626 0x2632 0x25a5)
terminate called throwing an exception(lldb)
I wish to place the labels on the view with all the formatting(color, font,size,etc.).
Also when i put the following code :
NSLog(#"%#",[labelArray objectAtIndex:i]);
i get the following in console:
UIDeviceRGBColorSpace 0 1 0 0.3
Can anybody elaborate ?

It seems you are settings labelFrame but you are not using it.
Before using addSubView, ensure label is not nil
To configure the font and size of a UILabel use setFont: method passing a UIFont as parameter
To configure the color use .textColor property passing a UIColor as parameter

Related

Error caused by unrecognized selector while trying to pass an argument to a void

Thanks to developers from Stack, I was able to spot the origin of my previous problem, which occurs in a different file of my app (I am thus writing a new post).
I am trying to code a chat app for fun and I wrote a custom view class to display all the sent and received messages. I am calling this class in my main view controller using viewWithTag in the following line of code: MessageView *messageView = (MessageView *)[cell viewWithTag:MESSAGE_VIEW_TAG];.
The MessageView class has one property called transcript and containing all the information of the message. I also set the transcript property of the MessageView class in my main view controller. However, I'm having trouble using it in the implementation file for MessageView.
I am getting the error Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewCell setTranscript:]: unrecognized selector sent to instance 0x7fc77afe1070', which I don't really understand... Here is my setTranscript method (don't read everything, I assume the error is only coming from the first line).
- (void)setTranscript:(Transcript *)transcript
{
// Set the message text
NSString *messageText = transcript.message;
_messageLabel.text = messageText;
// Compute message size and frames
CGSize labelSize = [MessageView labelSizeForString:messageText fontSize:MESSAGE_FONT_SIZE];
CGSize balloonSize = [MessageView balloonSizeForLabelSize:labelSize];
NSString *nameText = transcript.peerID;
CGSize nameSize = [MessageView labelSizeForString:nameText fontSize:NAME_FONT_SIZE];
// Comput the X,Y origin offsets
CGFloat xOffsetLabel;
CGFloat xOffsetBalloon;
CGFloat yOffset;
if ([transcript.direction isEqualToString:#"right"]) {
// Sent messages appear or right of view
xOffsetLabel = 320 - labelSize.width - (BALLOON_WIDTH_PADDING / 2) - 3;
xOffsetBalloon = 320 - balloonSize.width;
yOffset = BUFFER_WHITE_SPACE / 2;
_nameLabel.text = #"";
// Set text color
_messageLabel.textColor = [UIColor whiteColor];
// Set resizeable image
_balloonView.image = [self.balloonImageRight resizableImageWithCapInsets:_balloonInsetsRight];
}
}
From the alert message I get in the debugger area, I assume that transcript isn't defined correctly in my header (which I was careful to import in the implementation). So here's what my header file looks like:
#import <UIKit/UIKit.h>
#import "Transcript.h"
#class Transcript;
#define MESSAGE_VIEW_TAG (99)
#interface MessageView : UIView
#property (nonatomic, strong) Transcript *transcript;
#end
Does anyone know why I am getting this error message ? What is wrong with my argument, transcript ? Thank you very much by advance!!
It seems that you have a custom tableview cell where in which you have written a property Transcript which has a setter setTranscript.
It seems that you dequeueReusableCellWithIdentifier method in cellForRowAtIndexPath is returning UITableViewCell instead of you custom cell.
So try to set an identifier for the cell in the table view and in cellForRowAtIndexPath retrieve the cell with the same identifier.
looks like you're calling setter in UITableViewCell, but should call in MessageView.
Could you show code used to call "setTranscript"?

Terminating app due to uncaught exception 'CALayerInvalidGeometry', reason: 'CALayer position contains NaN

When I'm clicking button I'm getting this exception.I don't know where I'm doing wrong Someone help me .my app is rejected because of this exception
my code:
UILabel *rememberMeLabelObj = [[UILabel alloc]initWithFrame:CGRectMake(xaxisForRememberMeLabel, yaxisForRememberMeLabel, widthForRememberMeLabel, heightForRememberMeLabel)];
rememberMeLabelObj.text = #"Remember me";
rememberMeLabelObj.font =[UIFont systemFontOfSize:14.0];
rememberMeLabelObj.textColor = [UIColor colorWithRed:0.816f green:0.592f blue:0.157f alpha:1.00f];
[self.view addSubview:rememberMeLabelObj];
You are calling UILabel frame methods, and probably calculating the value somehow and the result is a NaN, which means "not a number" and, as you correctly suggested, it can be the result of o divide-by-zero operation.
But it can appear for a lot other reasons: http://en.wikipedia.org/wiki/NaN
Reference: kuba
I solved my issue actually I didn't gave float values because of this I got exception.I gave CGFloat and gave values as integer like 400,350,etc but I need give 400.0f .so,I got this issue .Now it was cleared
My iOS App has the same problem when the simulator version is 8.x, it is no problem in version 9.x,after I have checked and removed the following code, the problem is solved!
- (void)viewWillAppear:(BOOL)animated
{
[LoadTableView setContentOffset:CGPointMake(CGFLOAT_MAX, CGFLOAT_MAX)];
[super viewWillAppear:YES];
}
- (void)viewDidAppear:(BOOL)animated
{
[LoadTableView reloadData];
LoadTableView.hidden = 0;
[super viewDidAppear:YES];
}

How can I reset the motion effect of a UIView correctly?

I have extended the UIView class in order to implement my own quick method for adding a motion effect in a UIView:
#import "UIView+Extensions.h"
#implementation UIView (Extensions)
// ...
- (void)setMotion:(CGPoint)motion
{
// Remove all motion effects.
if (self.motionEffects.count > 0) {
[self removeMotionEffect:[self.motionEffects objectAtIndex:0]];
[self removeMotionEffect:[self.motionEffects objectAtIndex:1]];
}
// Add motion effects.
UIInterpolatingMotionEffect *horizontalMotionEffect = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:#"center.x" type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
UIInterpolatingMotionEffect *verticalMotionEffect = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:#"center.y" type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
[self addMotionEffect:horizontalMotionEffect];
[self addMotionEffect:verticalMotionEffect];
// Set motion effect values.
horizontalMotionEffect.minimumRelativeValue = #(-motion.x);
horizontalMotionEffect.maximumRelativeValue = #(motion.x);
verticalMotionEffect.minimumRelativeValue = #(-motion.y);
verticalMotionEffect.maximumRelativeValue = #(motion.y);
}
I'm getting a counter-intuitive error.
* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 ..
0]'
*** First throw call stack: (0x28d3749f 0x3652ec8b 0x28c4bc35 0x7ab1f 0x79e95 0x7d983 0x7a39f 0x6969f 0x7ddf3 0x2c1f9d0f 0x2c1f9a7d
0x2c1ff953 0x2c1fd3bd 0x2c26760d 0x2c45951b 0x2c45b98b 0x2c466209
0x2c45a217 0x2f4c80d1 0x28cfdd7d 0x28cfd041 0x28cfbb7b 0x28c493c1
0x28c491d3 0x2c25e1bf 0x2c258fa1 0x7f425 0x36aaeaaf) libc++abi.dylib:
terminating with uncaught exception of type NSException
Which means that somehow this line of code:
[self removeMotionEffect:[self.motionEffects objectAtIndex:0]];
Removes both effects instead of just the first one.
How is that even possible?
And this is not the worst part....
As you might suggest, changing the code to this:
// Remove all motion effects.
if (self.motionEffects.count > 0) {
[self removeMotionEffect:[self.motionEffects objectAtIndex:0]]; // Should remove the first, or both but removes the second one.
}
Should fix the problem since the line of code above will remove both.
IT DOES NOT. It actually removes the vertical effect, the second one!
So, each time you reset the motion the horizontal axis adds-up and goes
faster and faster.
This is beyond reasoning.
After the first
[self removeMotionEffect:[self.motionEffects objectAtIndex:0]];
self.motionEffects will contain only 1 object, so you can't use objectAtIndex:1 because you'll get the out of bounds exception...
Use
[self.motionEffects firstObject]
or some loops to do it...
Actually when the object at index zero is removed, the index of other items in the array is changed. For example, index of item at index 1 will be changed to 0 after removing the object at index 0. You can make this code work just by swapping the two lines in the condition if (self.motionEffects.count > 0)
Swift 4
extension UIView {
func stopParallax() {
for motion in motionEffects {
removeMotionEffect(motion)
}
}
}

Unrecognized selector sent to instance 0xa44bef0

Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[UISlider
setPopUpViewCornerRadius:]: unrecognized selector sent to instance
0xa44bef0'
Code:
self.slider1.maximumValue = 255.0;
self.slider1.popUpViewCornerRadius = 25.0;
[self.slider1 setMaxFractionDigitsDisplayed:0];
self.slider1.popUpViewColor = [UIColor colorWithHue:0.55 saturation:0.8 brightness:0.9 alpha:0.7];
self.slider1.font = [UIFont fontWithName:#"GillSans-Bold" size:22];
self.slider1.textColor = [UIColor colorWithHue:0.55 saturation:1.0 brightness:0.5 alpha:1];
Please guide me, Thanks in Advance
UISlider doesn't have this property popUpViewCornerRadius, because of that this error is occurring. You will need to remove this below line.
self.slider1.popUpViewCornerRadius = 25.0;
When you are allocating self.slider you need to use ASValueTrackingSlider instead of UISlider.
if you are using story board or xib you need to give ASValueTrackingSlider in identity inspector. like
http://cocoadocs.org/docsets/ASValueTrackingSlider/0.9.2/Classes/ASValueTrackingSlider.html refer the link.
UiSlider does not have this property of
popUpViewCornerRadius
Either remove this line else this property would be included in the Custom class.
So change the UISlider *slider1 to ASValueTrackingSlider *slider1

– drawInRect:withAttributes: crashes when setting UIFont

I have this code:
UIGraphicsBeginImageContextWithOptions(CGSizeMake(100, 100), NO, 0);
UIFont* font = [UIFont systemFontOfSize:12.0f];
NSDictionary* attributes = #{ NSFontAttributeName: font };
[#"hello" drawInRect:CGRectMake(0, 0, 100, 100) withAttributes:attributes];
UIGraphicsEndImageContext();
It crashes in drawInRect with
-[__NSDictionaryI font]: unrecognized selector sent to instance 0xcb77be0
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryI font]:
unrecognized selector sent to instance 0xcb77be0'
It works fine if I use deprecated
[#"hello" drawInRect:CGRectMake(0, 0, 100, 100) withFont:font];
What am I doing wrong?
The reason is you deployed your app on old iOS versio (<7.0) which didn't support method drawInRect:withAttributes:. Also, that's why you could run the deprecated one without problem.

Resources