Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 01-26-2012, 08:34 PM   #1 (permalink)
Registered Member
 
Join Date: Dec 2011
Posts: 21
learningtocode is on a distinguished road
Default Adding Favorites Feature to IOS App

I am new to Objective-C and am trying to develop a simple app for kids. I have looked all over this forum, and across the internet, and I havent quite gotten this answer. What I am trying to do is create an application that has 2 different arrays that link to various UIImages (over 200). I would like to set up a Favorites button, so that when selecting it on that specific image, it saves to a Favorites tab. Whats the best way of going about this. So far I have a tab view linked to a navigation view that is linked up to a Table View that goes to the image. Thanks
learningtocode is offline   Reply With Quote
Old 01-26-2012, 11:44 PM   #2 (permalink)
Just helping out.
 
Domele's Avatar
 
Join Date: Feb 2011
Posts: 2,565
Domele is on a distinguished road
Default

I'd create a FavoriteImage object. Give it one property, a UIImage named image. Also, make it comply to the NSCoding protocol. Here is a tutorial on how to make it comply to NSCoding: How To Save Your App Data With NSCoding and NSFileManager | Ray Wenderlich You can ignore the stuff about NSFileManager.

Then, I'd create a singleton (Duncan C shows how to make a singleton here: http://www.iphonedevsdk.com/forum/ip...singleton.html) and give it 3 methods and a NSMutableArray property named internal_favoriteImages:
Code:
- (void)addFavoriteImage:(FavoriteImage *)favoriteImage;
- (void)removeFavoriteImage:(FavoriteImage *)favoriteImage;
- (NSArray *)favoriteImages;
In addFavoriteImage, add the passed in image to your internal_favoriteImages array. Then, wrap your array into a NSData object using NSKeyedArchiver's archivedDataWithRootObject: class method. For this to work, your FavoriteImage object needs to comply to NSCoding. Then save the NSData object to NSUserDefaults.

In removeFavoriteImage, do the same thing as you did in addFavoriteImage except remove the passed in image from the array.

In favoriteImages, create a non mutable version of the array using NSArray's arrayWithArray: method and return that.

In your Singleton init method, check if a NSData object has been saved into NSUserDefaults and if so, retrieve the NSData object. Unpack it using NSKeyedUnarchiver's unarchiveObjectWithData: class method. If there is no NSData object present, just initialize your internal_FavoriteImages array.

The only problem with this approach is you are going to be saving the image data again which causes loss of disk space. I'm too lazy at the moment to figure out a solution to that so I'll let someone else tackle that problem.
__________________
If you are looking for a quality developer, I'm your man. Give me a PM if you are interested.

New app - See screenshots and details at www.globaclock.com.

If you want to thank me, click the link. Every click counts. If you want to do more, buy my app. A link is available on my website. Thanks.
Domele is offline   Reply With Quote
Old 01-27-2012, 02:48 AM   #3 (permalink)
Registered Member
 
Join Date: Sep 2011
Posts: 33
X-RaY is on a distinguished road
Default

I guess all the images are stored somewhere, right?
make sure the name of the images has some id in it.
Then, when someone favorites an image, store the id in an array, set a variable to let the favoritestab know it has to refresh.
In the refresh, just iterate through the array and display the corresponding image.
then, when the favorite is un-favorited, remove the id from the array and refresh the tab again

when the app is pushed to the background, store the array in the nsuserdefaults.
easiest way imo, and you won't have to save a duplicate of the favorite images.
__________________
I don't suffer from insanity; I enjoy every minute of it.
X-RaY is offline   Reply With Quote
Old 01-27-2012, 03:05 AM   #4 (permalink)
Just helping out.
 
Domele's Avatar
 
Join Date: Feb 2011
Posts: 2,565
Domele is on a distinguished road
Default

I agree with the ID part but if you have to set a variable for the favorite images controller to refresh, that's going to make it dependent on another class. That's not a good thing. By the way, if you use the ID, you won't need to make a separate FavoriteImage class. But I still advise that you make the singleton.
__________________
If you are looking for a quality developer, I'm your man. Give me a PM if you are interested.

New app - See screenshots and details at www.globaclock.com.

If you want to thank me, click the link. Every click counts. If you want to do more, buy my app. A link is available on my website. Thanks.
Domele is offline   Reply With Quote
Old 01-28-2012, 01:06 PM   #5 (permalink)
Registered Member
 
Join Date: Dec 2011
Posts: 21
learningtocode is on a distinguished road
Default

Thank you guys for both your inputs. I will give it a shot!!!
learningtocode is offline   Reply With Quote
Reply

Bookmarks

Tags
favorites button feature

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 403
9 members and 394 guests
Atatator, condor304, FrankWeller, MAMN84, mraalex, n00b, PowerGoofy, tim0504, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,674
Threads: 94,123
Posts: 402,908
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Atatator
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:57 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0