saving/sharing a String in xcode - iphone

i need to save a string within my application but i dont want to create a plist just for saving 1 string is there any other way to do so? im using storyboard. i tried segue but it didn't work
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString: #"modal"])
{
ViewController *vc = (ViewController *)[segue destinationViewController];
vc.subject = self.subject;
}
}

What do you mean by "I need to save a string within my applicaion"? Do you just want to pass the string to your destination ViewController?
If that's the case, you could define a new property in your destination ViewController (assuming you know which one it is)
#property (nonatomic, strong) NSString *myProperty;
And then
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString: #"modal"])
{
[segue.destinationViewController setMyProperty:self.myProperty];
}
}
If you want your string to be accessible from anywhere in the application, you can save it to the user defaults quite easily :
[[NSUserDefaults standardUserDefaults]setObject:myProperty forKey:#"myPropertyKeyName"];
You can get it back later :
NSString *mySavedProperty= [[NSUserDefaults standardUserDefaults]stringForKey:#"myPropertyKeyName"];

If u can to use the string any where in the project just create a extern variable like this way.
In AppDelegate.h
extern NSString *stringObject;
In AppDelegate.m
NSString *stringObject;
U can use the stringObject any where in the project.

Related

[UINavigationController setGoalName:]: unrecognized selector sent to instance 0x7964e2c0

I have created the app with following code. Its working fine with iOS7 but it throws the below error when I run with iOS8.
[UINavigationController setGoalName:]: unrecognized selector sent to instance 0x7964e2c0
My firstViewcontroller.m
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
GoalDetailsViewController *goalsDetailsViewController = segue.destinationViewController;
NSLog(#"%#",[NSString stringWithFormat:#"%#", [[self.arrCategoryTitle objectAtIndex:indexPath.row] objectAtIndex:indexOfCategory]]);
goalsDetailsViewController.goalName = #"Exercise Daily";
}
My GoalDetailsViewController.h
#interface GoalDetailsViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
#property (nonatomic) NSString *goalName;
Thanks in advance.
Seems like your destinationviewcontroller is a subclass of UINAvigationController.
Try this:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
GoalDetailsViewController *goalsDetailsViewController = [(UINavigationController*)segue.destinationViewController topViewController];
NSLog(#"%#",[NSString stringWithFormat:#"%#", [[self.arrCategoryTitle objectAtIndex:indexPath.row] objectAtIndex:indexOfCategory]]);
goalsDetailsViewController.goalName = #"Exercise Daily";
}
The easiest way to handle this crash would be to simply make sure that the destinationViewController is of the type you're expecting before you attempt to set a property on it. Something like this:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
GoalDetailsViewController *goalsDetailsViewController = segue.destinationViewController;
NSLog(#"%#",[NSString stringWithFormat:#"%#", [[self.arrCategoryTitle objectAtIndex:indexPath.row] objectAtIndex:indexOfCategory]]);
if ([segue.destinationViewController isKindOfClass:[GoalDetailsViewController class]]) {
GoalDetailsViewController *goalsDetailsViewController = segue.destinationViewController;
goalsDetailsViewController.goalName = #"Exercise Daily";
}
}
This change ensures that the destinationViewController is of kind GoalDetailsViewController before treating it as such.

segues in storyboard work perfectly but the prepare for segue is not being called

So I have a standard method that everyone uses in the prepare for segue.
When I think logically "prepare" means do this before the segue is called.
But I have set up 3 table cells that all have 1 segue to the SecondViewController.
When I test that it works perfectly no errors.
Now when I want to add to this app that when the user selects the first cell the label "labeltje" get another text value this does not happen.
Of course this code is in my TableViewController.m file and not in the DagenViewController, which is my second controller.
What am I not seeing?
Dagenviewcontroller is imported etc. no issues there.
This is the prepareforsegue bit of code:
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
DagenViewController *secondVC = (DagenViewController *)segue.destinationViewController;
if([segue.identifier isEqualToString:#"vrijdagSegue"])
{
NSString *vrijdag = #"vrijdag";
secondVC.labeltje.text = vrijdag;
}
else if([segue.identifier isEqualToString:#"zaterdagSegue"])
{
NSString *zaterdag = #"zaterdag";
secondVC.labeltje.text = zaterdag;
}
else {
NSString *zondag = #"zondag";
secondVC.labeltje.text = zondag;
}
}
Or is there a way to put something in the ViewDidLoad method of my SecondViewController that checks which segue was used?
Thank you for reading.
Try to do this way:
DagenViewController.h file
#property (strong, strong) NSString *preText
DagenViewController.m file
- (void) viewDidLoad
{
self.labeltje.text = preText;
}
In Your Method
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
DagenViewController *secondVC = (DagenViewController *)segue.destinationViewController;
if([segue.identifier isEqualToString:#"vrijdagSegue"])
{
secondVC.preText = #"vrijdag";
}
else if([segue.identifier isEqualToString:#"zaterdagSegue"])
{
secondVC.preText = #"zaterdag";
}
else {
secondVC.preText = #"zondag";
}
}
When prepareForSegue:sender: is called you are accessing a label that is nil since the view controller's view was not yet loaded. Create a string property(that you will set in prepareForSegue:sender:) and then in your DagenViewController viewDidLoad method set the label's text from that string property.

Your Second iOS App: Edit one object in two different views

I am going through the tutorials provided by Apple and tried to improve "My Second iOS App", the app for bird sightings.
(There is a MasterView where all entered sightings are listed. If you click one, you are directed to a DetailView of the sighting. You can add a sighting and are asked to enter a name and location.)
I want to sepperate the views for entering the birds name and location.
So I have two views (one for entering the name and one for entering the location) and one object I want to store.
In the file BirdSighting.m I added the following methods
-(id)initWithNameOnly:(NSString *)name date:(NSDate *)date
{
self = [super init];
if (self) {
_name = name;
_date = date;
return self;
}
return nil;
}
and
-(id)setLocation:(NSString *)location
{
if (self) {
_location = location;
return self;
}
return nil;
}
In the AddSightingNameViewController.m I implemented the following code
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"ToLocation"])
{
if ([self.birdNameInput.text length])
{
BirdSighting *sighting;
NSDate *today = [NSDate date];
sighting = [[BirdSighting alloc] initWithNameOnly:self.birdNameInput.text date:today];
self.birdSighting = sighting;
}
}
}
The view for entering the name leads with a push segue to the location-view. There has'nt been changed much else.
Now how do I pass the object generated in the first view to the second? And how do I call the setLocation method on this specific object in AddSightingLocationViewController.m? Do I have to define different properties? And how do I finally display the object in the MasterView with the correct data after I entered the location?
As this code is not working yet, I don't even know if it is working, what I am trying to do. So please be gentle, if this is crappy code.
This is the method I have been using:
First you will need to add a property in your destination view controller, to hold the object you want to pass:
#property (strong, nonatomic) BirdSighting *newSighting;
Then change the prepareForSegue method in your first view controller to the following:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"ToLocation"])
{
if ([self.birdNameInput.text length])
{
BirdSighting *sighting;
NSDate *today = [NSDate date];
sighting = [[BirdSighting alloc] initWithNameOnly:self.birdNameInput.text date:today];
self.birdSighting = sighting;
// Get destination view
YourDestinationViewController *vc = (YourDestinationViewController *)segue.destinationViewController;
// Pass birdSighting object to your destination view controller
[vc setNewSighting:self.birdSighting];
}
}
}
I think I originally got this method from this question
It is also worth noting that the BirdSighting class has a location #property in it's .h file & you will notice the #synthesize line in the .m file.
The #synthesize directive automatically creates accessor methods for you:
#property (nonatomic, copy) NSString *location;
Has the following methods automatically generated (but not visible in the file):
- (NSString *)location;
- (void)setValue:(NSString *)location;
Therefore it was unnecessary for you to override the setter method for location in the BirdSighting.m file with:
-(id)setLocation:(NSString *)location
If you remove that method (note that it should return void not id) you should now be able to access the location variable in a BirdSighting object in the following way:
// In this example we are accessing a BirdSighting #property (hence the use of self.sighting)
// #property (strong, nonatomic) BirdSighting *sighting;
// Getter - returns (NSString *)location of the BirdSighting object
[self.sighting location];
// Setter - sets the location property of the BirdSighting object to 'newLocation'
[self.sighting setLocation:newLocation];
Hope this clears some things up for you!

Passing an int to a new ViewController

Ive spent a month trying to pass my int to a new view. My int is called score and is connected to a label called scorelab. The user increases the score and once the they run out of time game switches views to game over screen and i would like the users score to be displayed on the game over screen in a label. The game play controller is called GamePlayViewController and the gave over controller is called GameDidEndViewController. I'm tired of trying to get this to work. I have looked at every tutorial possible but nothing seems to work for my case. Please help me i would appreciate it so so much, this is the very last part of game that I need to finish. Thank you in advance!
Sorry here is the code:
GamePlayViewController.h
#import "GameDidEndViewController.h"
int score;
IBOutlet UILabel *scorelab;
GamePlayViewController.m
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"GameOver"])
{
GameDidEndViewController *vc = [segue destinationViewController];
[vc setscore2: score];
}
}
GameDidEndViewController.h
#import "GamePlayViewController.h"
IBOutlet UILabel *scorelab2;
int score2;
}
#property (nonatomic , assign) int score2;
#property (nonatomic , assign) UILabel *scorelab2;
GameDidEndViewController.m
#synthesize score2 , scorelab2;
-(void)viewWillAppear:(BOOL)animated {
scorelab2.text = [NSString stringWithFormat: #"%d", score2];
[super viewWillAppear: animated];
}
What you do is create a property in GameDidEndViewController to hold the passed in score value.
#interface GameDidEndViewController : UIViewController {
....
}
#property(nonatomic,assign) int score;
#end
Make sure you #synthesize score in the #implementation(.m).
Now when you init and show the GameDidEndViewController you set the score, likely something like this.
GameDidEndViewController *end = .....// init the view.
end.score = score; // assuming you're doing this from GamePlayViewController where it has an int named `score`
Hope this helps !
Here is some tutorial for passing data between ViewControllers using Storyboard:
Storyboards Segue Tutorial: Pass Data Between View Controllers
Tutorial on How-To Pass Data Between Two View Controllers
or use something like this:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:...]) {
MyViewController *controller = (MyViewController *)segue.destinationViewController;
controller.score = score;
}
}
Hope this helps.

No visible #interface for 'FirstViewController' declares the selector 'setUIImage'

first i want to say i have searched for an answer to this error, but none of the answers seems to fit me.
what i am trying to do is pass an uiimage from one vc to another, this is the code that "should" do it. But i get the error "No visible #interface for 'FirstViewController' declares the selector 'setUIImage'"
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"ImageEdit"])
{
FirstViewController *vc = [segue destinationViewController];
[vc setUIImage:image];
}
}
In the interface file for the destinationViewController, do:
#property (strong, nonatomic) NSObject *uIImage;
and in the implementation file for the destinationViewController, do:
#synthesize uIImage;
HTH