Note that there are some explanatory texts on larger screens.

plurals
  1. PO"Your Second iOS App": How do I implement a Singleton?
    primarykey
    data
    text
    <p>I am still trying to improve <a href="http://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/SecondiOSAppTutorial/Introduction/Introduction.html" rel="nofollow noreferrer">"My Second iOS App"</a>, which is a tutorial provided by Apple. Here is a picture of the storyboard with added commentary of what should happen.</p> <p><img src="https://i.stack.imgur.com/5W0mo.png" alt="detail of the storyboard with added commentary"></p> <p>When the done button (step 3) is clicked, the actual screen should return to the main menu (3.a) while the finished BirdSightingObject should be added to the list of <code>BirdMasterViewController</code> (3.b). So I figured that I could use a Singleton, since there could be only one object added simultaniously and it makes accessing from different classes much easier.</p> <p>In the tutorial a class is already given for data handeling, "<code>BirdSighting</code>", which could probably be used as a singleton. But as I have only beginner level knowledge of OOP and design patterns I don't know, if I can use this, or if I have to to write my own in reference to the existing class.</p> <p>And secondly: I haven't the slightest idea how to access the <code>BirdMasterViewController</code> in the <code>(IBAction)done</code> method to finally save the object in the list.</p> <p><sup>Note: The following source code is partially provided by Apple, Inc. Refer to <a href="http://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/SecondiOSAppTutorial/CodeListings/CodeListings.html" rel="nofollow noreferrer">this</a> to see a full code listing of the tutorial. No copy right infridgement is intended.</sup></p> <p><code>BirdSighting.h</code>:</p> <pre><code>@interface BirdSighting : NSObject @property (nonatomic, copy) NSString *name; @property (nonatomic, copy) NSString *location; @property (nonatomic, strong) NSDate *date; - (id)initWithName:(NSString *)name location:(NSString *)location date:(NSDate *)date; - (id)initWithNameOnly:(NSString *)name date:(NSDate *)date; @end </code></pre> <p><code>BirdSighting.m</code>:</p> <pre><code>#import "BirdSighting.h" @implementation BirdSighting - (id)initWithName:(NSString *)name location:(NSString *)location date:(NSDate *)date { self = [super init]; if (self) { _name = name; _location = location; _date = date; return self; } return nil; } -(id)initWithNameOnly:(NSString *)name date:(NSDate *)date { self = [super init]; if (self) { _name = name; _date = date; return self; } return nil; } @end </code></pre> <p>The <code>(IBAction)done</code> method, in <code>MainMenuViewController.m</code>:</p> <pre><code>- (IBAction)done:(UIStoryboardSegue *)segue { if ([[segue identifier] isEqualToString:@"ReturnInput"]) { AddLocationToSighting *addController = [segue sourceViewController]; if (addController.birdSighting) { [self.dataController addBirdSightingWithSighting:addController.birdSighting]; [[self tableView] reloadData]; } [self dismissViewControllerAnimated:YES completion:NULL]; } } </code></pre> <p>Summary:</p> <ul> <li><strong>May I transform</strong> <code>BirdSighting</code> <strong>into a singleton?</strong> And if so, what is missing? (No need to code it out for me, I hope I can do it on my own if you provide a hint.)</li> <li><strong>How do I save the object in the</strong> <code>BirdMasterViewController</code><strong>?</strong> I have no idea how to access the noninvolved ViewController in <code>(IBAction)done</code>.</li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload