2010-11-10 2 views
0

Je suis pile pile avec la création de deux UIViews qui sont commutables (chaque UIView a quelques sous-vues). Puisque je ne fais pas IB, je veux le faire par programmation.1 UIViewController et 2 UIViews de commutation par programme

J'ai essayé d'ajouter mes sous-vues à mon premier UIView, puis la même chose avec la seconde, puis de passer à la visualisation comme ceci.

 
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"; 
} 

mais seule chose qui fonctionne correctement était « titre » et rien ne semblait sur le UIViews. L'UIView semble être OK parce que quand j'utilise

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

les deux sous-vues correctement.

Pls quelqu'un me donner un coup de pied dans la bonne direction à ce sujet.

TNX

EDIT: pointant également la solution, peut-être quelque chose de mal avec mon initialisation dans le loadview

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

    self.view = contentView; 

J'ai essayé de l'ajouter à l'auto [. vue ajouter ..] mais l'application est écrasé, alors j'ai utilisé cette

EDIT2

le contrôleur racine est UITableViewController..it est en mode navigation et après avoir choisi deux UIVies est attribué une cellule ce UIVieController (percView)

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

EDIT3 interrupteur se fait par le bouton, mais il est bien fait parce que j'ai utilisé plusieurs fois et cela a fonctionné

Répondre

3

Voici un exemple d'un contrôleur de vue qui fait ce que vous voulez (je pense) . C'est très basique, il suffit de basculer entre deux vues avec des arrière-plans de couleurs différentes. Mais, vous pouvez personnaliser davantage ces sous-vues dans la méthode loadView pour afficher tout ce dont vous avez besoin.

Le fichier d'interface:

@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 

Le fichier de mise en œuvre:

#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

C'est le code que je pensais initialement qui fonctionne mais ne fonctionne pas. – Vanya

+0

Avez-vous initialisé self.view en remplaçant loadView? En supposant que self.view n'est pas nul, son cadre est-il dimensionné et positionné correctement? –

+0

oui je remplace loadView et self.view a vue avec la taille de l'applicationFrame – Vanya

0

exemple de code;


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]; 
} 
Questions connexes