Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5958115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:29:50+00:00 2026-05-22T18:29:50+00:00

I have a viewcontroller(mainViewController) and view class(UIView class, mainView). mainViewController, I called a method

  • 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;
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T18:29:51+00:00Added an answer on May 22, 2026 at 6:29 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a viewController called MainViewController, which has a UIButton. When tapped, this button
I have my MainViewController which a UIViewController class, in this view I have a
I have a viewController (Planner) that loads two view controllers (InfoEditor and MonthlyPlan) when
I have a ViewController consisting of just a textView. In its viewDidLoad method, I
I am a little curious, I have a view controller class and an NIB/XIB
I have a View Controller that is swapping UIView objects in and out. There
i have this MainViewController, which is a controller for my table view. Whenever I
I have a viewController containing the following method: - (IBAction) playMovie { NSURL *url
I have a viewController called HaveScrollController, and this is something like this: @interface HaveScrollController
I have a modal view controller name: ComposeMessageViewController. And main view controller name: MainViewController.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.