我有一个UISegmentedControl,其中第一个控件将显示UITextView中的文本,而第二个控件显示可滚动的UIImageView。
在初次启动时,如果我切换到第二个控件,则显示图像并切换回第一个控件,该图像消失并且显示UITextView。
但是,当我第二次切换到第二个控件并切换回第一个控件时,图像仍然存在,并且无法再显示UITextView。
我的代码在第一个控件中显示了隐藏图像并显示文本的位置,反之亦然。
为什么在第一次在控件之间切换时它第一次起作用,但第二次却不起作用?
我究竟做错了什么?
谢谢
-(void)viewDidLoad
{
self.scrollView.delegate = self;
self.textView.text = @"THIS IS A TEST. THIS IS A TEST. THIS IS A TEST. THIS IS A TEST. THIS IS A TEST. THIS IS A TEST.";
self.textView.hidden = NO;
}
-(void)setScroller
{
CGSize scrollableSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
[self.scrollView setContentSize:scrollableSize];
self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"test.png"] ];
self.imageView.frame = CGRectMake(0, 0, self.scrollView.frame.size.width, self.view.frame.size.height);
self.scrollView.backgroundColor = [UIColor blackColor];
self.scrollView.minimumZoomScale = 1.0 ;
self.scrollView.maximumZoomScale = self.imageView.image.size.width / self.scrollView.frame.size.width;
//self.scrollView.zoomScale = 1.0;
[self.scrollView addSubview:self.imageView];
}
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.imageView;
}
- (IBAction)segmentedControl:(UISegmentedControl *)sender
{
if (self.segmentedControl.selectedSegmentIndex == 0)
{
// Display apppropriate info for About
self.imageView.hidden = YES;
self.textView.hidden = NO;
}
else
{
self.imageView.hidden = NO;
self.textView.hidden = YES;
[self setScroller];
}
}
最佳答案
您应该从[self setScroller];
方法中删除- (IBAction)segmentedControl:(UISegmentedControl *)sender
,然后将其放在-(void)viewDidLoad
中。每次切换到第二段时,您都在呼叫[self setScroller];
。
您的代码应如下所示:
-(void)viewDidLoad
{
[super viewDidLoad];
self.scrollView.delegate = self;
[self setupScroller];
}
-(void)setupScroller
{
// Set contentSize
CGSize scrollableSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
self.scrollView.contentSize = scrollableSize;
// Add textView
self.textView.text = @"THIS IS A TEST. THIS IS A TEST. THIS IS A TEST. THIS IS A TEST. THIS IS A TEST. THIS IS A TEST.";
self.textView.hidden = NO;
// Add ImageView
self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"test.png"]];
self.imageView.frame = CGRectMake(0, 0, self.scrollView.frame.size.width, self.view.frame.size.height);
self.imageView.hidden = YES;
[self.scrollView addSubview:self.imageView];
// Configure Zoom Scales and backgroundColor
self.scrollView.backgroundColor = [UIColor blackColor];
self.scrollView.minimumZoomScale = 1.0 ;
self.scrollView.maximumZoomScale = self.imageView.image.size.width / self.scrollView.frame.size.width;
}
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.imageView;
}
- (IBAction)segmentedControl:(UISegmentedControl *)sender
{
if (self.segmentedControl.selectedSegmentIndex == 0)
{
// Display appropriate info for About
self.imageView.hidden = YES;
self.textView.hidden = NO;
}
else
{
self.imageView.hidden = NO;
self.textView.hidden = YES;
}
}
关于ios - 为什么使用UISegmentedControl时UIImageView没有隐藏?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28135369/