iPhone 应用程序:SDK 3.0:按钮选定状态:无法工作!

发布于 2024-07-28 22:19:30 字数 272 浏览 5 评论 0原文

尝试做简单的按钮功能。

按钮A。有3种状态:默认、高亮、选中。 再次单击时应取消选择。 在我的一生中,我什至无法建立简单的三态功能。

突出显示(按下时)显示为内置(变为“库存”蓝色)。 我使用按钮属性加载选定状态的图像...使用控制/内容来单击突出显示和选择开关,试图找到正确的组合...

我认为它只是在下拉列表中选择我想要编辑的状态....它会注册我对该状态的编辑...加载图像/更改颜色/等等...

不!!!! 我错过了什么..?

Trying to do simple button functionality.

Button A. It has 3 states: default, highlighted, selected. Should UNSELECT when clicked again.
For the life of me, I can't even get a simple 3 state functionality established.

Highlight (on press) appears built in (goes to "stock" blue).
I"ve used Button Attributes to load in image for selected state...PLayed with Control/Content to click Highlight and Selected on and off, trying to find the right combo...

I thought it'd simply be selecting in dropdown the state I want to edit....and it would register my edits for that state...images loaded/ colors changed/ etc....

NO!!!!
What am I missing..?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

无所的.畏惧 2024-08-04 22:19:30

您想做什么来为不同的状态设置图像或标题? 为此,您可以使用诸如

-(void)setImage:(UIImage *)image forState:(UIControlState)state;

OR

-(void)setTitle:(NSString *)title forState:(UIControlState)state;

之类的方法,这就是您所要求的。 可能是我误解了你的问题,因为它不够清楚。

What are you trying to do set the images or titles for the different states? For that you can just use methods such as

-(void)setImage:(UIImage *)image forState:(UIControlState)state;

OR

-(void)setTitle:(NSString *)title forState:(UIControlState)state;

is that what you are asking. May be i am misunderstanding your question because its not clear enough.

清晨说晚安 2024-08-04 22:19:30

我在任何地方都找不到关于这个问题的直接答案。 所以我制定了自己的解决方案,看起来效果很好。

不要忘记连接您需要的 IB 中的按钮
绑定两者
触摸内部setButtonPressed

文件所有者按钮 ex。 1stButton


伪代码

  • 清除所有选定的按钮
  • 将发件人设置为选定的

代码


//Declare your buttons in .h

UIButton *1stButton;
UIButton *2ndButton;
UIButton *3rdButton;

@property(nonatomic, retain) IBOutlet UIButton *1stButton;
@property(nonatomic, retain) IBOutlet UIButton *2ndButton;
@property(nonatomic, retain) IBOutlet UIButton *3rdButton;

//In .m file - write 2 methods 

-(void)clearButtons
{
    [1stButton setSelected:FALSE];
    [2ndButton setSelected:FALSE];
    [3rdButton setSelected:FALSE];
}

//attach to touch up inside event in IB for each button

