开发者

Getting a variable into another class through properties or button tags with Iphone SDK?

开发者 https://www.devze.com 2022-12-24 03:01 出处:网络
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

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 开发者_Python百科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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消