0

I have a viewcontroller(mainViewController) and view class(UIView class, mainView). mainViewController, I called a method "generateView" from mainView class. Here is the code:

Method 1:

- (void) generateView {
    container = [[UIScrollView alloc] initWithFrame: CGRectMake(10, 10, 500, 300)];
    container.backgroundColor = [UIColor clearColor];
    container.contentSize = CGSizeMake(500,1200);
    container.showsVerticalScrollIndicator = YES;
    [container setScrollEnabled:TRUE];
    container.backgroundColor = [UIColor redColor];
    int row = 0;
    int col = 0;
    for (int i = 0; i < 5; i++) {
        if(i % 4 == 0 && i != 0){
            row++;
            col = 0;
        }
        UIButton *b = [[UIButton alloc] initWithFrame: CGRectMake(col*120, row*120, 100, 100)];
        NSData *imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:[NSString stringWithFormat:@"%@/images/items/%i.png", HOSTADDR, i ]]];
        [b setBackgroundImage:[UIImage imageWithData:imageData] forState:UIControlStateNormal];
        [b setBackgroundColor: [UIColor colorWithRed:0x42/255.0f green:0x30/255.0f blue:0x27/255.0f alpha:1]];
        [b setTag: i];
        [b addTarget:self action:@selector(testFunction:) forControlEvents:UIControlEventTouchUpInside];
        [b setEnabled:TRUE];
        [b setUserInteractionEnabled:TRUE];
        [container addSubview:b];
        col++;
    }
    [self addSubview:container];
}


- (void) testFunction: (id) sender {
    NSLog(@"Function called. Sender tag number: %i", [sender tag]);
}

Method 2:

- (void) generateView {
    container = [[UIScrollView alloc] initWithFrame: CGRectMake(10, 10, 500, 300)];
    container.backgroundColor = [UIColor clearColor];
    container.contentSize = CGSizeMake(500,1200);
    container.showsVerticalScrollIndicator = YES;
    [container setScrollEnabled:TRUE];
    container.backgroundColor = [UIColor redColor];
    int row = 0;
    int col = 0;
    for (int i = 0; i < 5; i++) {
        if(i % 4 == 0 && i != 0){
            row++;
            col = 0;
        }
        UIButton *b = [[UIButton alloc] initWithFrame: CGRectMake(col*120, row*120, 100, 100)];
        NSData *imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:[NSString stringWithFormat:@"%@/images/items/%i.png", HOSTADDR, i ]]];
        [b setBackgroundImage:[UIImage imageWithData:imageData] forState:UIControlStateNormal];
        [b setBackgroundColor: [UIColor colorWithRed:0x42/255.0f green:0x30/255.0f blue:0x27/255.0f alpha:1]];
        [b setTag: i];
        [b addTarget:mainViewController action:@selector(updateData:) forControlEvents:UIControlEventTouchUpInside];
        [b setEnabled:TRUE];
        [b setUserInteractionEnabled:TRUE];
        [container addSubview:b];
        col++;
    }
    [self addSubview:container];
}

What I really need is method 2, cuz I want to call the updateData in mainViewController, but no matter how i tried, the button is not calling the function thats supposed to be called. Any idea?

EDIT:

This is how I called the view class

- (void) loadContent: (id) sender {
    [self removeSubmenus];
    switch ([sender tag]) {
        case 2001:
        {
            MainView *mainView = [[MainView alloc] initWithSubmenu:CGRectMake(420, 85, 0, 0)];
            [self.view addSubview:mainView];

            break;
    }   
    default:
        break;
}

}

iDaniel
  • 3
  • 3

3 Answers3

0

You should set delaysContentTouches to YES on the UIScrollView.

You're also leaking a bunch of memory. Make sure you're releasing both the UIButton (b) and NSData (imageData) objects as soon as you're done with them.

-- EDIT: I tried your code and it works fine. But it seems you wanted all this code in a UIView, so I threw together a quick sample.

