Commits

Alfonso Alba García committed 1e788ab

Choosing a group when creating a user.

when the user is created, an UIPicekrView allows us to choose a group.The Parse Data Browser shows that the relations are being created.

  • Participants
  • Parent commits c2e8507

Comments (0)

Files changed (5)

ParseTest.xcodeproj/project.pbxproj

 		AC88ACFA1662AA7E000D05F0 /* PTGroupsViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = AC88ACF81662AA7D000D05F0 /* PTGroupsViewController.xib */; };
 		AC88ACFE1662AB8E000D05F0 /* PTPeopleViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = AC88ACFC1662AB8E000D05F0 /* PTPeopleViewController.m */; };
 		AC88ACFF1662AB8E000D05F0 /* PTPeopleViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = AC88ACFD1662AB8E000D05F0 /* PTPeopleViewController.xib */; };
-		AC88AD061662B4A1000D05F0 /* groups.png in Resources */ = {isa = PBXBuildFile; fileRef = AC88AD051662B4A1000D05F0 /* groups.png */; };
 		AC88AD0C16639BA2000D05F0 /* users.png in Resources */ = {isa = PBXBuildFile; fileRef = AC88AD0B16639BA2000D05F0 /* users.png */; };
 		AC88AD0E1663AF29000D05F0 /* StandardTableViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = AC88AD0D1663AF29000D05F0 /* StandardTableViewCell.xib */; };
 		AC88AD1C1663F778000D05F0 /* PTGroupsNavController.m in Sources */ = {isa = PBXBuildFile; fileRef = AC88AD1B1663F778000D05F0 /* PTGroupsNavController.m */; };
 		AC88ACFB1662AB8E000D05F0 /* PTPeopleViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PTPeopleViewController.h; sourceTree = "<group>"; };
 		AC88ACFC1662AB8E000D05F0 /* PTPeopleViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PTPeopleViewController.m; sourceTree = "<group>"; };
 		AC88ACFD1662AB8E000D05F0 /* PTPeopleViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = PTPeopleViewController.xib; sourceTree = "<group>"; };
-		AC88AD051662B4A1000D05F0 /* groups.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = groups.png; path = ../groups.png; sourceTree = "<group>"; };
 		AC88AD0B16639BA2000D05F0 /* users.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = users.png; sourceTree = "<group>"; };
 		AC88AD0D1663AF29000D05F0 /* StandardTableViewCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = StandardTableViewCell.xib; sourceTree = "<group>"; };
 		AC88AD1A1663F778000D05F0 /* PTGroupsNavController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PTGroupsNavController.h; sourceTree = "<group>"; };
 				ACBB31C41665808100078139 /* wallpaper@2x.jpg */,
 				ACBB31C51665808100078139 /* wallpaper.png */,
 				AC88AD0B16639BA2000D05F0 /* users.png */,
-				AC88AD051662B4A1000D05F0 /* groups.png */,
 				AC88AD2116643895000D05F0 /* circle_plus.png */,
 				ACCEA24F165FAC1500BC3BB8 /* ParseTest-Info.plist */,
 				ACCEA250165FAC1500BC3BB8 /* InfoPlist.strings */,
 				ACCEA25E165FAC1500BC3BB8 /* Default-568h@2x.png in Resources */,
 				AC88ACFA1662AA7E000D05F0 /* PTGroupsViewController.xib in Resources */,
 				AC88ACFF1662AB8E000D05F0 /* PTPeopleViewController.xib in Resources */,
-				AC88AD061662B4A1000D05F0 /* groups.png in Resources */,
 				AC88AD0C16639BA2000D05F0 /* users.png in Resources */,
 				AC88AD0E1663AF29000D05F0 /* StandardTableViewCell.xib in Resources */,
 				AC88AD2216643895000D05F0 /* circle_plus.png in Resources */,

ParseTest/PTAddPersonViewController.h

 
 #import <UIKit/UIKit.h>
 #import "PTPersonCRUDDelegateProtocol.h"
-@interface PTAddPersonViewController : UITableViewController <UITextFieldDelegate>
+@interface PTAddPersonViewController : UITableViewController <UITextFieldDelegate, UIPickerViewDataSource, UIPickerViewDelegate>
 
 @property (weak, nonatomic) IBOutlet UITextField *firstNameTextField;
 @property (weak, nonatomic) IBOutlet UITextField *lastNameTextField;
 @property (weak, nonatomic) IBOutlet UITextField *twitterTextField;
 
 @property (strong, nonatomic) id<PTPersonCRUDDelegateProtocol> delegate;
+@property (weak, nonatomic) IBOutlet UIPickerView *groupPicker;
+
+@property (strong, nonatomic) NSArray *groups;
 
 @end

ParseTest/PTAddPersonViewController.m

 
 @implementation PTAddPersonViewController
 
+
 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
 {
     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
     return YES;
 }
 
