iphone - 另一个 UIViewController 中的 UIViewController 的新实例 : Why can't I set an instance variable?

标签 iphone ios uiviewcontroller instance-variables

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

我正在尝试从 TabBarViewController 设置每个 PhotoViewController 实例的标签。我为每个 PhotoViewController 使用 nib 进行初始化,所以我的印象是每个 PhotoViewController 实例都会连接到各自的 IBOutlet - 让我可以简单地使用 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 非常简单。 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

最佳答案

您不能像这样设置任何与 View 相关的值(创建对象并从该 View 设置其他输入)。

因为您无法在任何 View Controller 的 viewDidLoad 之前设置任何级别的值。您需要相应地为标签设置字符串类型的属性,然后在 MyTabBarViewController 中设置它们的值,然后从堆栈中设置它们的值在 PhotoViewController 中选取 MyTabBarViewController 类的对象,然后访问它的属性并设置标签。

要从堆栈中选取 View 对象,您需要使用此行

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

关于iphone - 另一个 UIViewController 中的 UIViewController 的新实例 : Why can't I set an instance variable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6488492/

相关文章:

iphone - 索引对象矩阵 (UILabel)

IOS:多个数组显示在单个 Tableview 上

iphone - 如何设置自定义规则,例如 GameCenter 寻找比赛的难度级别

iphone - youtube 视频的缩略图

iOS 应用内未完成购买

ios - 在 UICollectionView 的底部加载额外的单元格

ios - 使用 Swift 在 didReceiveRemoteNotification 中呈现特定的 View Controller

ios - 在 loadView 方法中计算 View 大小的最佳实践

ios - Objective-C > TableView 和 ViewController 之间的 Segue

iPhone Dev - 创建一个像 Temple Run 一样的一次性 UIAlertView