2011-06-14 36 views
0

我不知道爲什麼,但我有一個傳遞數據對象(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. 
} 

UPDATE的實現:每請求,我加入一個比特的來自視圖控制器的代碼

這是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) 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 

實現文件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

CardView.xib

回答

1

好像你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; 
} 

所以,你有兩個XIBs,你的主要一個與您的viewController加載,和一個(CardView),其被加載每張卡一次。

現在,裏面CardView,你想太早建立的標籤。 initWithFrame(在上述情況下被稱爲loadFromNib)期間,您沒有訪問尚未thisCard。你所需要的thisCard一個setter,它會被稱爲值分配給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,

  1. 用單一的UIView與創建CardView.xib ,但將FileOwner設置爲您的VC,而不是CardView,因爲這是誰將加載它(以及誰擁有tempCardView屬性)。
  2. 將視圖的類型設置爲CardView,然後將其掛接到FileOwner(又名HandViewController)的tempCardView屬性
  3. 將所有其他卡片部件放入該CardView中,並將其掛接到該CardView的屬性。

你應該全部設置。

+0

謝謝你的迴應。我將嘗試將其更改爲UIView並查看會發生什麼。當我完成時,我會公佈結果。 – unclesol 2011-06-14 23:22:19

+0

所以,如果我使用UIView管理子視圖,而不是UIViewController - 有沒有辦法做到這一點,我可以使用一個nib文件來控制UIView中的幾個對象?如果我不必編寫子視圖中所有對象的佈局,它會使生活變得更簡單。再次感謝。 – unclesol 2011-06-15 00:36:55

+0

當然,事實上,您可以在VC的nib文件中的視圖中查看視圖。但我假設你想在你的例子中使用可變數量的數字。在這種情況下,您可以使用loadNibNamed加載一個:owner:options:將所有者設置爲您的VC,但在VC中定義了一個tempCardView屬性,該屬性鏈接到您在IB中創建的視圖。現在在viewDidLoad中,調用loadNibNamed和tempCardView將被鏈接到一個新的CardView。把它交給你的視圖,發佈你的副本(如超級視圖保留它),然後再做一遍。 – mackworth 2011-06-15 02:54:09

相關問題