iPhone - 按下按钮时调用 UIView 中生成的 UISwitch

发布于 2024-12-29 03:21:58 字数 727 浏览 2 评论 0原文

为了澄清我的问题,我的程序在屏幕上有三个灯泡(自定义 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 技术交流群。

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

发布评论

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

评论(2

抚笙 2025-01-05 03:21:58

想想你的方法:

- (IBAction)lightBulbPressed:(UIButton *)sender {
    // your method
}

你已经知道是谁调用了它。这条信息存储在sender中。
所以你可以保存它并稍后在 onOrOff 中使用

顺便说一句,如果你使用的是 UISwitch,你必须检查

UIControlEventValueChanged

而不是 UIControlEventTouchUpInside。

编辑:要传递您的 sender,您可以将其值存储到 .h 文件中声明的 NSString *buttonTapped

- (IBAction)lightBulbPressed:(UIButton *)sender {

if (sender == bttOne) {
    buttonTapped = @"ButtonOneTapped";
} else if (sender == bttTwo) {
    buttonTapped = @"ButtonTwoTapped";
} else if (sender == bttThree) {
    buttonTapped = @"ButtonThreeTapped";
}

  // your method

}

- (void)onOrOff {
    if ([buttonTapped isEqualToString:@"ButtonOneTapped"]) {
        // Button One
    } else if ([buttonTapped isEqualToString:@"ButtonTwoTapped"]) {
        // Button Two
    } else if ([buttonTapped isEqualToString:@"ButtonThreeTapped"]) {
        // Button Three
    }
}

Think about your method:

- (IBAction)lightBulbPressed:(UIButton *)sender {
    // 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

UIControlEventValueChanged

and not UIControlEventTouchUpInside.

EDIT: To pass your sender you can store its value to a NSString *buttonTapped declared in your .h file

- (IBAction)lightBulbPressed:(UIButton *)sender {

if (sender == bttOne) {
    buttonTapped = @"ButtonOneTapped";
} else if (sender == bttTwo) {
    buttonTapped = @"ButtonTwoTapped";
} else if (sender == bttThree) {
    buttonTapped = @"ButtonThreeTapped";
}

  // your method

}

- (void)onOrOff {
    if ([buttonTapped isEqualToString:@"ButtonOneTapped"]) {
        // Button One
    } else if ([buttonTapped isEqualToString:@"ButtonTwoTapped"]) {
        // Button Two
    } else if ([buttonTapped isEqualToString:@"ButtonThreeTapped"]) {
        // Button Three
    }
}
污味仙女 2025-01-05 03:21:58

一种方法是在 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.

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