任何人都可以通過我的項目的過程中走過我。我有太多的教程,我不確定要走哪條路線。所以我有兩個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
或不是一個字符串?
這是順便說一下
- (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];
}
我不是:
在這種情況下,你必須覆蓋的方法awakeFromNib這樣當然我明白你的問題。你的水果課對我來說很好,你想知道什麼? – trojanfoe
你在你的ViewController實現中如何使用你的Fruit類? –
你的問題不清楚。 **請澄清你實際要求的**。 – Greg