简体   繁体   English

在iPhone上切换UIButton

[英]Toggle UIButton on the iPhone

How can I make a button that says "Show Picture" and when it's clicked it changes to "Hide Picture". 如何制作显示“显示图片”的按钮,单击该按钮后将其更改为“隐藏图片”。 I'm new to objective C, I know how to make a button in interface builder but don't know how to switch the button's text and function. 我是目标C的新手,我知道如何在界面生成器中创建按钮,但不知道如何切换按钮的文本和功能。 Can someone help me out? 有人可以帮我吗?

Don't abuse the tag property. 不要滥用标签属性。 It is advise only to be used as a button identifier (for example when you have few buttons in your view you set their tags as 0, 1, 2... so can identify which button is a sender). 建议仅将其用作按钮标识符(例如,当视图中的按钮很少时,请将其标记设置为0、1、2 ...,以便可以识别哪个按钮是发送者)。 You could set some global int i variable and change it's value accordingly. 您可以设置一些全局int i变量并相应地更改其值。

abuse the .tag property of the button. 滥用按钮的.tag属性。 Hook up the touch down action to this function: 将触地动作连接到此功能:

-(IBAction)buttonClick:(UIButton*)sender
{
    if ( sender.tag )
    {
        sender.tag = 0;
        sender.text = @"Show Picture";
        // do actions when "hide" is clicked
    } else {
        sender.tag = 1;
        sender.text = @"Hide Picture";
        // do actions when "show" is clicked
    }
}

Instead of (ab)using the tag property, you could also simply toggle the button between selected and not selected, like so: 除了使用(ab)使用tag属性之外,您还可以在选定和未选定之间切换按钮,如下所示:

- (IBAction)myButtonAction:(id)sender
{
   [sender setSelected:![sender isSelected]];

   // or in Objective-C 2.0 if you're so inclined
   sender.selected = !sender.selected;
}

In IB, you could then set the text for the normal and the selected state of the button directly in the inspector (or programmatically through the setTitle:forState: method). 然后,在IB中,您可以直接在检查器中(或以编程方式通过setTitle:forState:方法)设置按钮的正常状态和选定状态的文本。

The tricky thing with this one is that a UIButton doesn't have an "official" text properly - see the docs here: 棘手的是,UIButton没有正确的“官方”文本-请参阅此处的文档:

http://developer.apple.com/iphone/library/documentation/uikit/reference/UIButton_Class/UIButton/UIButton.html http://developer.apple.com/iphone/library/documentation/uikit/reference/UIButton_Class/UIButton/UIButton.html

because it's designed to have multiple, separate sets of texts, displayed according to the button's current state; 因为它被设计为根据按钮的当前状态显示多组单独的文本; ie, whether it's currently enabled or disabled, highlighted, etc. So there's not one, simple property you can set to make this work. 例如,它是当前启用还是禁用,突出显示等。因此,您可以设置没有一个简单的属性来使其起作用。

So, you want to declare your button like this, as both an action and an outlet: 因此,您想像这样将按钮声明为动作和出口:

from button.h: 从button.h:

// inside the class declaration

BOOL pictureShown ; // initializer not required, defaults to 0 (NO)

UIButton * sampleButton ;

// skip irrelevant lines here

@property (nonatomic, retain) IBOutlet UIButton * sampleButton ;
- (IBAction) doSampleButton ;

Hook both of those up in Interface Builder, and then change the text using the setTitle:forState: method (and in this case, I've specified all the states, so the title stays the same across all of them). 将这两种方法都挂接到Interface Builder中,然后使用setTitle:forState:方法更改文本(在这种情况下,我指定了所有状态,因此标题在所有状态中都保持不变)。 For example: 例如:

from button.m: 从button.m:

@synthesize sampleButton ;

- (IBAction) doSampleButton {
    if (pictureShown == YES) {
        // hide the picture, and then...

        [sampleButton setTitle: @"Show Picture" forState: (UIControlStateNormal | UIControlStateHighlighted | UIControlStateSelected | UIControlStateDisabled)] ;
        pictureShown = NO ;
    } else {
        // show the picture, and then...

        [sampleButton setTitle: @"Hide Picture" forState: (UIControlStateNormal | UIControlStateHighlighted | UIControlStateSelected | UIControlStateDisabled)] ;
        pictureShown = YES ;
    }
}

You'll also note that I've declared an instance variable, "pictureShown", in the view controller for the view with the button, to track the current "mode" of the button, and that I'm essentially using an if statement inside the button's action to determine which function is carried out according to the current mode of the button, and to toggle the button text accordingly. 您还将注意到,我已经在带有按钮的视图的视图控制器中声明了一个实例变量“ pictureShown”,以跟踪按钮的当前“模式”,并且我实质上是在使用if语句在按钮的动作中确定根据按钮的当前模式执行哪个功能,并相应地切换按钮文本。

(I'm using this to track the current mode of the button rather than, for example, examining the current title of the button directly, or other ways of storing state on the button, because MVC-wise, this sort of state belongs in the controller class.) (我使用它来跟踪按钮的当前模式,而不是例如直接检查按钮的当前标题,或以其他方式在按钮上存储状态,因为在MVC方面,这种状态属于控制器类。)

(If the function required a lot of code, I'd use messages to self, ie: (如果该功能需要大量代码,则可以使用消息自言自语,即:

[self showPicture] ;
[self hidePicture] ;

to avoid having to cram it all inside doSampleButton, but that's purely stylistic, rather than technically required. 避免将其全部塞入doSampleButton内,但这纯粹是风格,而不是技术要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM