morgan1189
morgan1189

Reputation: 279

iphone - How to send a message from one object to another?

I'm kind of a newbie to objective programming, so sorry for the dumb question. I'm doing some kind of a messenger for some social network, and i'm stuck at the very simple thing - how to send a message from the object of one class to the object of the another class?

I have a class, called SignInViewController, which creates an instance of SignUpViewController after a SignUpButton is pressed and it's done just like that:

SignUpViewController *signUpViewController = [[SignUpViewController alloc]init];
signUpViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:signUpViewController animated:YES];

Then, after some management done with the AFNetwork (i use a specific class for this, called ServerManager), i want to send the message to draw a new textfield in my SignUpViewController instance, and i think it could be done like that:

in the SignUpViewController.h:

- (void)showTheCodeTextField;

in the ServerManager.m:

[[SignUpViewController self] showTheCodeTextField];

and then in SignUpViewController.m:

-(void)showTheCodeTextField
{
     NSLog(@"Time to draw codeTextField");
}

I am getting a familiar SIGABRT at the latter line of code. I know I am doing something wrong, but I just can't figure out what exactly.

Could you help me out please?

Upvotes: 3

Views: 787

Answers (5)

Muzamil Hassan
Muzamil Hassan

Reputation: 851

it is better if you use delegate on button event declare your delegate where you create your button and just use that delegate in any class where ever you want just use that delegate it is very helpful learn delegate for objective c its easy and efficient

Upvotes: 2

Nikita Pestrov
Nikita Pestrov

Reputation: 5966

You can use NSNotificationCenter to make that work. After presenting your SignUpController you add it as an observer of notifications that ServerManager sends.And when that notification comes, you call your message to draw the view.

So, after

SignUpViewController *signUpViewController = [[SignUpViewController alloc]init];
signUpViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:signUpViewController animated:YES];

Make an observer

[[NSNotificationCenter defaultCenter]signUpViewController selector:@selector(showTheCodeTextField:) name:@"SignUpSucceded" object:[ServerManager sharedInstance]];

Than, in your Server Manager, you send a notification, containig that textField, and the method in your SignUp gets called.That's it.You've got the textField text in your notification.userinfo

  [[NSNotificationCenter defaultCenter]postNotificationName:@"SignUpSucceded" object:self userInfo:[NSDictionary dictionaryWithObject:textField.text forKey:@"login"]];
     }

Getting tour Text here in signUpView

  -(void)showTheCodeTextField:(NSNotification*)notification
{
     NSLog(@"Time to draw codeTextField %@",[notification userInfo]);
}

Upvotes: 2

Extra Savoir-Faire
Extra Savoir-Faire

Reputation: 6036

If you have an instance of SignUpViewController in your ServerManager instance, you'd use this convention (assuming your instance is named 'signUpController'):

[[self signUpController] showTheCodeTextField];

Another possibility would be to send your ServerManager a notification containing a reference to your SignUpViewController, and passing the showTheCodeTextField message to that. This assumes you have knowledge of how to send a notification.

Good luck to you in your endeavors. You look like you're just getting started in Cocoa and Objective-C. Hang in there!

Upvotes: 1

Rok Jarc
Rok Jarc

Reputation: 18865

Looks like you're trying to send a message to a class (SignUpViewController) instead to it's instance/the object (signUpViewController):

Change

[[SignUpViewController self] showTheCodeTextField];

to

[[signUpViewController self] showTheCodeTextField];

and you should be o.k.

Upvotes: 1

N_A
N_A

Reputation: 19897

Your ServerManager class needs to have a reference to the SignUpViewController instance. Right now you are getting the self of the class not of the class instance. You should probably have a property on your ServerManager class which references the instance of your SignUpViewController.

Upvotes: 1

Related Questions