开发者

add to favorites button help

开发者 https://www.devze.com 2023-01-11 11:58 出处:网络
how would an add to favorites button work in order to take the controller selected and add it to a tab bar item,

how would an add to favorites button work in order to take the controller selected and add it to a tab bar item, i tried using an ibaction to add an entry to an array however i wasnt sure on how to handle this problem.

i would like to add a uibutton that adds an entry to this k开发者_C百科ind of array format

rootArray = [[NSArray alloc] initWithObjects:@"entry1", @"entry2",  nil];

the tab bar would have two views on the favorite table view and one with all the rest of the cells. i would like to click on the "normal" table view which would take me to the detailview controller already implemented. Afterwards i would like to have a button on the detailviewcontroller that would say something like add to favorites and add this detailview and its cell to the favorites tab controller. i would like to add the uibutton to add the detailview controller in the mentioned format above however im not sure how to do this.

i would appreciate some help in pointing me in the right direction thanks


Am assuming u have a database somewhere from where you are getting data for your "normal" tableview. What you need to do is you could add a favorites table or just add a "is_favorite" field to your table.I'd go with the latter option.

With this in place you just have to filter your results. Records with "is_favorite" as true added to the favorites table view while everything (both favorite and non-favorite) appear in the other table view.

For the details view, you could have a UIBarButton for adding to favorites. The iphone sdk already has a system button for that. Add the following to you viewDidLoad:

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(addToFavorites:)];

then add a method called addToFavorites to your detailviewcontroller class. This will respond to the IBAction. Inside addToFavorites add some code that will set the is_favorite field to true.

I believe all this covers how you could accomplish your task.

Edit: The explanation above was based on the assumption you were using a database.

Update Since you are storing everything in an array here's what you could do. Define the favorites (mutable)array in you YourAppDelegate.h file as you would any normal property.

NSMutableArray *favorites;

Then inside the addToFavorites do something like:

YourAppDelegate *app = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];
[app.favorites addObject:current_detail]; //add whatever detail you selected

favorites is the array you will be using as the source of data for your favorites table view.

Hope all this makes sense now.

0

精彩评论

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

关注公众号