2013-01-14 2 views
0

코코아에서 프로그래밍 방식으로 컴포넌트를 추가 할 수 있습니까? UI는 Xcode의 인터페이스 빌더를 사용하여 작성됩니다. 난 그냥 NSButtons을 추가하고 싶지만 그 수량과 위치는 런타임과 사용자 입력에서 알 수 있습니다.코코아에서 컴포넌트를 동적으로 추가하는 방법

누구나 알고 있을까요? 어떻게 할 수 있고 어떻게 이러한 ynamic 구성 요소를 배치 할 수 있습니까?

답변

1

물론 가능합니다. 하위 뷰를 추가 :

UIView *subview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)]; // or UIButton, UIScrollView, or any other view-based class you make think of 
[self addSubview:subview]; 

아니면 이런 식으로 뭔가 될 것 버튼에 대한 더 정확하게하기 :

UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
aButton.frame = CGRectMake(0, 0, 100, 100); 
[aButton addTarget:self action:@selector(methodName) forControlEvents:UIControlEventTouchUpInside]; 
[self addSubview:aButton]; // if done from the view self.view if done from the controller 

아, 죄송합니다 그냥, OSX 아니었다 발견 iOS이지만 기본 사항은 동일합니다. 대신 NSButton 클래스를 살펴보십시오.

NSButton *aButton = [[NSButton alloc] initWithFrame:NSMakeRect(0, 0, 100, 100)]; // x, y, width, height 

시작해야합니다.

관련 문제