2010-11-10 33 views
0

我是創建兩個可切換的UIViews(每個UIView有一些子視圖)的位堆棧。由於我不是IB,我想以編程的方式來做。1 UIViewController和2以編程方式切換UIViews

我已經嘗試將我的子視圖添加到我的第一個UIView,然後與第二個相同,然後切換到這樣的視圖。

 
if ([self.setView superview]) { 
    [self.setView removeFromSuperview]; 
    [self.view addSubview:contentView]; 
     self.navigationItem.title = @"BaseLine"; 
} else { 
    [self.contentView removeFromSuperview]; 
    self.view = setView; 
     self.navigationItem.title = @"Settings"; 
} 

但只有正常工作的是「標題」,沒有出現在UIViews上。 UIView似乎是好的,因爲當我使用

 
self.view = contentView; 
or 
self.view = setView; 

都正確顯示子視圖。

請有人踢我這個正確的方向。

TNX

編輯: 也指向該解決方案,也許有毛病我初始化中的loadView

 
CGRect screenRect = [[UIScreen mainScreen] applicationFrame]; 
    contentView = [[UIView alloc] initWithFrame:screenRect]; 
    setView = [[UIView alloc] initWithFrame:screenRect]; 

    self.view = contentView; 

我嘗試添加它第一次[自我。視圖中添加..],但該應用崩潰,所以就用這個

EDIT2

根控制器UITableViewController..it是在導航視圖和帶有兩個UIVies選擇一個小區此UIVieController(percView)之後被分配

 
    ShakeControl *percView = [[ShakeControl alloc] init]; 
    [self.navigationController pushViewController:percView animated:YES]; 
    [percView release]; 

EDIT3 開關由按鈕來完成,但細做,因爲我使用了很多次,它的工作

回答

3

這裏是你想要做什麼視圖控制器的一個實例(我覺得) 。這是非常基本的,只是在具有不同顏色背景的兩個視圖之間切換。但是,您可以在loadView方法中進一步自定義這些子視圖,以顯示您需要的任何內容。

接口文件:

@interface SwapViewController : UIViewController { 
    UIView *firstView; 
    UIView *secondView; 
    BOOL displayingFirstView; 
    UIButton *swapButton; 
} 
@property (nonatomic, retain) UIView *firstView; 
@property (nonatomic, retain) UIView *secondView; 
@property (nonatomic, retain) UIButton *swapButton; 

- (void)swap; 

@end 

實現文件:

#import "SwapViewController.h" 

@implementation SwapViewController 

@synthesize firstView; 
@synthesize secondView; 
@synthesize swapButton; 

- (void)loadView 
{ 
    CGRect frame = [[UIScreen mainScreen] applicationFrame]; 

    UIView *containerView = [[UIView alloc] initWithFrame:frame]; 
    containerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 
    self.view = containerView; 
    [containerView release]; 

    frame = self.view.bounds; 
    firstView = [[UIView alloc] initWithFrame:frame]; 
    firstView.backgroundColor = [UIColor redColor]; 

    secondView = [[UIView alloc] initWithFrame:frame]; 
    secondView.backgroundColor = [UIColor blueColor]; 

    self.swapButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    self.swapButton.frame = CGRectMake(10, 10, 100, 50); 
    [swapButton addTarget:self action:@selector(swap) forControlEvents:UIControlEventTouchUpInside]; 

    displayingFirstView = YES; 
    [self.view addSubview:firstView]; 
    [self.view addSubview:swapButton]; 
} 

- (void)swap 
{ 
    if(displayingFirstView) { 
     [self.firstView removeFromSuperview]; 
     [self.view addSubview:secondView]; 

     displayingFirstView = NO; 
    } else { 
     [self.secondView removeFromSuperview]; 
     [self.view addSubview:firstView]; 

     displayingFirstView = YES; 
    } 
    [self.view bringSubviewToFront:self.swapButton]; 
} 

@end 
+0

這是我原本認爲有效的代碼,但沒有。 – Vanya 2010-11-10 16:08:14

+0

你是否通過重寫loadView來初始化self.view?假設self.view不是零,它的框架大小和位置是否正確? – 2010-11-10 16:16:17

+0

是的,我重寫loadView和self.view有應用程序框架大小的視圖 – Vanya 2010-11-10 16:24:46

0

樣本代碼;


BOOL firstview = YES; // First, I have added view1 as a subview; 

if (firstview) { 
    [view1 removeFromSuperview]; 
    [self.view addSubview:view2]; 
} else { 
    [view2 removeFromSuperview]; 
    self.view addSubview:view1]; 
} 
相關問題