任何人都可以引导我完成我的项目过程。我有太多的教程,我真的不知道该走哪条路了。所以我有两个UITextField
s 可以接受一个字符串。这些如下:
UITextField *fruitNameField;
UITextField *fruitDescriptionField;
在我ViewController
的里面,代码是这样的
NSString *fruitName = [[NSString alloc] initWithString:[fruitNameField text]];
NSString *fruitDescription = [[NSString alloc] initWithString:[fruitDescriptionField text]];
NSLog(@"Fruit: %@, Description: %@", fruitName, fruitDescription);
正如您在上面看到的,它没有使用我的Fruit
课程。
所以这是我的Fruit
实现
- (id)init
{
return [self initWithName:@"N/A"
description:@"N/A"
priority:2];
}
- (id)initWithName:(NSString *)name description:(NSString *)desc priority:(int)prio
{
self = [super init];
if (self) {
[self setFruitName:name];
[self setFruitDescription:desc];
[self setFruitPriority:prio];
}
return self;
}
#pragma mark -
#pragma mark Description
- (NSString *)description
{
return [[NSString alloc] initWithFormat:@"[%d] %@: %@", fruitPriority, fruitName, fruitDescription];
}
问题是,无论何时输入我的字符串,我都需要使用Fruit
我创建的类ViewController
吗?
顺便说一句,这是我ViewController
的
- (IBAction)writeToPlist:(id)sender
{
NSLog(@"writeToPlist");
NSString *fruitName = [[NSString alloc] initWithString:[fruitNameField text]];
NSString *fruitDescription = [[NSString alloc] initWithString:[fruitDescriptionField text]];
NSDictionary *fruitDetail = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:fruitName, fruitDescription, nil]
forKeys:[NSArray arrayWithObjects:@"Fruit", @"Description", nil]];
NSMutableArray *fruitPlist = [[NSMutableArray alloc] initWithContentsOfFile:[self filePath]];
if (fruitPlist == nil) {
fruitPlist = [[NSMutableArray alloc] init];
}
NSMutableDictionary *fruitDictionary = [[NSMutableDictionary alloc] init];
[fruitDictionary setObject:fruitDetail forKey:fruitName];
[fruitPlist addObject:fruitDictionary];
[fruitPlist writeToFile:[self filePath] atomically:YES];
[[self presentingViewController] dismissViewControllerAnimated:YES
completion:nil];
}