Ok, we've got our view display up and running. Let's keep going with that - we want to add a title, and a url to the artist's site.
We also want to mix in a bunch of the images. First. let's add and connect the variables.
Ok, for now I just hooked up the title. But now we want to just copy all the images into the resources folder.
Ok, they're copied. Now, the next step is to make a collections of the strings, an NSArray probably, that contains the name of those. The first thing to do is just to figure out how to create an NS array and then add the entries one by one.
Ok, from this url,
http://www.cocoadev.com/index.pl?NSMutableArray
We see this example:
NSMutableArray *array; //simply defines a mutable Array
array = [[NSMutableArray alloc] init]; //saves memory for the Array and initializes it.
arrayCount = [array count]; //counts all objects of this array and puts it into the arrayCount variable
[array addObject:[NSNumber numberWithInt:15]]; // appends "15" to the (mutable) array
arrayVariable = [array objectAtIndex:378]; //returns the 379th Object of the array and puts it into arrayVariable
So, now all we need to do is create the NSStrings to add. I think if we do it with a stringWithFormat, we don't need to worry about releasing. Here's an example.
int age = 25;
NSString *message;
message = [NSString stringWithFormat: @"Your age is %d", age];
Actually, we can add the string with creating a separate variable
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:[NSString stringWithFormat: @"all_was_darkness"]];
[array addObject:[NSString stringWithFormat: @"fragile_as_snow"]];
And we can toss in the randomization while we're at it:
int index = arc4random() % [array count];
And pull it from the array and set the image to it. So, now we have a method like this
- (void)viewDidLoad
{
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:[NSString stringWithFormat: @"all_was_darkness"]];
[array addObject:[NSString stringWithFormat: @"fragile_as_snow"]];
int index = arc4random() % [array count];
NSString *pictureName = [array objectAtIndex:index];
// NSString* imagePath = [ [ NSBundle mainBundle] pathForResource:@"fragile_as_snow" ofType:@"png"];
NSString* imagePath = [ [ NSBundle mainBundle] pathForResource:pictureName ofType:@"png"];
UIImage *img = [ UIImage imageWithContentsOfFile: imagePath];
if (img != nil) { // Image was loaded successfully.
[imageView setImage:img];
[imageView setUserInteractionEnabled:NO];
[img release]; // Release the image now that we have a UIImageView that contains it.
}
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
Ok, it seems to be showing the same picture. Let's add a couple of more for good measure. And also set the title in the label, which is something we forgot to do.
So, now we have this:
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:[NSString stringWithFormat: @"all_was_darkness"]];
[array addObject:[NSString stringWithFormat: @"fragile_as_snow"]];
[array addObject:[NSString stringWithFormat: @"hanami_looking_at_cherries"]];
[array addObject:[NSString stringWithFormat: @"idle_hours"]];
int index = arc4random() % [array count];
NSString *pictureName = [array objectAtIndex:index];
art_title.text = pictureName;
Ok, but the text in the label is too small, and has the "...". Let's just increase the size of the label and see what happens.
Ok, that works, but I can't use the file name as the title - it has underlines in it. So, the simplest thing is to just create a parallel array. I actually should create a name value pair, and store that in the array. But I'm not going to right now - I'll make it a todo.
For now, let's use the logic from the android app, which has the correct titles:
R.drawable.all_was_darkness,
R.drawable.fragile_as_snow,
R.drawable.hanami_looking_at_cherries,
//R.drawable.idle_hours,
R.drawable.looking_at_the_moon,
R.drawable.sei_shonagan_and_the_hell_screen,
R.drawable.snow_cranes,
R.drawable.snow_viewing,
R.drawable.tasacode_whose_sleeve,
R.drawable.the_abbot,
R.drawable.the_go_game,
R.drawable.the_third_princess,
R.drawable.ukifune_and_karou_at_uji,
R.drawable.ukiune_wandering,
R.drawable.wisteria,
R.drawable.yugao };
Now, let's create an array with these titles:
imageTitleVector.add("All was Darkness");
imageTitleVector.add("Fragile as Snow");
imageTitleVector.add("Hanomi Looking at Cherries");
imageTitleVector.add("Looking at the Moon");
imageTitleVector.add("Sei Shonagan and the Hell Screen");
imageTitleVector.add("Snow Cranes");
imageTitleVector.add("Snow Viewing");
imageTitleVector.add("Tasacode Whose Sleeve");
imageTitleVector.add("The Abbot");
imageTitleVector.add("The Go Game");
imageTitleVector.add("The Third Princess");
imageTitleVector.add("Ukifune and Karou at Uji");
imageTitleVector.add("Ukufune Wandering");
imageTitleVector.add("Wisteria");
imageTitleVector.add("Yugao");
In fact, let's use the same ones for now and add more later, after the app is release.
- (void)viewDidLoad
{
NSMutableArray *fileNameArray = [[NSMutableArray alloc] init];
[fileNameArray addObject:[NSString stringWithFormat: @"all_was_darkness"]];
[fileNameArray addObject:[NSString stringWithFormat: @"fragile_as_snow"]];
[fileNameArray addObject:[NSString stringWithFormat: @"hanami_looking_at_cherries"]];
[fileNameArray addObject:[NSString stringWithFormat: @"looking_at_the_moon"]];
[fileNameArray addObject:[NSString stringWithFormat: @"sei_shonagan_and_the_hell_screen"]];
[fileNameArray addObject:[NSString stringWithFormat: @"snow_cranes"]];
[fileNameArray addObject:[NSString stringWithFormat: @"snow_viewing"]];
[fileNameArray addObject:[NSString stringWithFormat: @"tasacode_whose_sleeve"]];
[fileNameArray addObject:[NSString stringWithFormat: @"the_abbot"]];
[fileNameArray addObject:[NSString stringWithFormat: @"the_go_game"]];
[fileNameArray addObject:[NSString stringWithFormat: @"the_third_princess"]];
[fileNameArray addObject:[NSString stringWithFormat: @"ukifune_and_karou_at_uji"]];
[fileNameArray addObject:[NSString stringWithFormat: @"ukiune_wandering"]];
[fileNameArray addObject:[NSString stringWithFormat: @"wisteria"]];
[fileNameArray addObject:[NSString stringWithFormat: @"yugao"]];
NSMutableArray *titleArray = [[NSMutableArray alloc] init];
[titleArray addObject:[NSString stringWithFormat: @"All was Darkness"]];
[titleArray addObject:[NSString stringWithFormat: @"Fragile as Snow"]];
[titleArray addObject:[NSString stringWithFormat: @"Hanami - Looking at Cherries"]];
[titleArray addObject:[NSString stringWithFormat: @"Looking at the Moon"]];
[titleArray addObject:[NSString stringWithFormat: @"Sei Shonagan and the Hell Screen"]];
[titleArray addObject:[NSString stringWithFormat: @"Snow Cranes"]];
[titleArray addObject:[NSString stringWithFormat: @"Snow Viewing"]];
[titleArray addObject:[NSString stringWithFormat: @"Tasacode Whose Sleeve"]];
[titleArray addObject:[NSString stringWithFormat: @"The Abbot"]];
[titleArray addObject:[NSString stringWithFormat: @"The Go Game"]];
[titleArray addObject:[NSString stringWithFormat: @"The Third Princess"]];
[titleArray addObject:[NSString stringWithFormat: @"Ukifune and Karou at Uji"]];
[titleArray addObject:[NSString stringWithFormat: @"Ukufune Wandering"]];
[titleArray addObject:[NSString stringWithFormat: @"Wisteria"]];
[titleArray addObject:[NSString stringWithFormat: @"Yugao"]];
int index = arc4random() % [fileNameArray count];
NSString *pictureName = [titleArray objectAtIndex:index];
art_title.text = pictureName;
// NSString* imagePath = [ [ NSBundle mainBundle] pathForResource:@"fragile_as_snow" ofType:@"png"];
NSString* imagePath = [ [ NSBundle mainBundle] pathForResource:pictureName ofType:@"png"];
UIImage *img = [ UIImage imageWithContentsOfFile: imagePath];
if (img != nil) { // Image was loaded successfully.
[imageView setImage:img];
[imageView setUserInteractionEnabled:NO];
[img release]; // Release the image now that we have a UIImageView that contains it.
}
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
Great. Let's try this out.
Ok. Well, it works, but there's a little problem with navigation. I want to hide the back button and add a button to continue to the next level.
Ok, added the button. Here's what the old "Answers" used for doing a similar type thing:
-(IBAction)popViewController:(id)sender;
// and
-(IBAction)popViewController:(id)sender {
// return to previous screen
[self.navigationController popViewControllerAnimated:YES];
}
Ok, let's hook it up to the button we just added to the nib.
Wow - this thing just did about 14 different things wrong:
1) Didn't show the image
2) The pop didn't work.
3) I had changed the JLPT level, and it started mixing in (I think) entries from the previous level 4 run
4) It crashed when I did something trivial
Let's track down #1 first.
Actually, it crashes when I hit "back" on the settings view - another pop the failing.
Darn it. I've list the images. What went wrong?
Ok, I've should decide if, umm, I should just go to the backup. These displays and views and the builder are so touchy.
Ok, I took the conservative route and went with the backup. I have my images back.
Let's conservatively just add four titles:
NSMutableArray *fileNameArray = [[NSMutableArray alloc] init];
[fileNameArray addObject:[NSString stringWithFormat: @"all_was_darkness"]];
[fileNameArray addObject:[NSString stringWithFormat: @"fragile_as_snow"]];
[fileNameArray addObject:[NSString stringWithFormat: @"hanami_looking_at_cherries"]];
[fileNameArray addObject:[NSString stringWithFormat: @"idle_hours"]];
NSMutableArray *titleArray = [[NSMutableArray alloc] init];
[titleArray addObject:[NSString stringWithFormat: @"All was Darkness"]];
[titleArray addObject:[NSString stringWithFormat: @"Fragile as Snow"]];
[titleArray addObject:[NSString stringWithFormat: @"Hanami - Looking at Cherries"]];
[titleArray addObject:[NSString stringWithFormat: @"Looking at the Moon"]];
int index = arc4random() % [fileNameArray count];
NSString *pictureName = [titleArray objectAtIndex:index];
art_title.text = pictureName;
No! That kills the image display.
Wow - when I added a release for the two arrays, it worked! You've got to be careful about this releasing stuff in this language, otherwise it will mess you up - big time.
Ok, here's my code for now:
- (void)viewDidLoad
{
NSMutableArray *fileNameArray = [[NSMutableArray alloc] init];
[fileNameArray addObject:[NSString stringWithFormat: @"all_was_darkness"]];
[fileNameArray addObject:[NSString stringWithFormat: @"fragile_as_snow"]];
[fileNameArray addObject:[NSString stringWithFormat: @"hanami_looking_at_cherries"]];
[fileNameArray addObject:[NSString stringWithFormat: @"looking_at_the_moon"]];
[fileNameArray addObject:[NSString stringWithFormat: @"sei_shonagan_and_the_hell_screen"]];
[fileNameArray addObject:[NSString stringWithFormat: @"snow_cranes"]];
[fileNameArray addObject:[NSString stringWithFormat: @"snow_viewing"]];
[fileNameArray addObject:[NSString stringWithFormat: @"tasacode_whose_sleeve"]];
[fileNameArray addObject:[NSString stringWithFormat: @"the_abbot"]];
[fileNameArray addObject:[NSString stringWithFormat: @"the_go_game"]];
[fileNameArray addObject:[NSString stringWithFormat: @"the_third_princess"]];
[fileNameArray addObject:[NSString stringWithFormat: @"ukifune_and_karou_at_uji"]];
[fileNameArray addObject:[NSString stringWithFormat: @"ukiune_wandering"]];
[fileNameArray addObject:[NSString stringWithFormat: @"wisteria"]];
[fileNameArray addObject:[NSString stringWithFormat: @"yugao"]];
NSMutableArray *titleArray = [[NSMutableArray alloc] init];
[titleArray addObject:[NSString stringWithFormat: @"All was Darkness"]];
[titleArray addObject:[NSString stringWithFormat: @"Fragile as Snow"]];
[titleArray addObject:[NSString stringWithFormat: @"Hanami - Looking at Cherries"]];
[titleArray addObject:[NSString stringWithFormat: @"Looking at the Moon"]];
[titleArray addObject:[NSString stringWithFormat: @"Sei Shonagan and the Hell Screen"]];
[titleArray addObject:[NSString stringWithFormat: @"Snow Cranes"]];
[titleArray addObject:[NSString stringWithFormat: @"Snow Viewing"]];
[titleArray addObject:[NSString stringWithFormat: @"Tasacode Whose Sleeve"]];
[titleArray addObject:[NSString stringWithFormat: @"The Abbot"]];
[titleArray addObject:[NSString stringWithFormat: @"The Go Game"]];
[titleArray addObject:[NSString stringWithFormat: @"The Third Princess"]];
[titleArray addObject:[NSString stringWithFormat: @"Ukifune and Karou at Uji"]];
[titleArray addObject:[NSString stringWithFormat: @"Ukufune Wandering"]];
[titleArray addObject:[NSString stringWithFormat: @"Wisteria"]];
[titleArray addObject:[NSString stringWithFormat: @"Yugao"]];
int index = arc4random() % [fileNameArray count];
NSString *pictureName = [titleArray objectAtIndex:index];
// NSString *pictureName = [fileNameArray objectAtIndex:index];
art_title.text = pictureName;
// NSString* imagePath = [ [ NSBundle mainBundle] pathForResource:@"fragile_as_snow" ofType:@"png"];
NSString* imagePath = [ [ NSBundle mainBundle] pathForResource:pictureName ofType:@"png"];
UIImage *img = [ UIImage imageWithContentsOfFile: imagePath];
if (img != nil) { // Image was loaded successfully.
[imageView setImage:img];
[imageView setUserInteractionEnabled:NO];
[img release]; // Release the image now that we have a UIImageView that contains it.
}
[super viewDidLoad];
[fileNameArray release];
[titleArray release];
// Do any additional setup after loading the view from its nib.
}
No comments:
Post a Comment