2009-07-22 7 views
1

나는 분명히 여기있는 바보 같은 짓을하고있다. 요약하면 단추 (세그먼트 단추 사용)를 클릭하면 하위 뷰를 프로그래밍 방식으로 토글하고 싶습니다. 그것은 움직일 필요가 없습니다. 이상 적으로는 펜촉에서 이러한 뷰의 내용을로드합니다.동적으로 UIView의 하위보기를 설정하는 방법 (iPhone)?

Nib에서 초기화 된 UIViewController 하위 클래스를 navigationController로 푸시합니다. 이보기 컨트롤러 & "기본"보기가 표시됩니다. 이 펜촉에는 다음이 포함되어 있습니다.

File's Owner [Class=my view controller subclass] 
First Responder 
View 
--Bar Segmented Control 
--View //this is the 'subview' whose content I want to toggle 

아마도 내 IB 매장과 실수를 했나요?

switchableView <--> View 
view <--> View 

을 어디 switchableView 선언 :

IBOutlet UIView *switchableView; 
... 
@property (nonatomic, retain) UIView *switchableView; 

이 나는이있는 UIView의 내용을 대체에 붙어 다음과 같이 그들은 매여있다. 우선,보기가 처음 생기면 기본값으로 설정하려고합니다. 이미 세그먼트 화 된 버튼에 의해 생성 된 이벤트를 캡처 할 수 있으므로 다른 것으로 교체하는 것이 쉬워야합니다. 누군가 내가 잘못하고있는 것을 말해 줄 수 있습니까?

- (void)loadView { 
NSLog(@"loadView invoked"); 
[super loadView]; 
UIView *view = [[UIView alloc] init]; 
UILabel *label = [[UILabel alloc] init]; 
label.text = @"Segment1"; 
[view addSubview:label]; 
[switchableView addSubview:view]; //this is what I can't work out 
[view release]; 
[label release];  
} 

(viewDidLoad처럼)하지만, 둘 다 도움이 방법 호출된다. 나는 [switchableView addSubview:view]을 잘못하고있는 것 같아요. 나는 또한 switchableView = view을 시도했지만, Java와 Perl의 안전성에있어 오랜 시간이 걸리더라도 시도 할 권리가 있는지 없는지 100 % 확신 할 수 없습니다!

누군가 나를 똑바로 세울 수 있기를 바랍니다. 미리 감사드립니다.

답변

3

은 결국이 문제를 해결 (일부 포인터 나에게주는 여러분 모두에게 감사 포함) UICatalog 샘플 코드를 검사 한 후. 다음 작품 : 다음도 속성으로 정의된다

- (void)loadView { 
    [super loadView]; 

    //define what will go in the seg1 view 
    UILabel *mylabel = [[UILabel alloc] initWithFrame:switchableView.frame]; 
    [email protected]"finally"; 
    self.seg1view = [[[UIView alloc] initWithFrame:switchableView.frame] autorelease]; 
    [self.seg1view addSubview:mylabel]; 
    [mylabel release]; 

    //define what will go in the seg2 view 
    UILabel *mylabel2 = [[UILabel alloc] initWithFrame:switchableView.frame]; 
    [email protected]"it works!"; 
    self.seg2view = [[[UIView alloc] initWithFrame:switchableView.frame] autorelease]; 
    [self.seg2view addSubview:mylabel2]; 
    [mylabel2 release]; 

    //default the switchable view to seg1view 
    [switchableView addSubview:self.seg1view]; 
} 

-(void)doTheSwitch { 
    if (segmentedButton.selectedSegmentIndex == 0) { 
     [seg2view removeFromSuperview]; 
     [switchableView addSubview:seg1view]; 
    } 
    if (segmentedButton.selectedSegmentIndex == 1) {  
     [seg1view removeFromSuperview]; 
     [switchableView addSubview:seg2view]; 
    } 
} 

(유지, 세분화)

IBOutlet UIView *switchableView; 
UIView *seg1view; 
UIView *seg2view; 
+0

그냥 FYI : "이 메서드를 사용자 지정 구현하면 super를 호출해서는 안됩니다." http://developer.apple.com/library/ios/documentation/uikit/reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/instm/UIViewController/loadView –

0

다음은 몇 가지 발췌 내용입니다. 첫 번째는 단순히 내비게이션 컨트롤러의 초기화입니다.

// In AppDelegate.m 
-(void)applicationDidFinishLaunching:(UIApplication *)application 
{ 
    // Create and configure the main view controller. 
    UIViewController *firstViewController = [[UIViewController alloc] init]; 

    // Add create and configure the navigation controller. 
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:firstViewController]; 
    self.navController = navigationController; 
    [navigationController release]; 

    // Configure and display the window. 
    [window addSubview:navController.view]; 
    [window makeKeyAndVisible]; 
} 

그런 다음 새로운보기가 준비되면 바로 가기를 누릅니다.

// Wherever you need to get your new view on there 
UIViewController *secondViewController = [[UIViewController alloc] init]; 
[[self navigationController] pushViewController:secondViewController animated:YES]; 
[secondViewController release]; 

아이폰 세계에 능통 해지기 위해서는 하루에 약 5 개의 애플 샘플 프로젝트를 진행해야합니다. 초보자를위한이 체크 아웃 : TableSearch.

+0

건배,하지만이없는 나는 후에 누구인지. 이미 뷰 컨트롤러가 있는데 이미 뷰를 표시하고 있습니다. 새 뷰 컨트롤러를 스택에 푸시하고 싶지 않습니다. 내가하고 싶은 일은 뷰 컨트롤러를 사용하여 내 뷰에 포함 된 하위 뷰를 교체하는 것입니다. –

0

가장 큰 문제는보기에서 프레임을 설정하지 않는다는 것입니다. 즉, 프레임이 성공적으로 첨부되는 동안 크기가 0x0입니다. 이 (A UILabel의이 UIView의 당신은 전체 프레임을 커버하는 하나의 서브 뷰하지만 아무것도 2 세 개의보기를 적재 한 무상 때문에

- (void)loadView { 
    NSLog(@"loadView invoked"); 
    [super loadView]; 

    CGRect labelFrame = CGRectZero; 
    labelFrame.size.height = switchableView.size.height; 
    labelFrame.size.width = switchableView.size.width; 

    UILabel *label = [[UILabel alloc] initWithFrame:labelFrame]; 
    label.text = @"Segment1"; 
    [switchableView addSubview:label]; //this is what I can't work out 
    [label release];  
} 

또한 제거보기 :

같은 것을보십시오.) UIViewController보기 속성을 생략했습니다 (일반적으로 인스턴스 변수 또는 속성과 같은 이름의 로컬 변수를 만드는 것은 좋지 않습니다.이를 피하기 위해 설정할 수있는 컴파일러 경고가 있습니다).

0

질문을 올바르게 이해하면 다른보기로 전환하는 중입니다.이를 수행하는 한 가지 방법은 다음과 같습니다.

switchView라는 루트보기에 UIView를 추가하십시오. 코드 또는 인터페이스 빌더에서 어느 subView1라는 새에 UIView를 작성하고 루트 뷰로드 할 때 switchView의 서브 클래스로 추가 :

사용자가 루트 뷰에서 분할 제어를 전환
[switchView addSubview:subView1];

, 새로운 UIView의를 만들 및 subView2라는 비슷한 방식으로 switchView의 현재 하위 뷰로 전환 :

[subView1 removeFromSuperview]; 
[switchView addSubview:subView2];
관련 문제