add to favorites button help - iphone

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 kind 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.

Related

Adding a value to a bundle in ios

I was wondering how I can add an int to a bundle and pass it to the next view that I am calling.
What I am trying to do is click an item on a UITableView and that will call the next .m file and pass the int that pertains to a document that I will be looking up online. This document has a specific id and without passing it to the next view I will not be able to get the list that I need to compile and show on the next UITableView.
How are you displaying the next view? Can you not just add an instance variable to the view to be called?
View2 *view = [[View2 alloc] init]];
[view setSpecificId:myId];
display the view....

Passing data from UIViewController to 1 back UIViewController

So I have this UINavigationController, I'm on the first moving to the next view, than I want to hit the 'back' button and to go back to the first view with the data that I saved into 'strAddress' on the second view. I want to present the data on the first view on 'lblShowStr.text'.
how can I manage to do that? I've searched all the web, found some people that wrote, but couldn't understand what they have been told there.
Thanks!
You can get a reference to the previous viewController in your navigation stack by saying:
NSArray *viewControllers = self.navigationController.viewControllers;
MyViewControllerClass *previousController = [viewControllers objectAtIndex:[viewControllers count] - 2];
You can then set a property on the 'previous' view controller to store your text, or even set the label outlet's text directly like this:
previousController.lblShowStr.text = self.strAddress;
It's not the best way to do it (the best way involves creating a custom delegate protocol or using NSNotificationCenter) but it's the easiest way.
In your first view controller you might have an NSString property called strAddress.
and you put that string into lblShowStr.text every time the view appears.
In your second view controller you might have a property pointing to an instance of view controller one. When you instantiate your second view controller you could assign the property on it to the first view controller.
secondViewController.firstViewController = self;
or
[secondViewController setFirstViewController:self];
Then when the user presses the back button viewDidAppear would get called for the first view and update the string.
I am assuming you don't want to store this data anywhere else e.g. in your model or nsuserdefaults etc.

IPad Clear all fields back to start

Im creating an internal ipad app for which will allow our employees to fill in details on a site visit they have just performed.
One of the pages in the app is a massive form where the user enters all the information. I have a reset button on this form. This button will just clear all textfields, textviews, uncheck checkboxes etc etc.
Is there a clean way to reset a view to the state as if it is brand new (not-dirty). I dont really want to go through every control on the view and set it back to nothing.
Is there a way to wipe the entire view and restart again?
Thanks in advance
I think the easy-way to do this is by,
-(IBAction) reset
{
YourView *obj=[[YourView alloc] initWithNibName:#"YourView" bundle:[NSBundle mainBundle]];
[self presentModalViewController:obj animated:NO];
[obj release];
}
I think the easiest way to do this is to discard your view (or the sections of it you want to reset) and recreate them. That might be as simple as:
//assuming you have a nib file containing some custom FormView class with your current view controller as its owner and the FormView instance in the nib bound to a 'formView' property on the controller
[self.formView removeFromSuperView];
[[NSBundle mainBundle] loadNibNamed:#"FormView" owner:self options:nil];
[self.view addSubView:self.formView];
//keep a reference to the old formView first and animate the transition as you like
More complex but possibly worthwhile could be to have your view objects use KVO to watch for changes to some model object exposed as a property through a delegate or on a superview. That's handy if you want the view to be able to automatically update itself in response to changes to the model coming from other parts of the view or some external source like network updates. A "reset" could then be as simple as replacing that value of the property the views are observing with a new instance of your model.

Navigation based application with an add button

I created a new Navigation Based Application project.Then in MainWindow.xib I added a button to the navigationbar. I would like to push a new View onto the screen where I can enter information, which will be added as an object to the array of the UITableView.
But I don't know where to write the IBAction to link the button to (Appdelegate or the RootViewController)? Because as you see in the screenshot, it resides in MainWindow.xib because the RootViewController is merely a Table and doesn't contain the navigation. But in the document view of MainWindow.xib it is located under the RootViewController.
Do I have to create a new View Controller inside the XIB as well and create an IBOutlet for it?
I tried putting the code inside my AppDelegate and reference the button to the delegate but it doesn't work.
Please help. Thanks in advance.
See the screenshot here: http://i56.tinypic.com/5djbcm.png
When you ask yourself a question "where does this action belong" it's most probably a controller because controllers handle event flows in your app. Next question - "What controller is in charge when this action happens? What controller is most interested in this action?". Answer in your case is root table controller (RootViewController instance). Create an IBAction method in it which will push form controller (one you use to enter information) to navigation controller.
// somewhere in RootViewController.m
- (IBAction)addNewEntry {
NewEntryFormController *c = [[[NewEntryFormController alloc] init] autorelease];
// ...
[self.navigationController pushViewController:c animated:YES];
}

Can't understand iPhone view layout

I have an iphone app that I built based off a tutorial (for a different framework so I had to modify things a bit) that uses a TabBar and a NavigationBar on the same View that also contains a UITable populated from an SQLite db.
I built it last night and when you select an item in the UITable it was redirecting to a view that displayed the detail of my item (at that point, just a city name). I went in and tried to modify that DetailView...and nothing I change works.
So I have a few questions/problems:
1) Why is it that my CityDetailView.xib looks like this in Interface Builder:
alt text http://www.jamespwright.com/images/public/detailviewinterfacebuilder.jpg
But looks like this when the app is run:
alt text http://www.jamespwright.com/images/public/wrongdetailview.jpg
I am new to iPhone development, so I'm not even sure where to begin looking for this problem.
I know that the TableViewController is set to CitiesTableViewController and within that controller code in my didSelectRowAtIndexPath I run this code:
MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
City *city = (City *)[appDelegate.cities objectAtIndex:indexPath.row];
// Initialize a new CityDetailView if there isn't one already
if (self.cityView == nil) {
CityDetailViewController *viewController = [[CityDetailViewController alloc] initWithNibName:#"CityDetailViewController" bundle:[NSBundle mainBundle]];
self.cityView = viewController;
[viewController release];
}
[appDelegate.citiesNavController pushViewController:cityView animated:YES];
self.cityView.city = city;
self.cityView.title = city.name;
[self.cityView.cityName setText:city.name];
[self.cityView.stateName setText:city.state];
[self.cityView.population setText:city.population];
And I have the labels linked up in Interface Builder to the properties in the Controller.
I also have the CityDetailView.xib class identity set to CityDetailViewController which has all the properties correctly declared.
One thing I can't figure out, if I rename CityDetailView.xib to "CityDetailView.xib1" the program still runs the same, but if I change [self.cityView.title = city.name]; to [self.cityView.title = #"Bob's Your Uncle!"]; it displays that change within the program.
On a completely unrelated note, on my TableView (my 2nd tab) I have this odd bar at the bottom that I don't know what it is or how to get rid of it, I'm pretty sure it has to do with the NavigationController but I don't know why or how it's there. It is this one:
alt text http://www.jamespwright.com/images/public/tabandnavigationview.jpg
Can anyone offer me any advice on these problems?
When "nothing happens" when you change source files, there are usually only a few things you're doing:
Dates are screwed up somewhere and you need to clean (Build → Clean)
You're editing the wrong file
You're building the wrong target
No, seriously, you're editing the wrong file, or building the wrong target
Cleaning is definitely the first thing to try. Especially if you can rename the XIB and everything still cheerfully goes on.
The simplest explanation is that self.cityView.stateName and the subsequent labels are not hooked up in interface builder. The controller must have a hooked up outlet to each label in order to populate it. Objective-C won't raise an error if you send a message to an object it can't find.