我不知道爲什麼,但我有一個傳遞數據對象(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
謝謝你的迴應。我將嘗試將其更改爲UIView並查看會發生什麼。當我完成時,我會公佈結果。 – unclesol 2011-06-14 23:22:19
所以,如果我使用UIView管理子視圖,而不是UIViewController - 有沒有辦法做到這一點,我可以使用一個nib文件來控制UIView中的幾個對象?如果我不必編寫子視圖中所有對象的佈局,它會使生活變得更簡單。再次感謝。 – unclesol 2011-06-15 00:36:55
當然,事實上,您可以在VC的nib文件中的視圖中查看視圖。但我假設你想在你的例子中使用可變數量的數字。在這種情況下,您可以使用loadNibNamed加載一個:owner:options:將所有者設置爲您的VC,但在VC中定義了一個tempCardView屬性,該屬性鏈接到您在IB中創建的視圖。現在在viewDidLoad中,調用loadNibNamed和tempCardView將被鏈接到一個新的CardView。把它交給你的視圖,發佈你的副本(如超級視圖保留它),然後再做一遍。 – mackworth 2011-06-15 02:54:09