Pass Button object to method - objective-c

I am trying to write a generic method that would allow me to pass the following: "x", "y" "object" and then have it move. Currently I have this:
-(void) changeObjectLocations: (integer_t) xSpot: (integer_t) ySpot: (id) sender {
if (![sender isKindOfClass:[UIButton class]])
{
UIButton *myObject = (UIButton *)sender;
[UIView animateWithDuration:1.5
animations:^{
CGRect newFrame = myObject.frame;
newFrame.origin.x = xSpot;
newFrame.origin.y = ySpot;
myObject.frame = newFrame;
}];
}
else if (![sender isKindOfClass:[UILabel class]])
{
UILabel *myObject = (UILabel *)sender;
[UIView animateWithDuration:1.5 animations:^{
CGRect newFrame = myObject.frame;
newFrame.origin.x = xSpot;
newFrame.origin.y = ySpot;
myObject.frame = newFrame;
}];
}
}
I then want to call it like so:
-(void) orientationBlockLandscape {
[self changeObjectLocations: 456 :282 : btn1] ;
[self changeObjectLocations: 391 :227 : lblTitle] ;
}
Although it is working, on compile I get the following warning:
SecondViewController.m:33: warning: 'SecondViewController' may not respond to '-changeObjectLocations:::'
Is there a better way I can/should be passing the object? Thanks in advance for any and all help.
Geo...

Based on the warning outputted, it sounds like you didn't define changeObjectLocations in the header of your SecondViewController -- or it's not the same signature as what you've implemented.

The compiler looks for selectors which are the method definitions with the variables and return removed.
So a method like:
-(void) setObject:(id) anObject forKey:(NSString *) keyname;
... would have a selector of:
setObject:forKey:
Therefore, your method of:
-(void) changeObjectLocations: (integer_t) xSpot: (integer_t) ySpot: (id) sender
... will have a selector of:
changeObjectLocations:xSpot:ySpot:
Note that the parameter names are part of the selector so:
changeObjectLocations:xSpot:ySpot:
and
changeObjectLocations:::
.. are two entirely separate selectors which represent two entirely separate methods.
Although it is legal in the language to use parameters without names e.g. ":::" it is very, very poor practice largely because it is to easy to get a naming collision. Being explicit not only makes the code easier to read and maintain but makes it easier for the complier and runtime to function.

Related

Calling Obj-C Methods

I want to call a method B using a different method A. The problem is that the aksed parameters of method B is not present in Method A.
 
here's what I've tried ..
-(void) methodA {
// some code
CGSize *size = [CGSizeMake(self.frame.size.width, self.frame.size.height)];
[self methodB:size];
}
-(void) methodB:(CGSize) size {
//some code
}
There is certainly a better way...
Thanks
I don't think your code will be compiled.
CGSize is not an object.
Refactor to:
CGSize size = CGSizeMake(self.frame.size.width, self.frame.size.height);
[self methodB:size];
You should only wrap [] around methods. CGSizeMake(self.frame.size.width, self.frame.size.height) is a function, not a method, so get rid of the square braces. You can tell it's a function because functions look like functionName(argument1, argument2, ...) whereas methods look like [object methodName:argument1 methondNameContinued:argument2].
Further, CGSizeMake returns a CGSize structure, not a pointer to a CGSize structure, so ditch the * in *size.
Then you'll be left with this:
CGSize size = CGSizeMake(self.frame.size.width, self.frame.size.height);
[self methodB:size];
Which is correct.

Calling only parts of a method

If I have a method called
-(void) changeButton:(UIButton *)button andAlpha:(float)alpha andEnabled:(BOOL)enabled {
button.alpha = alpha;
button.enabled = enabled;
}
Is there anyway I can call only [self changeButton:buttonName andAlpha:0.5]; and miss out andEnabled(BOOL) enabled so that it will stay the same value.
No, only if you declare other methods.
like
-(void) changeButton:(UIButton *)button andAlpha:(float)alpha {
[self changeButton:button andAlpha:alpha andEnabled:button.enabled];
}
-(void) changeButton:(UIButton *)button andAlpha:(float)alpha andEnabled:(BOOL)enabled {
button.alpha = alpha;
button.enabled = enabled;
}
But remember, that this approach is not always good. For example enabled property could be backed up by some custom setter which will be called even if you don't want to change that value.
You can't do this, you must declare another method.
-(void) changeButton:(UIButton *)button andAlpha:(float)alpha;
I am sure you are asking about default parameterized function of c++.
But Objective-C doesn't support this.
You can create 2 methods though:
-(void) changeButton:(UIButton *)button andAlpha:(float)alpha {
button.alpha = alpha;
}
-(void) changeButton:(UIButton *)button andAlpha:(float)alpha andEnabled:(BOOL)enabled {
button.alpha = alpha;
button.enabled = enabled;
}
For C : there's nothing special added to the C subset by using ObjC. Anything that cannot be done in pure C can't be done by compiling in ObjC either. That means, you can't have default parameters, nor overload a function. Create 2 functions instead.
One alternate way(bit lengthy, as someone will use rarely) is to have a flag and check the flag for YES/NO.
-(void) changeButton:(UIButton *)button andAlpha:(float)alpha andEnabled:(BOOL)enabled withFlag:(BOOL)flag{
button.alpha = alpha;
if(flag){
button.enabled = enabled;
}
}

