微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法将数据对象传递给视图控制器内部的视图控制器

如何解决无法将数据对象传递给视图控制器内部的视图控制器

| 我不确定为什么,但是当在另一个视图内实例化该视图控制器时,无法将数据对象(NSManagedobject)传递给该视图控制器。当我在传递对象之前记录该对象时,它具有所有数据,但是在到达视图控制器之后,它为空。看起来这应该是相当简单的,但是由于某种原因,它并没有到达目的地。 将NSManagedobject分配给实现文件中的接受视图控制器的代码是:viewController.thisCard = aCard; 任何帮助深表感谢。提前致谢。 这是滚动视图控制器的标题
@interface HandViewController : UIViewController 
<uiscrollviewdelegate>
{
    IBOutlet UIScrollView *scrollView;
    IBOutlet UIPageControl *pageControl;

    BOOL pageControlIsChangingPage;
    NSManagedobjectContext *context;
}

@property (nonatomic,retain) UIView *scrollView;
@property (nonatomic,retain) UIPageControl *pageControl;
@property (nonatomic,retain) NSManagedobjectContext *context;

/* for pageControl */
- (IBAction)changePage:(id)sender;

@end
这是该对象的实现中的viewDidLoad
- (void)viewDidLoad
{
    scrollView.delegate = self;

    [self.scrollView setBackgroundColor:[UIColor blackColor]];
    [scrollView setCanCancelContenttouches:NO];

    scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
    scrollView.clipsToBounds = YES;
    scrollView.scrollEnabled = YES;
    scrollView.pagingEnabled = YES;

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

    NSEntityDescription *entity = [NSEntityDescription entityForName:@\"Game\" inManagedobjectContext:[self context]];
    [fetchRequest setEntity:entity];

    NSError *error;
    NSArray *fetchedGames = [self.context executeFetchRequest:fetchRequest error:&error];

    [fetchRequest release];

    Game *aGame = [fetchedGames objectAtIndex:0];

    CGFloat cx = 0;

    for (Card *aCard in aGame.turnUpcoming.hand) {

        CardViewController *viewController = [[CardViewController alloc] initWithNibName:@\"CardViewController\" bundle:nil];

        CGRect rect = self.view.frame;
        rect.size.height = scrollView.frame.size.height - 50;
        rect.size.width = scrollView.frame.size.width - 50;
        rect.origin.x = ((scrollView.frame.size.width - rect.size.width) / 2) + cx;
        rect.origin.y = ((scrollView.frame.size.height - rect.size.height) / 2);
        viewController.view.frame = rect;
        viewController.thisCard = aCard;
        [scrollView addSubview:viewController.view];

        cx += scrollView.frame.size.width;

        [viewController release];
    }

    self.pageControl.numberOfPages = [aGame.turnUpcoming.hand count];
    [scrollView setContentSize:CGSizeMake(cx,[scrollView bounds].size.height)];

    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}
更新:每个请求,我从视图控制器中添加一些代码 这是CardViewController的标头
@interface CardViewController : UIViewController {
    UILabel IBOutlet *cardValue;
    UILabel IBOutlet *cardInstruction;
    UILabel IBOutlet *cardHint;
    UILabel IBOutlet *cardTimer;
    UIButton IBOutlet *playCardButton;

    Card *thisCard;
}

@property (nonatomic,retain) UILabel IBOutlet *cardValue;
@property (nonatomic,retain) UILabel IBOutlet *cardInstruction;
@property (nonatomic,retain) UILabel IBOutlet *cardHint;
@property (nonatomic,retain) UILabel IBOutlet *cardTimer;
@property (nonatomic,retain) UIButton IBOutlet *playCardButton;
@property (nonatomic,retain) Card *thisCard;

@end
这是CardViewController的实现中的viewDidLoad
- (void)viewDidLoad
{
    [super viewDidLoad];

     if ([thisCard isObjectValid]) {
        cardValue.text = [thisCard.value stringValue];
     }

}
更新:根据以下建议,用于viewDidLoad中卡循环的新代码和新CardView代码 在viewDidLoad中循环:
for (Card *aCard in aGame.turnUpcoming.hand) {

        CGRect rect = scrollView.frame;
        rect.size.height = scrollView.frame.size.height - 50;
        rect.size.width = scrollView.frame.size.width - 50;
        rect.origin.x = ((scrollView.frame.size.width - rect.size.width) / 2) + cx;
        rect.origin.y = ((scrollView.frame.size.height - rect.size.height) / 2);

        tempCardView = [[CardView alloc] initWithFrame:rect];
        tempCardView.thisCard = aCard;

        NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@\"CardView\" owner:self options:nil];

        tempCardView = [nibObjects objectAtIndex:0];

        [scrollView addSubview:tempCardView];

        cx += scrollView.frame.size.width;

        [tempCardView release];
    }
文件CardView.h
@interface CardView : UIView {
    UILabel IBOutlet *cardValue;
    UILabel IBOutlet *cardInstruction;
    UILabel IBOutlet *cardHint;
    UILabel IBOutlet *cardTimer;
    UIButton IBOutlet *playCardButton;

    Card *thisCard;
}

@property (nonatomic,retain) Card *thisCard;

@end
实现文件CardView.m
@implementation CardView

@synthesize cardHint;
@synthesize cardTimer;
@synthesize cardValue;
@synthesize cardInstruction;
@synthesize playCardButton;
@synthesize thisCard;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        if ([thisCard isObjectValid]) {
            cardValue.text = [thisCard.value stringValue];
            cardInstruction.text = thisCard.instruction;
            cardHint.text = thisCard.hint;
            cardTimer.text = [thisCard.time stringValue];
        }
    }
    return self;
}