+#pragma marck UIPickerViewDataSource
+
+-(NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView
+{
+    return 1;
+}
 
+-(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
+{
+    return [self.groups count];
+}
+
+#pragma mark UIPickerViewDelegate
+
+- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
+{
+    PFObject *group = [self.groups objectAtIndex:row];
+    return [group objectForKey:@"name"];
+}
 
 #pragma mark Actions
 - (void) savePersonAction:(id)sender
 {
     NSLog(@"Saving person");
-    NSArray *objects = [NSArray arrayWithObjects:self.firstNameTextField.text, self.lastNameTextField.text, self.emailTextField.text, self.twitterTextField.text, nil];
-    NSArray *keys = [NSArray arrayWithObjects:@"firstName",@"lastName",@"email",@"twitter", nil];
+    NSInteger selectedGroupRow = [self.groupPicker selectedRowInComponent:0];
+    NSArray *objects = [NSArray arrayWithObjects:self.firstNameTextField.text, self.lastNameTextField.text, self.emailTextField.text, self.twitterTextField.text, [self.groups objectAtIndex:selectedGroupRow], nil];
+    NSArray *keys = [NSArray arrayWithObjects:@"firstName",@"lastName",@"email",@"twitter",@"group", nil];
     NSDictionary *data = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
     [self.delegate addPerson:data];
     [self.navigationController popViewControllerAnimated:YES];
     [self.emailTextField resignFirstResponder];
     [self.twitterTextField resignFirstResponder];
 }
+
 @end

ParseTest/PTAddPersonViewController.storyboard

                         <rect key="frame" x="0.0" y="20" width="320" height="548"/>
                         <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
                         <color key="backgroundColor" cocoaTouchSystemColor="groupTableViewBackgroundColor"/>
+                        <pickerView key="tableFooterView" contentMode="scaleToFill" id="HOc-MR-ZOO">
+                            <rect key="frame" x="0.0" y="282" width="320" height="216"/>
+                            <autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMaxY="YES"/>
+                            <connections>
+                                <outlet property="dataSource" destination="IRh-Rw-SjJ" id="tUa-dR-s3A"/>
+                                <outlet property="delegate" destination="IRh-Rw-SjJ" id="uDL-Bs-Yiv"/>
+                            </connections>
+                        </pickerView>
                         <sections>
                             <tableViewSection headerTitle="Personal data" id="Hll-4g-9dC">
                                 <cells>
                     <connections>
                         <outlet property="emailTextField" destination="qIi-jj-IHo" id="3Dy-3l-hqw"/>
                         <outlet property="firstNameTextField" destination="WFW-CA-cbv" id="GP8-pa-TwG"/>
+                        <outlet property="groupPicker" destination="HOc-MR-ZOO" id="f45-iV-zQe"/>
                         <outlet property="lastNameTextField" destination="jkX-Qf-qHC" id="kSe-1E-4ut"/>
                         <outlet property="twitterTextField" destination="wcG-iT-iaz" id="Zub-f4-cML"/>
                     </connections>
             <relationships>
                 <relationship kind="outlet" name="emailTextField" candidateClass="UITextField"/>
                 <relationship kind="outlet" name="firstNameTextField" candidateClass="UITextField"/>
+                <relationship kind="outlet" name="groupPicker" candidateClass="UIPickerView"/>
                 <relationship kind="outlet" name="lastNameTextField" candidateClass="UITextField"/>
                 <relationship kind="outlet" name="twitterTextField" candidateClass="UITextField"/>
             </relationships>

ParseTest/PTPeopleViewController.m

 @interface PTPeopleViewController ()
 {
     NSArray *people;
+    NSArray *groups;
 }
 @end
 
 {
     PFQuery *query = [PFQuery queryWithClassName:@"Person"];
     [query orderByAscending:@"firstName"];
-    [query orderByAscending:@"lastName"];
     [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
         if(!error)
         {
             });
         }
     }];
+    
 }
 
 #pragma mark Actions
     UIStoryboard *tableViewStoryboard = [UIStoryboard storyboardWithName:@"PTAddPersonViewController" bundle:nil];
     PTAddPersonViewController *viewController = [tableViewStoryboard instantiateViewControllerWithIdentifier:@"PTAddPersonViewController"];
     viewController.delegate = self;
-    [self.navigationController pushViewController:viewController animated:YES];
+    PFQuery *groupsQuery = [PFQuery queryWithClassName:@"Group"];
+    [groupsQuery orderByAscending:@"name"];
+    [groupsQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
+        groups = objects;
+        viewController.groups = groups;
+        [self.navigationController pushViewController:viewController animated:YES];
+    }];
+
 }
 
 #pragma mark PTPersonCRUDDelegateProtocol
 
 - (void) addPerson:(NSDictionary *)data
 {
+    PFObject *group = [data objectForKey:@"group"];
     PFObject *person = [PFObject objectWithClassName:@"Person"];
     [person setObject:[data objectForKey:@"firstName"] forKey:@"firstName"];
     [person setObject:[data objectForKey:@"lastName"] forKey:@"lastName"];
     [person setObject:[data objectForKey:@"email"] forKey:@"email"];
     [person setObject:[data objectForKey:@"twitter"] forKey:@"twitter"];
-    
+    PFRelation *memberOf = [person relationforKey:@"memberOf"];
+    [memberOf addObject:group];
     [person saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
         if(succeeded)
         {
             dispatch_async(dispatch_get_main_queue(), ^{
                 [self loadDataInBackground];
             });
+            
+            //We cannot create the inverse relation until the object is saved
+            PFRelation *relation = [group relationforKey:@"members"];
+            [relation addObject:person];
+            [group saveInBackground];
         }
         else
         {
             NSLog(@"Person could not be saved.");
         }
     }];
-    
 }
 @end