iOS Tutorial iOS Tutorial Part II

View the Project on GitHub FatFractal/hoodyoodoo

Modify WouldYaViewController

Now, with the two Celebrity objects retrieved, we need to add the logic to create and persist the players WouldYa results to the API. This involves creating a new object, that we will call a WouldYa. In order to create the “WouldYa” class for your project, click “File”, “New”, “New File” to bring up the template chooser for the new class file. We will use the standard Objective-C class template. Click Next.

We now enter the name of the Objective-C class that we want to create (“WouldYa”) as a subclass of NSObject.

Clicking “Next” will create a WouldYa.h and WouldYa.m file in your project.

If you open the WouldYa.h file, you will see that it contains:

@interface WouldYa : NSObject
@end

If you open the WouldYa.m file, you will see that it contains:

#import "WouldYa.h"
@implementation WouldYa
@end

We now need to modify these files to include the ivars that you want for this object.

Creating the WouldYa.h header file

For the WouldYa class, we will modify the WouldYa.h file as follows.

@interface WouldYa : NSObject {
    NSString        *selectedGuid;
    NSString        *rejectedGuid;
}
@property (strong, nonatomic) NSString          *selectedGuid;
@property (strong, nonatomic) NSString          *rejectedGuid;
@end

Creating the WouldYa.m implementation file

For the WouldYa.m file, we follow a typical object model pattern, and we include a description method for convenience as well for good practice.

#import "WouldYa.h"
@implementation WouldYa
@synthesize selectedGuid, rejectedGuid;
- (id)init {
    self = [super init];
    if (self) {
    }
    return self;
}
- (NSString*) description {
    return [[NSString alloc]
    initWithFormat:@"WouldYa[selectedGuid[%@] rejectedGuid[%@]]",
    [self selectedGuid], [self rejectedGuid]];
}
@end

Modify WouldYaViewController.h

Next, we need to modify the WouldYaViewController.h header file as follows:

Add the import for the WouldYa class:

#import "WouldYa.h"
// Add the following properties:
@property (strong, nonatomic) WouldYa            *wouldYa;
@property (strong, nonatomic) IBOutlet UIButton  *rightCelebrityButton;
@property (strong, nonatomic) IBOutlet UILabel   *rightCelebrityLabel;
@property (strong, nonatomic) IBOutlet UIButton  *genderButton;
@property (strong, nonatomic) IBOutlet UIButton  *playAgainButton;
@property (strong, nonatomic) NSString           *currentGender;

Add the following methods

The toggleGender method toggles the value of the  currentGender property when the genderButton is clicked:

- (IBAction)toggleGender:(id)sender;

The celebrityWasPicked method sets the value of the  wouldYa property when either the leftCelebrityButton or rightCelebrityButton is clicked:

- (IBAction)celebrityWasPicked:(id)sender;

The persistWouldYa method verifies that the user is logged in, that wouldYa is not nil and then persists wouldYa to the API.

- (void) persistWouldYa;

The playAgain method calls loadCelebrities when playAgainButton is clicked:

- (IBAction)playAgain:(id)sender;

NEXT: Logic for WouldYa