0

I've created a User class with a User object allocated in the first view controller. While in the first view controller, a few of the objects properties are filled with some data. I'm using a second view controller to obtain additional user information and then send it back to the first view controller to store in the remaining object properties. I'm using a protocol and delegation to get this done. I followed the instructions here: Passing Data between View Controllers

Looks like I did everything correctly, except i don't know how to associate the results with the properties of my object. It would be nice to be able to return to the original method in the first view controller that calls the second view controller. Is this possible? A lot of the answers I'm seeing are requiring global variables, but I'm not sure if thats necessary for my case.

/**Implementation of Main View Controller **/
-(IBAction)signUpButton
{

    User * firstUser = [[User alloc] init];

    firstUser.userName = userNameField.text;
    firstUser.password = passwordField.text;

    /**second view controller **/

    SetUp *setUpView = [[SetUp alloc] initWithNibName:Nil bundle:Nil];

    setUpView.delegate = self;

    [self presentViewController:setUpView animated:YES completion:^{ }];

    firstUser.zone = holdZoneInfo;
    firstUser.area= holdAreaInfo;

    NSLog(@"first User %@, %@, %@, %@",firstUser.userName, firstUser.password, firstUser.zone, firstUser.area);

    /**username and password display fine, but zone and area are null since the delegation operation hasn't been completed as this point **/

}

/**Protocol Method Declared in Main View Controller**/
-(void) sendUserInfoBack: (SetUp *) SetUpController didFinishWithZone:(NSString*)item1 didFinishWithArea:(NSString*) item2
{

    holdZoneInfo = item1;
    holdAreaInfo = item2;

    NSLog(@"Delegation result: %@ %@", holdZoneInfo, holdAreaInfo);
    /**This displays correctly**/

}

/**Second view controller implementation file**/

-(IBAction)goToMainView:(id)sender
{
    NSString * neededZoneStore = zoneField.text;
    NSString * neededAreaStore = areaField.text;

    User * user = [[User alloc] init];

    user.zone = neededZoneStore;
    user.area = neededAreaStore;

    [self.delegate sendUserInfoBack:self didFinishWithZone: neededZoneStore didFinishWithArea: neededAreaStore];


    [self dismissViewControllerAnimated:YES completion:NULL];
}

So I need firstUser.zone = holdZoneInfo but I'm unable to make this happen

Community
  • 1
  • 1
Brosef
  • 2,945
  • 6
  • 34
  • 69

1 Answers1

0

In your SetUp view controller class, declare a user property of the type User. When you alloc/init a SetUp view controller set the user property to the firstUser. When the SetUp view controller calls the delegate method, return the firstUser object.

And let the SetUp view controller fill the User object with data from the user.

// in signUpButton
setUpView.user = firstUser;

Refactor the name of the delegate method:

- (void)setUpViewController:(SetUp *)controller exitedWithUser:(User *)user;
bilobatum
  • 8,918
  • 6
  • 36
  • 50
  • Thanks for the advice, but not sure I follow. So I created a User class property titled user in my SetUp view controller. I can alloc/init user and store the needed properties in my SetUp view controller, but I don't see how I can connect this with my object firstUser in my main view controller. – Brosef Dec 09 '13 at 05:46
  • I think i'm starting to see. So I would need to change my delegate method from: `-(void) sendUserInfoBack: (SetUp *) SetUpController didFinishWithZone:(NSString*)item1 didFinishWithArea:(NSString*) item2` to `-(void) sendUserInfoBack:(SetUp*)SetUpController exitedWithUser:(User*) user;` is that correct? – Brosef Dec 09 '13 at 06:02
  • Shouldnt it be `-(User*) sendUserInfoBack:(SetUp*)SetUpController exitedWithUser:(User*) user;` – Brosef Dec 09 '13 at 06:10
  • A return value for a delegate method is only for the benefit of the delegating object, so return void here. The delegating object already has access to the user. Note: the SetUp view controller is considered the delegating object in this case. The main view controller is the delegate object. Sorry for the terms. – bilobatum Dec 09 '13 at 06:14
  • Doesn't seem to work. I think i have improperly defined this method in the main view controller implementation file: `-(void) sendUserInfoBack:(SetUp*)SetUpController exitedWithUser:(User*) user;` Right now I just have it defined as `return user;` – Brosef Dec 09 '13 at 06:19
  • i get the following error for this method `-(void) sendUserInfoBack:(SetUp*)SetUpController exitedWithUser:(User*) user;` "Implicit conversion of an objective-c pointer to 'void' is disallowed with ARC" – Brosef Dec 09 '13 at 06:28