@end
更新:6/16-添加了XIB屏幕截图 CardView.xib     

解决方法

        似乎您的CardViewController实际上应该是UIView。正如苹果公司所说:\“单个视图控制器通常管理与单个屏幕内容相关的视图,尽管在iPad应用程序中并非总是如此。” (在更改UIView之后)在循环中,您正在创建一个tempCard,然后加载另一个。如果您已经在IB中链接了tempCardView(请参见下文),那么您所需要做的就是:
for (Card *aCard in aGame.turnUpcoming.hand) {
    CGRect rect;
    rect.size.width = scrollView.frame.size.width - 50;
    rect.size.height = scrollView.frame.size.height - 50;
    rect.origin.x = ((scrollView.frame.size.width - rect.size.width) / 2) + cx;
    rect.origin.y = ((scrollView.frame.size.height - rect.size.height) / 2);
    [[NSBundle mainBundle] loadNibNamed:@\"CardView\" owner:self options:nil];  //magically connected to tempCardView.
    self.tempCardView.frame = rect;
    self.tempCardView.thisCard = aCard;

    [scrollView addSubview:tempCardView];
    cx += scrollView.frame.size.width;
    self.tempCardView = nil;
}
因此,您有两个XIB,主要的XIB随您的viewController一起加载,而一个(CardView)为每张卡加载一次。 现在,在CardView中,您尝试过早设置标签。在initWithFrame(在上述情况下,由loadFromNib调用)期间,您尚无访问此Card的权限。您将需要此Card的二传手,只要将值分配给thisCard属性,就会调用该二传手:
   -(void) setThisCard: (Card *) newCard {
       if (thisCard == newCard) return;
       [newCard retain];
       [thisCard release];
       thisCard = newCard;
       self.cardValue.text = [thisCard.value stringValue];
       self.cardInstruction.text = thisCard.instruction;
       self.cardHint.text = thisCard.hint;
       self.cardTimer.text = [thisCard.time stringValue];
    }
现在在Interface Builder中, 使用一个UIView创建CardView.xib,但是将FileOwner设置为您的VC,而不是CardView,因为那是谁来加载它(以及谁拥有tempCardView属性)。 将视图的类型设置为CardView,然后将其挂接到FileOwner(也称为HandViewController)的tempCardView属性。 将所有其他卡片部件放入CardView内,并将其连接到CardView的属性。 你应该准备好了。     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。