primitive accessors in this example

Could someone help me understand the primitive accessors with this example : i don't understand what is automatically set and the order of those methods :
1.after a person is created, is willSave the first method called? (i guess so, because save: is called after we create a person with insertNewObjectForEntityForName )
2.in RootViewController (the second chunk of code), we then call the getter of eyeColor with : person.eyeColor :
a) in eyeColor, we call : [self eyeColorData] ,
b) but setPrimitiveEyeColorData is in willSave, which is accessible only if primitiveEyeColor exists,
c) but setPrimitiveEyeColor is in eyeColor and only called if [self eyeColorData] exists. So, i'm a bit confused with this code, could someone help me?
here's the code about eyeColor and eyeColorData :
#dynamic eyeColorData;
#dynamic eyeColor;
#interface AWPerson (PrimitiveAccessors)
- (UIColor *)primitiveEyeColor;
- (void)setPrimitiveEyeColor:(UIColor *)value;
- (NSData *)primitiveEyeColorData;
- (void)setPrimitiveEyeColorData:(NSData *)value;
#end
+ (id)personInManagedObjectContext:(NSManagedObjectContext *)moc {
return [NSEntityDescription
insertNewObjectForEntityForName:#"Person"
inManagedObjectContext:moc];
}
+ (id)randomPersonInManagedObjectContext:(NSManagedObjectContext *)moc {
AWPerson *randomPerson = [self personInManagedObjectContext:moc];
//...
randomPerson.eyeColor = [self randomColor]; //setter eyeColor
return randomPerson;
}
+ (UIColor *)randomColor {
static NSArray *colorsArray = nil;
if( !colorsArray ) {
colorsArray = [[NSArray alloc] initWithObjects:
[UIColor lightGrayColor],
[UIColor blueColor],
[UIColor greenColor], nil];
}
int randomIndex = arc4random() % [colorsArray count];
return [colorsArray objectAtIndex:randomIndex];
}
- (void)willSave {
UIColor *color = [self primitiveEyeColor];
if( color ) {
[self setPrimitiveEyeColorData:
[NSKeyedArchiver archivedDataWithRootObject:color]];
} else {
[self setPrimitiveEyeColorData:nil];
}
[super willSave];
}
- (UIColor *)eyeColor {
[self willAccessValueForKey:#"eyeColor"];
UIColor *tmpValue = [self primitiveEyeColor];
[self didAccessValueForKey:#"eyeColor"];
if( tmpValue ) return tmpValue;
NSData *colorData = [self eyeColorData];
if( !colorData ) return nil;
tmpValue = [NSKeyedUnarchiver unarchiveObjectWithData:colorData];
[self setPrimitiveEyeColor:tmpValue];
return tmpValue;
}
in RootViewController :
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
AWPerson *person = [[self fetchedResultsController] objectAtIndexPath:indexPath];
[cell setBackgroundColor:person.eyeColor];
}
Thanks
EDIT - Added info on willSave
To answer your first question, willSave is called whenever the object is saved (using the save method). So the first method called will be one of the class methods (used to create the object) or init and then, since you said that the object is saved just after it is created, willSave gets called.
I think the key to understanding this is to realize that eyeColor, primitiveEyeColor, and their setters are all ultimately interacting with the same variable in memory (the iVar named eyeColor). The difference is whether or not the code in the setter/getter (in this case the - (UIColor *)eyeColor { function) is called.
There are just a few different ways to interact with it:
[self primitiveEyeColor]; - This reads the value of the iVar directly.
[self setPrimitiveEyeColor:tmpValue]; - This sets the value of the iVar directly.
[self eyeColor] - This calls the - (UIColor *)eyeColor method in your class (which should ultimately retrieve the iVar or a representation of it).
[self setEyeColor:value] - This calls the - (void)setEyeColor:(UIColor *)newColor method in your class. Note that in this case it doesn't exist so it simply calls the primitive method (and does the KVO magic).
In this particular code, they are using a "non-standard persistent attribute" because NSManagedObject does not support UIColor's. Read about it here.
EDIT 2
To answer your other questions:
a) The color in randomPerson.eyeColor = [self randomColor] is
accessible with [self primitiveEyeColor] (in willSave)?
Yes, once eyeColor is set (either via the setEyeColor method or the setPrimitiveEyeColor method), you can read it from primitiveEyeColor and it will return the same value.
Note that once it is set, eyeColor and primitiveEyeColor return the same value and can be called from anywhere in your class (not just willSave).
b) So if [self primitiveEyeColor] != nil : in eyeColor, the line :
if( tmpValue ) return tmpValue; should therefore always be true...
when can we unarchive eyeColorData if UIColor *tmpValue = [self
primitiveEyeColor] is always returned in -(UIColor *)eyeColor?
This method only looks at eyeColorData (which was stored during the last call to willSave) if eyeColor is nil. This is an optimization because we could skip all of this and just unarchive eyeColorData every time if we wanted to. In this case, once a value is unarchived or set to a new value, it always stores that value and returns it so that we don't have to call unarchive again.
Also, there is really what I believe to be an error here (although it could be by design). Let's say that we perform the following steps:
Set eyeColor to a random color (let's say blue).
save the object.
Set eyeColor to nil
Now, if you check the color using [self eyeColor] it will see that primitiveEyeColor is nil and unarchive eyeColorData again, therefore returning the blue color that was stored previously. You should probably be over-riding the set function so that it sets eyeColorData to nil when eyeColor is set to nil. That way, checking the value of eyeColor after setting it to nil will return nil as expected.

Setting Actions for PopUpButton

I have created an NSPopUpButton programmatically and I made an array for my choices, how can I create a setAction for each individual array choice? Thanks!
NSRect buttonRect = NSMakeRect(1705, 145, 78, 50);
//Button Array. When I pick the choice it closes the diologue box
NSArray *newArray;
NSString *color1 = #"Blue Color";
NSString *color2 = #"Green Color";
NSString *color3 = #"Clear Color";
newArray = [NSArray arrayWithObjects: color1, color2, color3, nil];
NSPopUpButton *button = [[NSPopUpButton alloc] initWithFrame:buttonRect pullsDown:YES];
[self addSubview:button];
[button addItemsWithTitles:newArray];
//want my action for each individual string
[button setAction:#selector(changeFilterColor)];
-(void) changeFilterColor
{
NSLog(#"colorChanged");
}
You need to add the NSMenuDelegate protocol to your interface (.h file):
#interface MyClass : NSObject <NSMenuDelegate>
Then:
[[button menu]setDelegate:self];
after you create the NSPopUpButton. Also, remove the line with setAction:.
Copy this delegate method:
-(void)menu:(NSMenu *)menu willHighlightItem:(NSMenuItem *)item
{
if ([item.title isEqualToString:#"Blue Color"]) { ... } //etc
}
Add the necessary if statements to complete the comparisons.
Another option for this is the use the setAction method you originally had in your code. But then for the selector you choose to call set it up to receive a sender object. Something like this:
-(void)method:(NSMenuItem *)sender
Then you can verify that the sender object is valid and get the title from that instead. I'm a little wary of using NSMenuDelegate for this.

Using NSString value to refer to UILabel

Have mercy, newbie here.
I have a NSString value that I construct on the fly, which is the name of a UILabel instance. I want to send the label a message to update its text. But, the two data types don't match. Here's enough code (I think):
In header file:
IBOutlet UILabel *Clue1; // IBOutlet and IBAction are IDE flags
IBOutlet UILabel *Clue2; // IB = interface builder
IBOutlet UILabel *Clue3;
In implementation file:
- (IBAction) newPuzzle:(id)sender { // Clear all fields & get new clue
[Clue1 setText:#""]; // Clear the fields
[Clue2 setText:#""];
[Clue3 setText:#""];
// Send up a randomly chosen new clue
NSArray *clues = [NSArray arrayWithObjects:#"222", #"333", nil];
NSInteger randomIndex = arc4random()%[clues count];
NSString *aClue = [clues objectAtIndex:randomIndex];
// The clue will be split into component digits and each piece sent to a different label
for (NSInteger charIdx = 0; charIdx < aClue.length; charIdx++) {
NSString *cluePos = [NSString stringWithFormat:#"Clue%d", charIdx + 1];
NSLog(#"%#", cluePos); // works
[cluePos setText:#"test"]; // Xcode notes the type mismatch
}
}
There are some similar questions on SO, but none are close enough for me to recognize that they apply to my case, at least as far as I can tell. Using the terminology of another language (R), I need to "coerce" the class of cluePos from NSString to UILabel. I'm on Xcode 4.2.1 and OSX 10.7.2.
TIA.
You can't coerce a string into a label because they are fundamentally different. The string doesn't have any knowledge of your view controller class or it's properties (some of which happen to be labels).
You can however use the valueForKey: method to get a property of an object by name, where the name is specified as a string. So to get a property called Clue1 on my view controller I'd say:
UILabel *label = [self valueForKey:#"Clue1"];
Or in your case, this:
NSString *cluePos = [NSString stringWithFormat:#"Clue%d", charIdx + 1];
UILabel *label = [self valueForKey:cluePos];
label.text = #"test";
(I'm assuming 'self' in this case refers to the view controller, but you can call this on any object that has properties.)
Another way to do this is to turn your string into a selector using NSSelectorFromString. That would look like this:
SEL selector = NSSelectorFromString(#"Clue1");
UILabel *label = [self performSelector:selector];
For your purposes either solution works equally well, however the advantage of using the selector is that you can pass arguments to the method call (so you could call a method that returns an object, not just access a property or IBOutlet).
Note that both of these methods will raise an exception if you try to access a property or call a method that doesn't exist. You can test if the property exists before calling it by saying:
SEL selector = NSSelectorFromString(#"Clue1");
BOOL labelExists = [self respondsToSelector:selector];
if (labelExists)
{
UILabel *label = [self performSelector:selector];
label.text = #"test";
}
else
{
//do something else
}