另一个 UIViewController 中的 UIViewController 的新实例:为什么我不能设置实例变量?



所以我有一个名为MyTabBarViewController的UIViewController子类,它有一个UIScrollView。在MyTabBarViewController内部,我正在创建另一个名为PhotoViewController的UIViewController子类的实例。(注意:我这样做是为了使用IB设置IBOutlets)

我正在尝试从TabBarViewController中设置每个PhotoViewController实例的标签。我为每个PhotoViewController初始化了nib,所以我觉得每个PhotoViewControl实例都会连接到它们各自的IBOutlets——这让我可以简单地使用pvc.label.text=@"text I want"设置标签名称。

你能解释一下为什么我的逻辑不正确吗?因为它不起作用,也不知道该怎么办。:-/

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非常直接。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

您不能像这样设置任何与视图相关的值(从该视图中生成对象并设置其他条目)。

因为不能在任何视图控制器的viewDidLoad之前设置任何级别的值。您需要相应地为标签设置字符串类型的属性,并在MyTabBarViewController中设置它们的值,然后从堆栈中在PhotoViewController中拾取MyTabBarView Controller类的对象,然后访问它的属性并设置标签。

为了从堆栈中拾取视图对象,您需要使用这行

MyTabBarViewController *obj = (MyTabBarViewController *)[self.navigationController.viewControllers objectAtIndex: [self.navigationController.viewControllers count]-2];

IBOutlet实体在viewDidLoad之前不存在,而这通常在您启动演出之前不会发生。因此,在MyTabBarViewController中,您正在寻址一个不存在的标签等。(当然,Objective-C很方便地忽略了对零指针的调用,所以看起来一切都正常——只是什么都没发生。)

根据规范,您可以通过引用视图控制器的视图属性来触发加载,但我从未尝试过。

相关内容

  • 没有找到相关文章

最新更新