Iphone - How to send a message from one object to another?
I'm kind of new to objective programming, so sorry for the dumb question. I'm making a kind of messenger for some social network and I'm stuck with a very simple thing - how to send a message from an object of one class to an object of another class?
I have a class called SignInViewController that instantiates SignUpViewController after the SignUpButton is clicked and runs like this:
SignUpViewController *signUpViewController = [[SignUpViewController alloc]init];
signUpViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:signUpViewController animated:YES];
Then after some control with AFNetwork (I use a certain class called ServerManager for this) I want to send a message to draw a new textbox in my SignUpViewController instance and I think it can be done like this
in SignUpViewController.h:
- (void)showTheCodeTextField;
in ServerManager.m:
[[SignUpViewController self] showTheCodeTextField];
and then in SignUpViewController.m:
-(void)showTheCodeTextField
{
NSLog(@"Time to draw codeTextField");
}
I am getting the familiar SIGABRT on the last line of code. I know I am doing something wrong, but I just cannot figure out what exactly.
Could you help me?
source to share
You can use NSNotificationCenter to get the job done. After presenting your SignUpController, you add it as an observer for notifications sent by the ServerManager. And when that notification arrives, 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 server manager you send notification, specify that textField and the method in your SignUp will be called. Here it is. You have your textField innotification.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]);
}
source to share
If you have a SignUpViewController instance in your ServerManager instance, you must use this convention (if your instance is named "signUpController"):
[[self signUpController] showTheCodeTextField];
Another possibility is to send a notification to the ServerManager containing a link to your SignUpViewController and send the message showTheCodeTextField. This assumes that you know how to send a notification.
Good luck with your endeavors. It looks like you are just getting started with Cocoa and Objective-C. Hang there!
source to share