1

當在UIView容器中的兩個子視圖之間切換時,出現奇怪的錯誤。 請注意,這兩個子視圖具有相同的視圖控制器類型(相同視圖),唯一的區別是由屬性定義的標籤。iOS:transitionFromView - 'toView'中的標籤不顯示

從box1 - > box2切換(動畫)時,box2中的標籤文本未顯示。但是,如果我除了在box2上設置標籤文本,還要更改視圖的背景顏色,動畫之後顯示標籤文本。

在我看來,就像在繪製box2時出現問題一樣,只有標籤文本被定義時。怎麼來的?

viewDidLoad中

// Define the to sub vies and the container 
box1 = [[MyBoxViewController alloc] initWithNibName:@"MyBoxViewController" bundle:nil]; 
box2 = [[MyBoxViewController alloc] initWithNibName:@"MyBoxViewController" bundle:nil]; 
containerView = [[UIView alloc] initWithFrame:PercentageGroupView.frame]; 

[containerView addSubview:box1.view]; 
[self.view addSubview:containerView]; 

viewWillAppear中

box1.title = "Box nr. 1"; 
box2.title = "Box nr. 2"; 

segmentedControlChanged

[UIView transitionFromView:box1.view 
        toView:box2.view 
        duration:1 
        options:UIViewAnimationOptionTransitionFlipFromBottom 
       completion:nil]; 

有我忘記了什麼?

回答

0

所以,我設置了以下方案:

ViewController.h

#import <UIKit/UIKit.h> 
#import "MyBoxViewController.h" 
@interface ViewController : UIViewController 
@property (strong, nonatomic) MyBoxViewController *activeBox; 
@property (strong, nonatomic) MyBoxViewController *box1; 
@property (strong, nonatomic) MyBoxViewController *box2; 
- (IBAction)SwitchViews:(id)sender; 
@end 

ViewController.m

#import "ViewController.h" 
@implementation ViewController 
@synthesize activeBox; 
@synthesize box1; 
@synthesize box2; 
- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Define the to sub views and the container 
    [self setBox1:[[MyBoxViewController alloc] initWithNibName:@"MyBoxViewController" bundle:nil]]; 
    [[[self box1] view] setBackgroundColor:[UIColor blueColor]]; 
    [[[self box1] titleLabel] setText:@"Box Number 1"]; 
    [[[self box1] view] setFrame:CGRectMake(20, 20, 200, 200)]; 
    [self setBox2:[[MyBoxViewController alloc] initWithNibName:@"MyBoxViewController" bundle:nil]]; 
    [[[self box2] view] setBackgroundColor:[UIColor orangeColor]]; 
    [[[self box2] titleLabel] setText:@"Box Number 2"]; 
    [[[self box2] view] setFrame:CGRectMake(40, 40, 200, 200)]; 
    UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 400, 400)]; 
    [containerView addSubview:[[self box1] view]]; 
    [self setActiveBox:[self box1]]; 
    [[self view] addSubview:containerView]; 
} 
- (IBAction)SwitchViews:(id)sender { 
    if ([self activeBox] == [self box1]) { 
     // switch from 1 to 2 
     [UIView transitionFromView:[[self box1] view] 
          toView:[[self box2] view] 
          duration:1 
          options:UIViewAnimationOptionTransitionFlipFromBottom 
         completion:nil]; 
     [self setActiveBox:[self box2]]; 
    } else { 
     // switch from 2 to 1 
     [UIView transitionFromView:[[self box2] view] 
          toView:[[self box1] view] 
          duration:1 
          options:UIViewAnimationOptionTransitionFlipFromBottom 
         completion:nil]; 
     [self setActiveBox:[self box1]]; 
    } 
} 
@end 

MyBoxViewController.h

#import <UIKit/UIKit.h> 
@interface MyBoxViewController : UIViewController 
@property (strong, nonatomic) IBOutlet UILabel *titleLabel; 
@end 

MyBoxViewController.m

#import "MyBoxViewController.h" 
@implementation MyBoxViewController 
@synthesize titleLabel; 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 
@end 

請注意,我ViewController.xib,我添加了一個UIButton我綁-(IBAction)SwitchViews:(id)sender

唯一突出的是當我激活[UIView transition...]時,我設置的框架得以保留。我沒有看到您提供的框架指定的代碼中的任何地方。

除此之外,在我的代碼中,我得到一個藍色矩形(20,20,200,200)並單擊該按鈕將其翻轉以顯示橙色矩形(40,40,200,200)。

這對你有幫助嗎?