creating UI elements through a method...

Discussion in 'iOS Programming' started by WillH16, Apr 16, 2010.

  1. WillH16 macrumors newbie

    Joined:
    Apr 16, 2010
    #1
    Hey guys, I'm new to iPhone programming, and what I'm trying to do is basically use a method to build out my UI. In this case, it's dependent on the number of names a user entered into a table view. Sorry if I'm not too clear as I'm not too good with some terminology.

    Code:
    -(void)initPlayerWithName:(UILabel *)player but1:(UIButton *)b1 but2:(UIButton *)b2 but3:(UIButton *)b3 ycoord:(int)y andIndex:(int)i scoreSource:(NSMutableArray *)s{
    	AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    	
    	player = [[UILabel alloc] initWithFrame:CGRectMake(0, y, 90, 40)];
    	player.text = [[appDelegate playerDataSource] objectAtIndex:i];
    	player.backgroundColor = [UIColor greenColor];
    	
    	b1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    	[b1 addTarget:self action:@selector(enterScore:) forControlEvents:UIControlEventTouchUpInside];
    	[b1 setTitle: @"-" forState:UIControlStateNormal];
    	b1.frame = CGRectMake(91, y, 50, 40);
    	b1.tag = i * 1 + 10;
    	b2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    	[b2 addTarget:self action:@selector(enterScore:) forControlEvents:UIControlEventTouchUpInside];
    	[b2 setTitle: @"-" forState:UIControlStateNormal];
    	b2.frame = CGRectMake(142, y, 50, 40);
    	b2.tag = i * 2 + 20;
    	b3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    	[b3 setTitle: @"-" forState:UIControlStateNormal];
    	[b3 addTarget:self action:@selector(enterScore:) forControlEvents:UIControlEventTouchUpInside];
    	b3.frame = CGRectMake(193, y, 50, 40);
    	b3.tag = i * 3 + 30;
    }
    I've created the ivars for the buttons, labels, and arrays and I'm trying to build them into the UI using that method.

    Code:
    [self initPlayerWithName:player1 but1:button1 but2:button2 but3:button3 ycoord: 199 andIndex:0 scoreSource:p1Score];
    However, when the method gets called this way, I don't think the ivars are being properly referenced because when I call, for example, [player1 text], I get a null return. Ditto for [button1 currentTitle] and everything else. I assume it's because it just builds it out without assigning any IBOutlets...or something like that.

    So is there a way for me to basically tell the method, "Hey, I already have the UI elements set up, I just need you to build them out for me". Much appreciated!
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    If you don't know how many buttons there are going to be then don't try and create ivars for them: use a loop and store the references in a NSMutableArray.
     

Share This Page