开发者

IPhone Navigation-Based Application Model

开发者 https://www.devze.com 2023-01-24 08:09 出处:网络
I am making a Navigation-Based Application for the IPhone and I start off with a big list of names (开发者_如何学Cabout 100) and I was wondering what would be the quickest and most efficient way to al

I am making a Navigation-Based Application for the IPhone and I start off with a big list of names (开发者_如何学Cabout 100) and I was wondering what would be the quickest and most efficient way to alter the content according to what name the user tapped on in the list.

Right now I change the content according to what the title is like this:

NSString *comparisonString = @"Bill Clinton";
NSString *comparisonString2 = @"Bob Dole";
 if (self.title == comparisonString) {
  NSString *alterString = [NSString stringWithFormat: @"He is a democrat from North Carolina "];
  [appDelegate setModelData:alterString];
 }
 else if (self.title == comparisonString2) {
  NSString *alterString = [NSString stringWithFormat: @"He was born into a family in New York City... "];        
  [appDelegate setModelData:alterString];
 }

would this be the best way to do this? Seems like a lot of code would be repeated. I am brand new to Objective-C and developing for the IPhone, any advice will help me. Thank you!


What you've written so far in those two conditions is equivalent to this (with no if statement):

NSString *alterString = [NSString stringWithFormat: @"Content for %@!", self.title];
[appDelegate setModelData:alterString];

Hope that helps.

All you are doing is changing that same part of the string in both the if and else statements (and presumably more else statements later)

EDIT

ok, looking now at the edited question, you basically have a look up table now. So self.title is the key and you want to use that to lookup another string.

One way to do this is to create a plist with a dictionary that maps each name to a string. Then load that plist and look up the name from there. Something like this:

NSDictionary* people = [NSDictionary dictionaryWithContentsOfFile:pathToThePlist];
NSString *content = [people objectForKey:name];

(BTW, right now you are using '==' for string comparison, you want to use -[NSString isEqualToString:] for that in the future.

0

精彩评论

暂无评论...
验证码 换一张
取 消