How to change subviews' properties?

Discussion in 'iOS Programming' started by mikezang, Nov 2, 2010.

  1. mikezang macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #1
    I made a UIView in IB and I want to modify some of properties of subviews in UIView, what can I do?
    For example, I want to modify the origin of button.

    Code:
    - (id)initWithFrame:(CGRect)frame {
        if ((self = [super initWithFrame:frame])) {
    
    		NSArray *keyboards = [[NSBundle mainBundle] loadNibNamed:@"ChineseRhymeKeyboard" owner:self options:nil];
    		self = [[keyboards objectAtIndex:0] retain];	
    		
    		for (UIButton *button in self.subviews) {
    //			CGRect frame = button.frame;
    //			frame.origin.y -= 10;
    //			button.frame = frame;
    //			[self addSubview:button];
    			NSLog(@"%@", button.titleLabel.text);
    		}
        }
    	
        return self;
    }
    
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Simply changing the frame of a view (even if it is the subview of another view) should work. Re-adding it as a subview again, however, is nuts.
     
  3. mikezang thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #3
    Thanks, but I found that button can be set highlight when initialize and it can't be set to highlight after I clicked it, do you know why?

    Code:
    for (int i = 0; i < 4; i++) {
    	UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    	button.frame = CGRectMake(0.0, 0.0, KEY_WIDTH, KEY_HEIGHT);
    	button.center = CGPointMake(KEY_X1 + KEY_WIDTH / 2, KEY_Y + KEY_HEIGHT * (2 * i + 1) / 2 + KEY_GAP_Y * i);
    	button.tag = (i + 1) * 100;
    			
    	if (i == 0) {
    		button.enabled = NO;
    		button.highlighted = YES;
    	}
    			
    	[button setTitle:[fourVoice objectAtIndex:i] forState:UIControlStateNormal];
    	[button addTarget:self action:@selector(selectFourVoice:) forControlEvents:UIControlEventTouchUpInside];
    
    	[self addSubview:button];
    }
    
    Code:
    -(IBAction) selectFourVoice:(id)sender {
    	UIButton *fourVoice = nil;
    	
    	for (int i = EvenTone; i <= 400; i+=100) {
    		fourVoice = (UIButton *)[self viewWithTag:i];
    		
    		fourVoice.enabled = YES;
    		fourVoice.highlighted = NO;
    	}
    	
    	fourVoice = (UIButton *)sender;
    	
    	for (int i = 1; i <= 4; i++) {
    		for (int j = 1 + 100 * i; j <= 100 * i + 10; j++) {
    			UIButton *key = (UIButton *)[self viewWithTag:j];
    			
    			[self setKeyTitle:key forTone:fourVoice.tag];
    		}
    	}
    
    	fourVoice.highlighted = YES;
    	fourVoice.enabled = NO;
    }
     

    Attached Files:

  4. mikezang thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #4
    Well, I got what I need.
     

    Attached Files:

  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    And you fixed it how?...

    If you're going to come to these forums and ask for help, you might want to consider contributing back to them by sharing your solution as well. :)
     
  6. mikezang thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #6
    Absolute! I am glad to open my source. Check here.
     

Share This Page