Objective C IBOutlets

我的未来我决定 提交于 2020-01-15 06:03:26

问题


In cases where multiple buttons call an IBOutlet, can the IBOutlet determine which button was pressed?

edit:

All fixed and wired up. key point: Object ID is not sender tag! Tag is a standalone value on the first page of the attributes.

- (IBAction)buttonPressed:(id)sender
{
    switch ( [sender tag] )
    {
        case 109:
            NSLog(@"Button 1");
            break;

        case 108:
            NSLog(@"Button 2");
            break;
    }
}

回答1:


Normally you would connect the buttons event (Touch Down) to the IBAction method you wish to invoke in your controller.

The method prototype would look like:

-(IBAction) doStuff:(id)sender;

Then the "sender" could be used to verify identity of the object doing the calling if needed.

IBOutlet would be used to contact the UIButton from your code, to set its text or properties for example.




回答2:


In cases where multiple buttons call an IBOutlet

maybe an IBAction?

firs if all

- (IBAction) actionPerformed:(id)sender

so, sender is your button

second is that every subclass of UIView has a tag field, so you can use it

int tag = [sender tag];

you can set tag in IB




回答3:


Better still, define an IBAction method for each button in your UI. Then you can individually wire buttons to methods using IB and avoid hard coding const integers into your code.



来源:https://stackoverflow.com/questions/622072/objective-c-iboutlets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!