views:

130

answers:

1

Hello. I have been pounding my head against a wall trying to figure out what I think is a simple thing to do, but I have, so far, been unable to figure it out with the iPhone SDK. I have a view with 4 buttons. When I push the buttons, I have another view called and come up to take over the screen. I want to have a simple variable passed from the old view to the new view. The purpose of this is to know what button was pressed out of the four identical ones. (trying to get an int value of 1 through 4) Here is what I have tried so far:

I initially tried to call the variable from the class itself. My understanding was that my new view was only sitting on top of the old view, so it should not have been released. I could not get access to the variable as the variable was said to not be declared.

I then tried to create a method that would return the int value. Again, the view class was not seen and was being declared as a "first use" of it.

I then attempted to follow another similar post on here that suggested to try and send the button tag number to some part of the new view. I attempted to do this by setting the new views button title but I still cannot figure out a way to code it without coming up with errors.

I am just looking for what method would be the best for me to pursue. I have no problem going back and reading the books on Objective-C, but I'm just trying to figure out which way I should concentrate on. Thank you for any insight.

A: 

Something like this should work:

    button.tag = <whatever the tag value should be>;
    [button addTarget: self selector: @selector(onTouchDown:) forControlEvents: UIControlEventTouchDown];

    - (void) onTouchDown: (id) sender
    {
        int tag = ((UIView*)sender).tag;

        UIViewController *vc = [[MyVC alloc] initWithID: tag];
        // display the view for the vc    
    }

Incidentally, I find NSNotification's very useful for passing results back to a parent controller.

Amagrammer
Thank you. This does work although I figured out something similar just before you answered. The other S.O. answer I was looking at had the function call to the other view before the new view was moved up to the front. For some reason, the function would not work unless I put the call after it was presented instead of before like the other example stated. Thank you for your time.
MarcZero