gpt4 book ai didi

objective-c - iOS : transitionFromView - label in 'toView' not displayed

转载 作者:行者123 更新时间:2023-11-28 17:33:37 26 4
gpt4 key购买 nike

在 UIView 容器中的两个 subview 之间切换时出现奇怪的错误。请注意,这两个 subview 是相同的 View Controller 类型(相同的 View ),唯一的区别是标签,它由属性定义。

当从 box1 -> box2 切换(动画)时,不显示 box2 中的标签文本。但是,如果我除了在 box2 上设置标签文本之外,还更改 View 的背景颜色,则标签文本会在动画后显示。

在我看来,当只定义标签文本时,绘制 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];

我是不是忘记了什么?

最佳答案

因此,我设置了以下场景:

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,我将其绑定(bind)到 -(IBAction)SwitchViews:(id)sender .

唯一让我印象深刻的是,当我激活 [UIView transition...] 时,我设置的帧会保持不变。我在您提供的代码中没有看到指定框架的任何地方。

除此之外,在我的代码中,我得到一个蓝色矩形 (20,20,200,200),单击按钮将其翻转以显示橙色矩形 (40,40,200,200)。

这对你有帮助吗?

关于objective-c - iOS : transitionFromView - label in 'toView' not displayed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10487591/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com