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?

+3


source to share


5 answers


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]);
}

      

+2


source


better if you use delegate in button event, declare your delegate where you create your button and just use this delegate in any class wherever you want to use this delegate, it is very useful to teach delegate to objective c its simple and efficient



+2


source


Your class ServerManager

must have an instance reference SignUpViewController

. Right now, you are getting a self

class, not an instance of the class. You should probably have a property in the class ServerManager

that references an instance of your SignUpViewController

.

+1


source


It looks like you are trying to send a message to class ( SignUpViewController

) instead of instance / object ( SignUpViewController

):

Change

[[SignUpViewController self] showTheCodeTextField];

      

to

[[SignUpViewController self] showTheCodeTextField];

      

and you should be ok

+1


source


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!

+1


source







All Articles