所以我有一个叫做MyTabBarViewController的UIViewController子类,它有一个UIScrollView。 MyTabBarViewController的内部我创建了另一个名为PhotoViewController的UIViewController子类的实例。 (注:我这样做,所以我可以使用IB设置IBOutlets)另一个UIViewController中的UIViewController的新实例:为什么我不能设置实例变量?
我想从我的TabBarViewController设置每个PhotoViewController实例的标签。并且我为每个PhotoViewController初始化了一个nib,所以我的印象是每个PhotoViewController实例都会连接到它们各自的IBOutlets - 这使我可以简单地使用pvc.label.text = @“我想要的文本”来设置标签名称。
你能解释为什么我的逻辑不正确吗?因为它不工作,不确定该怎么做。 : -/
MyTabBarViewController.m
#import "MyTabBarViewController.h"
@implementation MyTabBarViewController
@synthesize pageControl,scroller;
-(IBAction)clickPageControl:(id)sender
{
int page=pageControl.currentPage;
CGRect frame=scroller.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
[scroller scrollRectToVisible:frame animated:YES];
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
int page = scrollView.contentOffset.x/scrollView.frame.size.width;
pageControl.currentPage=page;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
scroller.delegate=self;
scroller.pagingEnabled=YES;
scroller.directionalLockEnabled=YES;
scroller.showsHorizontalScrollIndicator=NO;
scroller.showsVerticalScrollIndicator=NO;
scroller.contentSize=CGSizeMake(pageControl.numberOfPages*scroller.frame.size.width, scroller.frame.size.height);
CGFloat scrollWidth = 0;
int pageNumber = 0;
for (int i=0; i<3; i++)
{
PhotoViewController *pvc = [[PhotoViewController alloc] initWithNibName:@"PhotoViewController" bundle:nil];
CGRect rect = scroller.frame;
rect.size.height = scroller.frame.size.height;
rect.size.width = scroller.frame.size.width;
rect.origin.x = scroller.frame.origin.x + scrollWidth;
rect.origin.y = scroller.frame.origin.y;
pvc.label.text = [NSString stringWithFormat:@"%d", pageNumber];
pvc.label.textColor = [UIColor redColor];
pvc.view.frame = rect;
[scroller addSubview:pvc.view];
[pvc release];
pageNumber++;
scrollWidth += scroller.frame.size.width;
}
pageControl.numberOfPages=3;
pageControl.currentPage=0;
[self.view addSubview:scroller];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
PhotoViewController.h是相当直接的。而PhotoViewController.m也是,但我已经包含了执行文件,如果我的问题在那里。
PhotoViewController.m
#import "PhotoViewController.h"
@implementation PhotoViewController
@synthesize label, imageView, sendButton, cancelButton;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
感谢Ishu :-)我得到了关于'viewDidLoad'的第一部分答案,但对于答案的第二部分我还是有点不清楚。我将如何设置MyTabBarViewController中的值?如果以后我想访问PhotoViewController的属性并设置标签,为什么我需要在MyTabBarViewController中设置值?我只问,因为当我在MyTabBarViewController中设置值时,如果稍后在PhotoViewController中设置它们,听起来像是浪费了一步。非常感谢Isha的最初反馈! :-) – NateHill
如果您在MyTabBarViewController中创建属性,那么答案的第二部分将有所帮助。如果你在photoViewController中创建属性,那么你不需要。我想我的答案MyTabBarViewController类的属性。 – Ishu
最稳健的方法是在每个PhotoViewController中设置独立的属性,以便从viewDidLoad引用。 (如果您可以使用父类中的常用值,只需在子对象中设置父属性并使用它来引用父对象。)这涵盖了由于内存不足而导致手机在某一时刻卸载视图控制器的极小机会。但是,如果您不介意这种微不足道的可能性,只需在设置标签之前参考小孩的“视图”即可。 –