TestView.h

#import <UIKit/UIKit.h>

@interface TestView : UIView {}

@end

TestView.m

#import "TestView.h"

@implementation TestView

- (id)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];
    if (self) {

        UIScrollView *container = [[UIScrollView alloc] initWithFrame: CGRectMake(10, 10, 500, 300)];
        int row = 0;
        int col = 0;
        for (int i = 0; i < 5; i++) {
            if(i % 4 == 0 && i != 0){
                row++;
                col = 0;
            }
            UIButton *b = [[UIButton alloc] initWithFrame: CGRectMake(col*120, row*120, 100, 100)];

            [b setBackgroundColor: [UIColor colorWithRed:0x42/255.0f green:0x30/255.0f blue:0x27/255.0f alpha:1]];
            [b setTag: i];
            [b addTarget:self action:@selector(testFunction:) forControlEvents:UIControlEventTouchUpInside];
            [container addSubview:b];
            [b release];
            col++;
        }
        [self addSubview:container];

    }
    return self;
}

- (void) testFunction: (id) sender {
    NSLog(@"Function called. Sender tag number: %i", [sender tag]);
}

- (void)dealloc {
    [super dealloc];
}

@end

TestController.h

@interface TestController : UIViewController { }
@end

TestController.m:

#import "TestController.h"
#import "TestView.h"
@implementation TestController

- (void) generateView {
    TestView *tv = [[TestView alloc]initWithFrame:[self.view frame]];
    [self.view addSubview:tv];
    [tv release];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self generateView];
}

- (void)dealloc {
    [super dealloc];
}

@end
csano
  • 13,266
  • 2
  • 28
  • 45
  • Noted on releasing the objects. But I tried set delaysContentTouches to yes, but still no luck sir – iDaniel May 30 '11 at 05:11
  • I created another UIButton outside the container(uiscrollview), but it still failed to call the function, do you know why? (so weird..) – iDaniel May 30 '11 at 05:16
  • Try setting setUserInteractionEnabled to YES on the scroll view. – csano May 30 '11 at 05:17
  • I already did sir, it still not called. I put a new uibutton outside the container, and it still cant call any function.. (its driving me crazy!!) – iDaniel May 30 '11 at 05:19
  • No idea what's wrong here without seeing more code. I pasted the code you provided into a new View-based project and it worked fine. – csano May 30 '11 at 05:30
  • Hmm.. Could you please write a simple call for this scenario: In a view controller, call a uiview class to generate a scrollview, inside a scrollview has 8 buttons, each button should be able to call a function (based on tag) to the view controller? – iDaniel May 30 '11 at 05:51
  • hmm... I really have no clue why mine is not working. Anyway, thanks for your time and answer =) – iDaniel May 30 '11 at 06:56
0

Try adding this code to your method

container.delaysContentTouches = NO;
container.canCancelContentTouches = NO;

Pls modify your code to avoid memory leaks. Make sure you delete objects that you take ownership of.

visakh7
  • 26,380
  • 8
  • 55
  • 69
0

Try this,

[b addTarget:mainViewController.view action:@selector(updateData:) forControlEvents:UIControlEventTouchUpInside];
Navaneethan
  • 2,125
  • 6
  • 22
  • 32
  • No luck sir. This is my updateData function in mainViewController: - (void) updateData:(id) sender { NSLog(@"Sender: %i", [sender tag]); } – iDaniel May 30 '11 at 05:08
  • Try by creating object for mainviewController or create a delegate in mainview in that call the mainviewcontroller updateData. – Navaneethan May 30 '11 at 05:28
  • If I create the button in mainViewController, then it works sir. But I want to separate the functions. I want to generate the view using UIView class, not in viewController class – iDaniel May 30 '11 at 05:32
  • try the below link http://stackoverflow.com/questions/4487840/uiviewcontroller-uiview-dealloc-not-getting-called – Navaneethan May 30 '11 at 06:01