How to make a favourite list iphone - iphone

I am making an app that has two tabs, one of which is a "favourite" tab. In the other tab called "search", I have a list of results displayed in a table view, when you tap one, you get to see the detail of that particular result. What I'm trying to do here is, there is a button in the detail view, when it is pressed, the current result gets sent to the "favourite" tab. I tried to use delegate to pass the information, but it didn't work out. Here is my code:
DetailViewController.m
-(IBAction) addSomething {
[self.delegate detailViewController:self addToFavourite:self.something];
}
FavouriteViewController.m, implement the delegate method:
- (void) detailViewController:(DetailViewController *)detailViewController addToFavourite:(Something *)something{
detailViewController.delegate = self;
[thingsList addObject:something];
[theTableView reloadData];
}
Everything is built and fine, but when I click "add" button in the detail view, the data doesn't get sent to "favourite" tab's view. Can anyone help me with this one? Do I need to use core data in this case, I never used core data before. Thanks.

One way to do it that is drop dead easy is storing your information in NSUserDefaults.
NSUserDefaults *defaults = [NSUserDefaults standardDefaults];
[defaults setObject:thingsList forKey:#"myData"];
[defaults synchronize];
When you load your data up in your other tab, just fill it from user defaults.
NSUserDefaults *defaults = [NSUserDefaults standardDefaults];
otherData = [defaults objectForKey:#"myData"];
I probably abuse user defaults more than I should, but it makes for a very easy way to store your data locally and have it persist between runs. It will also backup with iTunes for added persistence. Hope this helps.

Related

Keep track of launches bug

I found some threads that discuss this and implemented it into my code, however I'm having an error.
I am trying to do something every 5th launch.
Also the code sets launchAmounts, is that built in into userDefaults, or do I have to declare this somewhere?
I am doing this from viewwillappear in my main view controller.
NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
NSInteger appLaunchAmounts = [userDefaults integerForKey:#"LaunchAmounts"];
appLaunchAmounts = appLaunchAmounts %5;
NSLog(#"app has been launched = %d", appLaunchAmounts);
[userDefaults setInteger:appLaunchAmounts+1 forKey:#"LaunchAmounts"];
if (appLaunchAmounts==0) {
That code looks like it should work, except for the fact that you have it in viewWillAppear. That method could be called many times in one run if you're switching back and forth between different view controllers. You should put it in the applicationDidFinishLaunching method in the app delegate.
What's not working the way you have it now?

How to make scollView (for T&C) only show up once at begin of app, then never show up again?

My questions is...how to make....this...
I am trying to make a scollview to show the term&condition at beginning of my app when the user is 1st time using the app.
if the user accepted the T&C (by clicking accept button), this T&C scollview will never show up again at beginning of the app, as he already accepted. So he will be free to use the app in future.
How do I implement this? any suggestions?
Use NSUserDefaults with a key like "TCShown". If the key does not exist in the NSUserDefaults at the beginning of the launch, you show the T&C and create "TCShown" value, set it to YES ([NSNumber numberWithBool:YES];) and store it to the NSUserDefaults.
Edit:
Assuming that you want to present the T&C in your first viewController,
#define kTCViewedFlag = #"tcViewed"
-(void) viewDidAppear {
NSUserDefaults *myDefaults = [NSUserDefaults standardUserDefaults];
if(![myDefaults objectForKey:kTCViewedFlag]) {
//show the TC
}
}
-(IBAction) userAcceptedTC {
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:YES] forKey:kTCViewedFlag];
[[NSUserDefaults standardUserDefaults] synchronize];
//dismiss the scrollView
}
-(IBAction) userDidDeclineTC {
//handle refusal of TC
}
In addition to Kaan's answer, you can add the TCShown field to the server and update the values accordingly. This will take care of the case when the user who has already accepted the T&C's logs in from a different device.
Maybe you'll find this useful: RLAgreement View Controller
This project allows developers to include
and Agreement, Terms of Service, Non Disclosure Agreement, etc. to an
iPhone App. The controller stores a variable in the user's settings
when the user has a valid agreement and it checks every time the user
opens the App.

How to add the data from tableview to sql

I am making an application. On one of the tab I have used tableview on which I am adding the local notifications timing by using time picker. I am also using sql database in my app. Now I want the timing which I have added in the table view must be store in database so that I can retrieve it on another tab. How Can I do this implementation. Please If anyone know it. Give me some solutions.
Thanks alot.
If all you need is to receive the data in an other tab I think storing in mysql is a bit overkill.
What don't you use a shared object that will be used by both tables?
An other simple option will be to store the data in NSUserDefaults
//create an array of all the data in the table view.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:array forKey:#"table data"];
[defaults synchronize];
And in the next tab
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSArray *array = [defaults objectForKey#"table data"];
//pass the array to the table view.
But that really depends on how much data you store and do you need it to be saved when the app is closed.
But the best solution in my opinion will be to use core data.
GOOD Luck

Getting variables from one view to another

I realise that there are other topics like this, but none of them really help. I'm trying to get variables from one view into another, but I have absolutely no idea how.
To give some backstory, my game is a fruit ninja like game where stuff goes on the screen and you have to slice it. If 3 sprites leave the screen unsliced, the game is over and it flips to the game over view screen with a button to go back. Additionally, this SHOULD go to the "flipSideView", which is the highscore, but my implementation of the flipSideView transition doesn't work. This isn't the main issue, the main issue is that I don't know how to get the score from the game in the mainView (which stores it as an int) into the flipSideView (which has the player name).
The main view changes to the gameOverView through this condition in the tick method (which performs regular checks and methods for the game)
if (lifeCounter < 1)
{
gameIsOver = YES;
[self showInfo:0];
[self viewGameOverScreen];
}
That goes to the gameOverView, which will sit there until the replay button is pressed with:
- (IBAction)replayAction:(id)sender
{
[self.delegate gameOverViewControllerDidFinish: self];
}
gameOverViewControllerDidFinish restarts the game and dismisses the view.
- (void) gameOverViewControllerDidFinish: (GameOverViewController *) controller
{
[self restart];
[self dismissModalViewControllerAnimated: YES];
}
The restart method just resets the 3 primary values in the main view (The score, the level, the lives).
As it restarts, the game should take the score and whatever name is stored in the text field in the flipSideView (which can be viewed at any one time gameplay) and store it somehow for future reference. It's not supposed to store it in a file yet because that's next week's task.
I don't wanna post the entire program due to plagiarism issues, but if there are additional parts that might make it easier to understand, I will definitely post them.
Thanks in advance,
Christian
Use Search
NSUserDefaults *currentScore = [NSUserDefaults standardUserDefaults];
[currentDefaults setFloat:yourScoreVariable forKey:#"HighScore"];
[currentDefaults synchronize];
Above to store the score.
Below to retrieve the score.
CGFloat fltHighScore;
NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];
fltHighScore = [currentDefaults floatForKey:#"HighScore"];
Suggestion:
You could put the variable in your Controller class,where they both could access.
It is always better to have sharable data in Controller then views if data has to be shared among views.
Update the shareable data through delegate method.
You can use your app delegate for this. You need to declare your variable there, and then you can access this in the following way throughout your whole app:
TestAppDelegate *appDelegate = (TestAppDelegate *)[[UIApplication sharedApplication] delegate];
NSString *string = [appDelegate.Placemarks objectForKey:appDelegate.title];
Don't know if it's the best way tough...
Luck with it!

Why won't my NSUserDefaults load?

So I have an iphone app that presents a modal view when it starts if the user has not registered. Once they register, it saves a user ID into NSUserDefaults. When the app starts at a later time, it needs to load this user id in the appdelegate file.
//Registration.m
NSUserDefaults *savedUID = [NSUserDefaults standardUserDefaults];
[savedUID setObject:UID forKey:#"user_id"];
//AuctionTracAppDelegate.m
NSUserDefaults *savedID = [NSUserDefaults standardUserDefaults];
NSString *uidString = [savedID stringForKey:#"user_id"];
My problem is that when I try to load this value at a separate execution, the object I saved is always null. I know for a fact that it is saving the string object correctly. Also, this weird behavior JUST surfaced. This exact code was working earlier, then after working on a totally unrelated file, this code fails. I'm clueless as to what happened. Any hints?
I believe that you need to call the synchronize method.