0

I've been stuck with an issues for a couple of days and I think it's time to ask for help.

I have an UIView class "BonusObjects" which is set as delegate to ViewController.

I add some UIImageViews in this class, start moving them towards the edge of the screen with NSTimer and try to see if they intersect with an UIImageView from ViewController.m. I'm getting the coords of the target object as int and check if the X and Y of the images from BonusObjects are anywhere near the target.

Since the images from BonusObject have X=0, I have to convert their coords from the superview. Like that:

CGPoint newPoint = [[self superview]convertPoint:internal.center 
                                        fromView:[internal superview]];

bonusX= newPoint.x;

Here is the actual question: Why the delegate doesn't work in this if statement:

if (padX < bonusX && bonusX < padX+40 && padY <= internal.frame.origin.y) {

    [[self delegate] bonusEffect:internal.tag];

    [internal removeFromSuperview];
    internal=NULL;
}

But it works if I put it out of the if, or change bonusX to some random value that isn't taken from the superview.

Everything else in that if works fine - it removes the image, and displays the correct .tag value.

I can provide more code if needed.

EDIT: The delegate is (null) in that if, but outside of it it is <ViewController: 0xc03e600>.

EDIT2: Here is how I set the delegate:

in BonusObjects.h

@protocol BonusDelegate

-(void)bonusEffect:(int)effect;

@end


@interface BonusObjects : UIView <UIApplicationDelegate> {

    id <BonusDelegate> delegate;
     } @property (nonatomic)  id <BonusDelegate> delegate;

in ViewController.h:

#import "BonusObjects.h"

@interface ViewController : UIViewController <BonusDelegate, ADBannerViewDelegate> {


     BonusObject *bonusView;
       }

And in ViewController.m:

 bonusView = [[BonusObjects alloc] init];
 [bonusView setDelegate:self];
nihilvex
  • 221
  • 3
  • 9
  • Can you add that additional code into your question. – foundry Jan 14 '13 at 14:32
  • This isn't the cause, but ` internal=NULL` is better as `internal = nil` http://stackoverflow.com/questions/557582/null-vs-nil-in-objective-c – foundry Jan 14 '13 at 14:35
  • @nihilvex, don't use the Quote button to format code. Use the Code button `{}` instead. –  Jan 14 '13 at 14:39
  • So are you saying, that everything works fine, only the `if` condition is never `true`? – foundry Jan 14 '13 at 14:42
  • No, the if condition is true when they intersect, and everything in it works fine except for the delegate. The delegate is (null) inside that if, but working outside of it. – nihilvex Jan 14 '13 at 14:44
  • IF you comment out the if() line and closing brace of the if-block, but leave all else as is,does it still get set to null? If so stick a breakpoint somewhere above it and watch the delegate, you should see exactly where this is happening. – foundry Jan 14 '13 at 17:22
  • I guess the fact that I'm using ARC causes that issue.. have to find a way to retain the delegate. – nihilvex Jan 14 '13 at 18:25

2 Answers2

2

(padX < bonusX && bonusX < padX) will always fail

foundry
  • 31,615
  • 9
  • 90
  • 125
0

I got it. In ViewController.m where I create the BonusObject I had to set the delegate for that object to self.

BonusObjects *newObj = [[BonusObjects alloc] initWithFrame:objRect];
        newObj.delegate=self;
        [self.view addSubview:newObj];

I was setting the delegate of the view to self, but not for the object itself. Works like a charm now. Thanks everyone!

nihilvex
  • 221
  • 3
  • 9