0

I have a scoreboard app that has many UITextFields that holds the scores for each player of every round. When clicking on the text field, instead of just using the keyboard to input a score, I want to create another viewController (a popover or a page-curl modal segue would work). This 2nd view controller has a special calculator the player can use to input their score. So basically every score box triggers an action to the calculator view. Once I have the result on the calculator view, I want to dismiss the view and have the score that was just calculated to appear in the score box that cause the segue to occur. (I want to transfer the a specific UILabel.text from the 2nd view Controller back to the first view controller).

After trying a bunch ways to do this, I feel like I must learn about delegates. I'm trying to wrap my head around this concept and see if this really does apply to what I'm trying to do. It seems I have to somehow have my UITextFields (the score boxes) "listen" or "wait for" the calculator result to change. Is this correct? Am I over complicating things? Do I need to learn about delegates to make this work?

iOSAppGuy
  • 633
  • 7
  • 23

1 Answers1

0

Yes you can play around with delegation , that way you can achieve what you want to.Also you can use "NSNotificationCenter" to listen to specific events and call methods or whatever you want to.

Like this:

Add an observer :

[[NSNotificationCenter defaultCenter] addObserver:self
        selector:@selector(receiveTestNotification:) 
        name:@"TestNotification"
        object:nil];

then post it from where you want to like you said from textfield input:

[[NSNotificationCenter defaultCenter] 
        postNotificationName:@"TestNotification" 
        object:self];

And receive it in a method like this

-(void) receiveTestNotification:(NSNotification *) notification
{
    // [notification name] should always be @"TestNotification"
    // unless you use this method for observation of other notifications
    // as well.

    if ([[notification name] isEqualToString:@"TestNotification"])
        NSLog (@"Successfully received the test notification!");
}
Stunner
  • 12,025
  • 12
  • 86
  • 145
Suny
  • 1,175
  • 1
  • 10
  • 27
  • Cool thanks! I will play around with the NSNotification class. Is this the basic idea: my scoreboard class creates an observer (do I put this in the viewDidLoad method?) then on the calculator class, I call the postNotificationName method when I click the sumCalulator method. That causes the receiveTestNotication method to execute. This method sets the score box from the first view controller to the result of the calculator. Is this right? – iOSAppGuy Aug 03 '12 at 04:59
  • yes you would do that in viewDidLoad.Also be sure to remove the observer in Dealloc method using this method removeObserver:name:object: – Suny Aug 03 '12 at 09:03
  • I want to try to reply by pasting code into these comments and I can't seem to figure it out. It says indent 4 spaces and paste in your code. That doesn't work. Somebody says indent 4 spaces on every line. That doesn't work either. Somebody says hit a particular icon when pasting in code. There is no icon. ???? – iOSAppGuy Aug 05 '12 at 00:37