Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 836
RNelke Avatar asked Mar 21 '26 03:03

RNelke


1 Answers

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!

like image 145
Pliskin Avatar answered Mar 23 '26 18:03

Pliskin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!