Chris
Chris

Reputation: 1750

How to create and access UI in code for iOS

Ok, so preface this with the fact that I really just started learning native iOS a few days ago, came from Titanium. I have dynamic UI (navigation elements) that will be created depending on if the user is logged in a or not. In the code below I understand how to create buttons, but I am unable to set the background image for the buttons, code hinting doesn't even give me the option. If I set a an IBOutlet in the .h file that I assign the button to it, then I have access to the setBackgroundImage method. I can't set a property in the .h file for each button if I don't know how many navButtons I am going to end up having?

Or am I approaching this in completely the wrong way? Should I be creating a separate class to handle this? As you can probably tell I am a bit lost.

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

self.iconArray = @[@"member-icon",@"join-icon",@"business-development-icon",@"referral-system-icon",@"apprenticeship-icon",@"links-icon",@"paydues-icon"];

//Find out how many views are in the iconArray
NSInteger numberOfViews = [self.iconArray count];

for (int i = 0; i < numberOfViews; i++) {

    //create the sub view and allocate memory
    UIView *navButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 102, 85)];


    [self.navScroller addSubview:navButton];


}

}

Upvotes: 0

Views: 160

Answers (1)

ngoue
ngoue

Reputation: 1055

My initial thought is to change the type declaration of your navButton from UIView to UIButton. Code hinting isn't giving you a setBackgroundImage: method because UIView doesn't have one and that's where it's looking.

Change:

UIView *navButton ...

to:

UIButton *navButton ...

As far as giving each button an action, I would suggest making a single method that has the sender as a parameter. Somewhere in your UIViewController create this method:

- (void)buttonPressed:(UIButton *)sender
{
    if (sender.currentTitle isEqualToString:@"member-icon") {
        // perform segue using segueIdentifier
    } 

    else if (sender.currentTitle isEqualToString:@"...") {
        // perform segue using segueIdentifier
    }

    // else if until all scenarios are covered

}

Then inside your for loop you'll need to do something similar to the following:

// implement inside the for loop where the button is created
for (int i = 0; i < numberOfViews; i++) {

    //create the sub view and allocate memory
    UIButton *navButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 102, 85)];

    [navButton setTitle:@"/*insert button title*/" forState:UIControlStateNormal];
    [navButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self.navScroller addSubview:navButton];
}

Another way to do it would be to have a method for each button and add the method as the action: parameter depending on which button is being created. Hope this helps...

Upvotes: 1

Related Questions