Creating a CGRect as an Associated Object? - ios

I'm attempting to create a CGRect property for my UIButton's as an associated object, so that I don't have to subclass UIButton just for this.
Basically I'm adding a property titled, tapArea, and I'm having trouble creating it. This is what I have so far:
#dynamic tapArea;
- (void)setTapArea:(CGRect)tapArea {
objc_setAssociatedObject(self, #selector(tapArea), [NSValue valueWithCGRect:tapArea], OBJC_ASSOCIATION_ASSIGN);
}
- (CGRect)tapArea {
return [objc_getAssociatedObject(self, #selector(tapArea)) CGRectValue];
}
When I NSLog say dogButton.tapArea, the logger prints out {{0,0},{0,0}} which makes sense. The problem arises when I attempt to set it. I get the following crash:
-[__NSCFString CGRectValue]: unrecognized selector sent to instance 0x14e65e80
I believe I'm getting this error bc I'm using NSValue, but I'm not pulling it back out correctly. How can I fix my getter?
P.S. This is how I'm declaring my property in my category header file:
#property (nonatomic, assign) CGRect tapArea;
Also, how can I set my property equal to the button's frame by default instead of {{0, 0}, {0, 0}}?

You're working awfully hard to avoid subclassing a UIButton, which is pretty damn easy and quick. Don't make it more complicated than necessary.
Also, an NSValue is an NSObject which should be retained not assigned. Your storage semantic is incorrect, which is probably why you are having problems. Your associated object is an object and needs to be retained like an object.
Use OBJC_ASSOCIATION_RETAIN_NONATOMIC

Related

animator.startAnimation -- What does this warning mean?

I'm trying to learn how to use UIViewPropertyAnimator in objc. I made a simple test app with an object called 'blueBox'. I want to vary the properties of blueBox.
I declare 'animator' outside of #implementation ... #end:
UIViewPropertyAnimator *animator;
then define it like so:
- (void)viewDidLoad {
[super viewDidLoad];
CGRect newFrame = CGRectMake(150.0, 350.0, 100.0, 150.0);
animator = [[UIViewPropertyAnimator alloc]
initWithDuration:2.0
curve:UIViewAnimationCurveLinear
animations:^(void){
self.blueBox.frame = newFrame;
self.blueBox.backgroundColor = [UIColor redColor];
}];
}
When I want to use it I write:
animator.startAnimation;
It works as expected (changes the object's color and frame) but there is a warning on 'animator.startAnimation;' that says "Property access result unused - getters should not be used for side effects". What property access result is that referring to? How should I write that so I don't get a warning?
startAnimation is a method, not a property. You should write:
[animator startAnimation];
Though Objective-C does allow you to use property syntax when calling a method that takes no parameters, your use is written like you are attempting to read a property value. But since (obviously) you make no attempt to store the result (there isn't one), the compiler complains you are ignoring the accessed value.
Simply avoid the wrong syntax and you avoid the issue.
BTW, you claim that the line:
UIViewPropertyAnimator *animator;
is outside the #implementation / #end pair. That makes it a file global variable. Is that what you really want? If you want it to be an instance variable of the class (which is probably what you really want), it should be:
#implementation YourClass {
UIViewPropertyAnimator *animator; //instance variable
}
// your methods
#end

UIPicker View subclassed not working IOS

Being new to objective-C coding I started out writing a basic app, fully programmatically (not using storyboards or xib) in one file, my AppViewController h and m files.
Everything worked lovely.
So then I wanted to break up the mass of code by subclassing sections, and everything went well apart from the UIPickerView. In fact simply commenting out the [background addSubview:colorPicker]; seemed to totally fix the issue. I never found the answer online so I proceeded to make a new document to replicate said issue.
So here goes:
UIPickerViewController.h
#import <UIKit/UIKit.h>
#import "Picker.h"
#interface UIPickerViewController : UIViewController
#end
Simply imports my new class.
UIPickerViewController.m
#import "UIPickerViewController.h"
#interface UIPickerViewController ()
#end
#implementation UIPickerViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *superview = self.view;
int height = superview.bounds.size.height;
int width = superview.bounds.size.width;
CGRect popupRect = CGRectMake(0, 0, width, height);
UIView *popup = [[UIView alloc]initWithFrame:popupRect];
popup.tag = 8;
[superview addSubview:popup];
Picker *picker = [[Picker alloc]initWithFrame:popupRect];
[picker viewAddTypeScreenToView:superview];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#end
Sets up a new view with a tag (so that i could reference it later with my new class)
Then actions a method from my new class to populate my new view.
Picker.h
#import <UIKit/UIKit.h>
#interface Picker : UIView
<UIPickerViewDataSource,UIPickerViewDelegate>
{
UIPickerView *colorPicker;
NSMutableArray *colorsArray;
}
#property (nonatomic, retain) UIPickerView *colorPicker;
#property (nonatomic, retain) NSMutableArray *colorsArray;
#property (strong,nonatomic) UILabel *myValue;
-(void)viewAddTypeScreenToView:(UIView*)superview;
#end
Setting up my variables and accessible method.
Picker.m
#import "Picker.h"
#implementation Picker
#synthesize colorsArray;
#synthesize colorPicker;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
-(void)viewAddTypeScreenToView:(UIView*)superview
{
UIView *baseView =[superview viewWithTag:8];
int height = baseView.bounds.size.height;
int width = baseView.bounds.size.width;
CGRect fullScreen = CGRectMake(0, 0, width, height);
UIView *background = [[UIView alloc]initWithFrame:fullScreen];
background.backgroundColor = [UIColor blackColor];
colorsArray = [[NSMutableArray alloc] initWithObjects:#"Red",#"Blue",#"Yellow",#"Green",nil];
CGRect myPickerRect = CGRectMake(10, 70, (width/2)-40, 200);
colorPicker = [[UIPickerView alloc]initWithFrame:myPickerRect];
colorPicker.dataSource = self;
colorPicker.delegate = self;
colorPicker.showsSelectionIndicator = YES;
[colorPicker selectRow:2 inComponent:0 animated:YES];
CGRect labelFrame = CGRectMake(10, 10, 180, 50);
_myValue = [[UILabel alloc]initWithFrame:labelFrame];
_myValue.textColor = [UIColor redColor];
_myValue.text = #"select colour";
[background addSubview:_myValue];
[background addSubview:colorPicker];
[baseView addSubview:background];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return colorsArray.count;;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return colorsArray[row];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
_myValue.text = [NSString stringWithString:colorsArray[row]];
}
#end
And finally the initiation called by the method in the picker class file.
This gives me an error along these lines
-[UITableViewCellContentView pickerView:titleForRow:forComponent:]: unrecognized selector sent to instance 0x8f2b000
2014-03-19 10:29:48.407 Briefcase[1800:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewCellContentView pickerView:titleForRow:forComponent:]: unrecognized selector sent to instance 0x8f2b000'
Which i've read is to do with either the datasource, or ARC systems, however none of the responses that I have found relate to or work with the type of set up that I have above. I'm sure it's something really simple but after a few days of failed searching, it's officially driving me crazy.
The problem is most likely that the instance of Picker that is being created in UIPickerViewController is never added to the view hierarchy and thus gets released prematurely (provided we're talking about a project using ARC here).
This leads to the pickerview's delegate and datasource becoming invalid and, basically, pointing at any random object. That's what is causing your crash: A message to your delegate cannot be delivered because the delegate is dead already. The picker still keeps a pointer which used to point at the delegate, but which has become invalid and points at a random object now, in this case a table view cell, which basically doesn't know what to do with this message and crashes.
The problem should go away if you add Picker *picker as an ivar or a retaining / strong property to UIPickerViewController.h - this will retain the picker beyond the scope of the viewDidLoad method and should keep it alive.
But that would be just a workaround, the real problem is your overall design. You said you're new to objective-c and indeed, it looks like you lack a basic understanding of iOS view and view controller hierarchies and, to some degree, the concept of object oriented programming. You might want to dig into something more basic before trying to fix your code because, quite frankly, it should be rather re-written than fixed.
I'd be happy to provide you with suggestions about how to structure your code, but please provide some information about what functionality you'd like to achieve first.
Edit (in response to your comment):
As a rule of thumb, do not spread functionality over several classes unless necessary. For objects, which serve a rather infrastructural purpose, like a specialized textfield or a pickerview, always ask yourself: "If I would like to reuse that object in another project, would that be as easy as using any other existing object, like, for example, UILabel?" If the answer is "No", then something is wrong. Ideally, interface objects are self-contained and to use them, you just invoke them, add them to a view and tell them, which text to display or which options to offer. If that information is subject to change or if the object needs to interact with other parts of your code, make use of delegation and protocols. Under no circumstances should the functionality of your object be tied to hard coded values or rely to some view to have a certain tag.
If you subclass UIView, the resulting object should behave like any other instance of UIView. It should be added to the view hierarchy by you or some object, but it shouldn't add or remove itself. If it works without being added to the view hierarchy at all, something is wrong. A view serves the purpose of being a part in your interface and all the logic it contains should work to that end, not more, not less.
Normally, interface objects should not interfere with one another. If something happens to one object (button pressed, option selected, text changed...) and another object is supposed to reflect that change, it is the view controllers responsibility to make that happen. The view controller is the place where the logic happens. If there is a task which requires a lot of complex logic, it might be a good idea to encapsule that logic into a purpose build class. One such example would be a class which manages network connections. This class should be again self contained: If the view controller needs some remote information, it asks your network class. Once your network class has that information (or failed to retrieve it), it reports back to your view controller. The view controller then updates the interface - under no circumstance should the networking class contain code which affects the interface.
It is important to understand that you could very well ignore these rules and still end up with a working app. And in some cases, the "direct" way may appear to be easier to implement and thus may look very tempting. But you'll pay the price later - once you start debugging your code. If your picker does not behave the way it should, you need to look into several places and wrap your mind around several objects, just to make one interface object behave right. And likely you will break one functionality while fixing the other.
So, try to make it right from the start, even though it requires more planning and learning. Trust me, it pays out, I started out just like you several years ago ;)

Unrecognized selector setting alpha for a UIImageView

I'm getting an unrecognized selector exception trying to set the alpha on a UIImageView, but I don't understand why this should be seeing a UIImageView inherits from UIView and alpha is a property of that.
#property (strong, nonatomic) IBOutlet UIImageView *pulldownTab;
...
self.pulldownTab.alpha = 1.0;
2013-05-21 07:33:32.964 TestControl[655:907] -[__NSCFNumber setAlpha:]: unrecognized selector sent to instance 0x1e587fa0
** UPDATE **
Moral of the story - Solved. be careful with key value coding, a call to this function was being made earlier
- (void) fadeToAlphaValue: (double) alpha forKeyPath: (NSString*) keyPath
{
NSNumber* number = [NSNumber numberWithDouble:alpha];
[UIView animateWithDuration:kFadeAnimationDuration
animations:^ {
[self setValue:number forKeyPath:keyPath];
}];
}
[self fadeToAlphaValue:0.0 forKeyPath:#"self.pulldownTab"];
Are you sure you are assigning the IBOutlet properly in the interface Builder?
The reference is not properly working, when you access to self.pulldownTab is accessing to a NSCFNumber variable in memory.
I had the same problem here. In my case, I was setting myTextView, but not the .text property, earlier in the code, like this:
self.myTextView = #"text";
instead of what I should have been doing:
self.myTextView.text = #"text";
for some reason this didn't throw any errors at this point, but it created an issue later when I tried to set the alpha property on an NSString (which doesn't have such a thing) :)
Anyways, just look for any stupid mistakes like this in your code before the .alpha property is being set - they'll likely be the cause of the problem!

UIScrollView loses reference to delegate

I need to create a number of UIScrollViews dynamically and fill them with content. This is all good except when i set the delegate to self and pan the list i get this exception:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString scrollViewDidScroll:]: unrecognized selector sent to instance 0x7581230'
NSCFString obviously isn't my view controller (which implements the protocol UIScrollViewDelegate) so from what i gather somehow the memory gets messed up and it doesn't keep the reference correctly. Occasionally this can be something else too which strongly points to something being wrong with the memory
Here's the code to create the list:
for (NSUInteger i = 0; i < self.stories.currentStory.selectableWordCount; i++) {
UIScrollView *list = [[UIScrollView alloc] init];
list.alwaysBounceVertical = YES;
list.showsVerticalScrollIndicator = NO;
list.clipsToBounds = NO;
list.delegate = self;
list.pagingEnabled = YES;
[self.view addSubview:list];
.. // add UILabels to the list, set the frame, contentSize etc
[self.wordLists addObject:list]; // this is a #property (nonatomic, strong) NSMutableArray, declared in a private interface()
}
If i NSLog the delegate it's correct. respondsToSelector also matches fine. Interestingly if i comment out the scrollViewDidScroll: respondsToSelector: doesn't match any more and (probably because of this) the UIScrollView won't attempt to call this method any more. This then means that it can reach the delegate correctly to check for the method availability but when it gets called something goes wrong.
I'm targeting iOS5 with ARC. If this wasn't the case i would assume that i messed something up with the memory myself but now i don't have the same control.
I'm having a hard time debugging this issue, any help on how to proceed would be appreciated
D'uh. I was obviously looking in the wrong place. The view controller was added through a .xib and the view was pointing to a subview on the stage. However i needed to create an IBOutlet to the view controller in the main view controller to make sure it stays in memory. Hopefully this can help somebody else with a similar problem :)

weird problem with UILabel

I am using UILabel for custom cells in my UITableView. Heres all the code that I am using :
header file:
UILabel *timeLabels;
#property (nonatomic, retain) UILabel *timeLabels;
code file:
- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier
timeLabels=[[UILabel alloc] init];
timeLabels.textAlignment=UITextAlignmentLeft;
timeLabels.font=[UIFont boldSystemFontOfSize:12];
timeLabels.backgroundColor=[UIColor clearColor];
timeLabels.textColor=[UIColor blueColor];
- (void) layoutSubviews
frame=CGRectMake(boundsX+5, 5, 60, 45);
timeLabels.frame=frame;
[timeLabels release]
I am getting the following error on timeLabels.frame=frame;
2011-08-08 12:44:07.290 EncameoApp[2014:707] -[NSCFString setFrame:]: unrecognized selector sent to instance 0x136890
2011-08-08 12:44:07.361 EncameoApp[2014:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString setFrame:]: unrecognized selector sent to instance 0x136890'
Which is pretty strange given that timeLabels is not a NSString, but rather a UILabel !
Can anyone please let me know what I missed here ? Thanks.
I also faced this kind of problem, but i had solved this problem by set value of timeLabels as following:-
timeLabels.text = #"value";
instead of
timeLabels = #"value";
The code snippet you show is correct, anyway my guess is that you have very possibly a memory problem that makes your UILabel instance to be released at some point before layoutSubviews is executed, then that memory is reused by an NSString, so you get the error there.
In my experience, the most common case for this to happen is anyway erroneously overwriting timeLabels with the wrong value could produce the same result. This could be done within the class or from another class (that maybe tries to set the label value).
If you want to make a simple test, add
NSLog(#"timeLabels address %x", timeLabels);
both to init and to layoutSubviews to compare the two values and see that they differ (or maybe they don't, in this case you would have a memory corruption problem).
You should inspect your code, and post more of it if you need more help.

Resources