我正在尝试将 plist 数据加载到具有两个视图的实用程序模板应用程序中的 UILabels 中。有一个翻转动画允许在视图之间切换。
这是plist的结构:
Root...............................(Array)
........Item 0.....................(Dictionary)
.................Question..........(string) ///Hello////
.................Answer............(string) ///Goodbye//
我已经设法使用键(Constants.h)将问题字符串加载到第一个视图(FlashCard1ViewController)中,但我正在努力让答案字符串加载到第二个视图(FlippedView)中。有人能帮忙吗?
以下是我正在使用的代码,如果有人有其他问题或需要澄清,请告诉我,我会尽力回答。
感谢
詹姆士
FlashCard1ViewController.h
#import <UIKit/UIKit.h>
@interface FlashCard1ViewController : UIViewController {
NSMutableArray *flashCards;
IBOutlet UILabel *Label1;
}
@property (nonatomic, retain) NSMutableArray *flashCards;
@property (nonatomic, retain) UILabel *Label1;
- (IBAction)showInfo;
@end
FlashCard1ViewController.m
#import "FlashCard1ViewController.h"
#import "Constants.h"
#import "FlippedView.h"
@implementation FlashCard1ViewController
@synthesize Label1, flashCards;
/////////////////////Code to load the Array into the UIlable////////////////////////////////////////////////
- (void)viewDidLoad {
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:@"DataDetail" ofType:@"plist"]; //Defines path for DATA For ARRAY//
NSMutableArray* tmpArray = [[NSMutableArray alloc]initWithContentsOfFile:path]; //initialises the contents of the ARRAY with the PLIST"
self.flashCards = tmpArray;
NSDictionary *Question1 = [flashCards objectAtIndex:0];
Label1.text = [Question1 objectForKey:Question_KEY];
self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
[tmpArray release];
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
// Custom initialization
}
return self;
}
- (void)FlippedViewDidFinish:(FlippedView *)controller {
[self dismissModalViewControllerAnimated:YES];
}
- (IBAction)showInfo {
FlippedView *controller = [[FlippedView alloc] initWithNibName:@"FlippedView" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
}
- (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.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[Label1 release];
[flashCards release];
[super dealloc];
}
@end
翻转视图.h
#import <UIKit/UIKit.h>
@interface FlippedView : UIViewController {
NSMutableArray *flashCards; //provides Directory for detail View//
IBOutlet UILabel *Label2;
}
@property (nonatomic, retain) NSMutableArray *flashCards;
@property (nonatomic, retain) UILabel *Label2;
- (IBAction)done;
@end
翻转视图
#import "FlippedView.h"
#import "FlashCard1ViewController.h"
#import "Constants.h"
@implementation FlippedView
@synthesize Label2, flashCards;
- (void)viewDidLoad {
[super viewDidLoad];
NSDictionary *Answer1 = [flashCards objectAtIndex:0];
Label2.text = [Answer1 objectForKey:Answer_KEY];
self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
}
- (IBAction)done {
FlashCard1ViewController *controller = [[FlashCard1ViewController alloc] initWithNibName:@"FlashCard1ViewController" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
}
- (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.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[flashCards release];
[Label2 release];
[super dealloc];
}
@end
常量.h
#define Question_KEY @"Question"
#define Answer_KEY @"Answer"