iPhone - 按下按钮时调用 UIView 中生成的 UISwitch
为了澄清我的问题,我的程序在屏幕上有三个灯泡(自定义 UIButton) 当按下任何灯泡时,我会以编程方式生成一个带有开关的 UIView 当我打开开关时,相应的灯泡会亮起(更改其背景图像) 但是,我在访问这个 UISwitch 时遇到了麻烦,因为我无法公开声明它。
我的代码是这样的:
@property buttonA;
@synthesize buttonA;//all three buttons have their background image set to 'off.png'
- (IBAction)lightBulbPressed:(UIButton *)sender
{
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(1,1, 64, 64)];
UISwitch *mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(0,0,64,64)];
[mySwitch addTarget:self action:@selector(onOrOff) forControlEvents:UIControlEventValueChanged];
[myView addSubview:mySwitch]
[self.view addSubview:myView];
}
所以困扰我的是如何对选择器 onOrOff 进行编程,以便它知道哪个开关被触摸并更改的背景图像相应的按钮。
To clarify my question, my program has three lightbulb on the screen (Customized UIButton)
when any lightbulb is pressed, I programatically generate a UIView with a switch on it
when I turn on the switch, corresponding lightbulb will light up (change its background image)
However, I have trouble accessing this UISwitch since I can't declare it publicly
My code goes something like this:
@property buttonA;
@synthesize buttonA;//all three buttons have their background image set to 'off.png'
- (IBAction)lightBulbPressed:(UIButton *)sender
{
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(1,1, 64, 64)];
UISwitch *mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(0,0,64,64)];
[mySwitch addTarget:self action:@selector(onOrOff) forControlEvents:UIControlEventValueChanged];
[myView addSubview:mySwitch]
[self.view addSubview:myView];
}
So what troubles me is how to program the selector onOrOff, so that it knows which switch is being touched and change the background image of corresponding button accordingly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
想想你的方法:
你已经知道是谁调用了它。这条信息存储在
sender
中。所以你可以保存它并稍后在
onOrOff
中使用顺便说一句,如果你使用的是 UISwitch,你必须检查
而不是 UIControlEventTouchUpInside。
编辑:要传递您的
sender
,您可以将其值存储到 .h 文件中声明的NSString *buttonTapped
Think about your method:
You already know who called it. This piece of information is stored in
sender
.So you can save it and use later in
onOrOff
By the way, if you are using UISwitch you have to check
and not UIControlEventTouchUpInside.
EDIT: To pass your
sender
you can store its value to aNSString *buttonTapped
declared in your .h file一种方法是在 IB 中为它们提供不同的标签编号,然后在 - (IBAction)lightBulbPressed:(UIButton *)sender 方法中获取它们的标签。例如
NSInteger pressButtonTag = [sender tag];
,然后从那里开始。另外,您可以在 IB 中添加该视图,向其添加开关,放入所有者的层次结构而不是视图,然后设置,而不是每次用户按下按钮时 alloc/init
myView
.h 中的出口。每当你需要它时调用它,然后再次通过标签访问开关,例如(UISwitch *mySwitch = (UISwitch *)[myView viewWithTag:kSwitchTag];
)并做任何你想做的事情(在或off),将其添加到子视图并稍后将其删除。这样效率更高。One way to do so, is taht you give them distinct tag numbers in IB, and in
- (IBAction)lightBulbPressed:(UIButton *)sender
method, get their tag. e.g.NSInteger pressedButtonTag = [sender tag];
, and go from there.Also, instead of alloc/init
myView
every time user presses a button, you can add that view in IB, add the switch to it, put in the hierarchy of the owner but not the view, and set an outlet to it in .h. Call it whenever you need it, and again, access the switch by tag e.g. (UISwitch *mySwitch = (UISwitch *)[myView viewWithTag:kSwitchTag];
) and do whatever you want to do (on or off), add it to the subview and remove it later. This is more efficient.