-(void) setButtonPressed:(UIButton *)sender 
{
    self.clearButtons;
    [sender setSelected:TRUE];
    [sender setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
}

I couldn't find a straight answer about this anywhere. So I brewed my own solution that seems to work great.

Dont forget to wire up your buttons in IB you will need
to bind both
touch up inside to setButtonPressed

file owner to button ex. 1stButton


Psuedo Code

  • Clear all buttons selected
  • Set sender to selected

CODE


//Declare your buttons in .h

UIButton *1stButton;
UIButton *2ndButton;
UIButton *3rdButton;

@property(nonatomic, retain) IBOutlet UIButton *1stButton;
@property(nonatomic, retain) IBOutlet UIButton *2ndButton;
@property(nonatomic, retain) IBOutlet UIButton *3rdButton;

//In .m file - write 2 methods 

-(void)clearButtons
{
    [1stButton setSelected:FALSE];
    [2ndButton setSelected:FALSE];
    [3rdButton setSelected:FALSE];
}

//attach to touch up inside event in IB for each button

-(void) setButtonPressed:(UIButton *)sender 
{
    self.clearButtons;
    [sender setSelected:TRUE];
    [sender setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
}
网名女生简单气质 2024-08-04 22:19:30

您确实应该发布一些代码(或 IB 屏幕截图),因为很难完全解析您所说的内容。

UIButton 的默认行为是在按下时显示选定状态,然后恢复到正常状态。 如果您希望它“锁定”按下,则需要在挂接到 UIButton 的 IBAction 中执行类似以下操作:

BOOL pressed;
UIButton *button

- (IBAction) toggleButton:(id)sender
{
    button.selected = ! pressed;
    pressed = ! pressed;
}

You really should post some code (or an IB screenshot) as it's hard to fully parse what you are saying.

The default behavior of a UIButton is to show the Selected state on press, then revert back to the normal state. If you want it to "lock down" a press, you need to do something like this in the IBAction hooked to the UIButton:

BOOL pressed;
UIButton *button

- (IBAction) toggleButton:(id)sender
{
    button.selected = ! pressed;
    pressed = ! pressed;
}
べ映画 2024-08-04 22:19:30

啊! 好吧,作为一名程序员,我希望在 Interface Builder 中获得简单的“库存”解决方案。

要清楚“想要/希望:

按钮 AE。每个按钮应该有 3 种状态:默认、突出显示、选定。
默认:休息状态。
突出显示:使用界面生成器功能进行一些处理或导入将出现在突出显示上的图形。 按下按钮时会出现此状态。
已选择:当按钮 AE 释放时,它会通过显示新状态、再次使用界面生成器中的调整(字体颜色、阴影)或导入图形来表明已选择该按钮。 当您再次单击“选定”按钮时,它将返回到“默认”状态。

我想我只是认为这很“简单”,因为 SDK 3.0 Interface Builder 有 3 个状态的下拉菜单。 我认为代码会“神奇地”分配给按钮,使其能够根据需要发挥作用,并实现美观的转变。

我非常感谢任何和所有的反馈。 我可以将任何编码建议传递给该应用程序的合作伙伴。

AH! Well, not being a programmer, I was hoping for simple "stock" solution within Interface Builder.

To be clear of the "want/wish:

Button A-E. Each button should have 3 states: Default, highlight, selected.
Default: resting state.
Highlight: some treatment using interface builder features or import a graphic that will appear on Highlight. This state appears while button is being pressed.
Selected: when button A-E is released, it shows that it has been selected by displaying a new state, again by using tweaks within interface builder (font color, shadow) or import a graphic. When you click the SELECTED button again, it returns to Default state.

I guess I just thought it'd be "easy" because SDK 3.0 Interface Builder has dropdowns for the 3 states. I thought code would "magically" be assigned to the button allowing it to function as desired along with it's aesthetic shift.

I TOTALLY appreciate any and all feedback. I can pass along any coding advice to partner on this App.

旧话新听 2024-08-04 22:19:30

试试这个:

UIImage *img1 = [UIImage  imageNamed:@"image1.png"];
UIImage *img2 = [UIImage  imageNamed:@"image2.png"];
UIImage *img3 = [UIImage  imageNamed:@"image3.png"];

[button setImage:img1 forState:UIControlStateNormal];
[button setImage:img2 forState:UIControlStateHighlighted];
[button setImage:img3 forState:UIControlStateSelected];
[button setImage:img2 forState:(UIControlStateHighlighted+UIControlStateSelected)];

[img1 release];
[img2 release];
[img3 release];

这是因为状态变量实际上是一个位标志。

Try this:

UIImage *img1 = [UIImage  imageNamed:@"image1.png"];
UIImage *img2 = [UIImage  imageNamed:@"image2.png"];
UIImage *img3 = [UIImage  imageNamed:@"image3.png"];

[button setImage:img1 forState:UIControlStateNormal];
[button setImage:img2 forState:UIControlStateHighlighted];
[button setImage:img3 forState:UIControlStateSelected];
[button setImage:img2 forState:(UIControlStateHighlighted+UIControlStateSelected)];

[img1 release];
[img2 release];
[img3 release];

This is because the state variable is actually a bit flag.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文