Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mikezang

macrumors 6502a
Original poster
May 22, 2010
939
41
Tokyo, Japan
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;
}
 
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.
 
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.
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;
}
 

Attachments

  • SnapShot 2010-11-03 at 22.20.12.jpg
    SnapShot 2010-11-03 at 22.20.12.jpg
    79.9 KB · Views: 78
  • SnapShot 2010-11-03 at 22.20.19.jpg
    SnapShot 2010-11-03 at 22.20.19.jpg
    79.6 KB · Views: 84
Well, I got what I need.
 

Attachments

  • SnapShot 2010-11-04 at 0.10.16.jpg
    SnapShot 2010-11-04 at 0.10.16.jpg
    79.7 KB · Views: 93
  • SnapShot 2010-11-04 at 0.10.21.jpg
    SnapShot 2010-11-04 at 0.10.21.jpg
    79.8 KB · Views: 80
  • SnapShot 2010-11-04 at 0.10.26.jpg
    SnapShot 2010-11-04 at 0.10.26.jpg
    78.6 KB · Views: 77
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. :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.