1

在 UIView 容器中的两个子视图之间切换时,我遇到了一个奇怪的错误。注意,这两个子视图是相同的视图控制器类型(相同的视图),唯一的区别是标签,它是由一个属性定义的。

从 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];

视图将出现

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

分段控制已更改

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

我忘记了什么吗?

4

1 回答 1

0

所以,我设置了以下场景:

视图控制器.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

视图控制器.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)。

这对你有帮助吗?

于 2012-05-07T21:47:43.693 回答