一个 UIView 中的多个 UISwitch 项目

发布于 2024-12-19 05:58:52 字数 1046 浏览 2 评论 0原文

可能的重复:
处理表视图中的多个 UISwitch 控件不使用标签属性

那么我想问一下如何在一个 UIview 上配置 7 个 UISwitch?我将开关放置在界面生成器中,然后我想通过代码来管理它们。管理这个问题的最佳方法是什么。这是我的 .h 到目前为止,

#import <UIKit/UIKit.h>

@interface searchEditViewController : UIViewController{

    UISwitch *switchOne;
    UISwitch *switchTwo;
    UISwitch *switchFor;
    UISwitch *switchFive;
    UISwitch *switchSix;
    UISwitch *switchSeven;


}

@property(nonatomic,retain)UISwitch *switchOne;
@property(nonatomic,retain)UISwitch *switchTwo;
@property(nonatomic,retain)UISwitch *switchThree;
@property(nonatomic,retain)UISwitch *switchFour;
@property(nonatomic,retain)UISwitch *switchFive;
@property(nonatomic,retain)UISwitch *switchSix;
@property(nonatomic,retain)UISwitch *switchSeven;
-(IBAction)toggleButtonPressed:(id)sender;
@end

我想创建一个操作方法toggleButtonPressed 来处理其中的七个

Possible Duplicate:
Handling multiple UISwitch controls in a table view without using tag property

Well i want to ask how can i configure 7 UISwitches on one UIview? I place the switches with interface builder and then i want to manage them through code. What is the best way of managing this. Here is my .h so far

#import <UIKit/UIKit.h>

@interface searchEditViewController : UIViewController{

    UISwitch *switchOne;
    UISwitch *switchTwo;
    UISwitch *switchFor;
    UISwitch *switchFive;
    UISwitch *switchSix;
    UISwitch *switchSeven;


}

@property(nonatomic,retain)UISwitch *switchOne;
@property(nonatomic,retain)UISwitch *switchTwo;
@property(nonatomic,retain)UISwitch *switchThree;
@property(nonatomic,retain)UISwitch *switchFour;
@property(nonatomic,retain)UISwitch *switchFive;
@property(nonatomic,retain)UISwitch *switchSix;
@property(nonatomic,retain)UISwitch *switchSeven;
-(IBAction)toggleButtonPressed:(id)sender;
@end

I want to make one action method toggleButtonPressed that will take care the seven of them

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

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

发布评论

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

评论(2

荆棘i 2024-12-26 05:58:52

为所有 UISwitch 对象创建一个操作(我猜这是 toggleButtonPressed)。现在在这个方法中你可以知道哪个 UISwitch 被触发了:

-(IBAction)toggleButtonPressed:(id)sender{
    UISwitch *switchObj = (UISwitch*)sender;
    if (switchObj == self.switchOne){
        // do stuff
    }

    if (switchObj == self.switchTwo){
        // do stuff
    }

//    switch(switchObj.tag){
//        case 1:
//            // do stuff
//            break;
//        case 2:
//            // do stuff
//            break;
//    }
}

编辑。您可以将 tag 属性设置为某个值(从 1 到 7),并将 if-statement 更改为 switch-case

Create one action for all UISwitch objects (I guess this is toggleButtonPressed). In this method now you can know which UISwitch was triggered:

-(IBAction)toggleButtonPressed:(id)sender{
    UISwitch *switchObj = (UISwitch*)sender;
    if (switchObj == self.switchOne){
        // do stuff
    }

    if (switchObj == self.switchTwo){
        // do stuff
    }

//    switch(switchObj.tag){
//        case 1:
//            // do stuff
//            break;
//        case 2:
//            // do stuff
//            break;
//    }
}

Edit. You can set tag property to some value (from 1 to 7) and change if-statement to switch-case.

橙幽之幻 2024-12-26 05:58:52

@beryllium,你的方法确实有效,但你可能想尝试一些更优雅的方法。

相反,标记每个开关,然后使用 switch-case 代替 if-then,正如您似乎已注释掉的那样。

这是一个示例,请注意 kUIActivityIndi​​catorViewStyleWhiteLarge 实际上是我创建并分配了一个值的常量 #define kUIActivityIndi​​catorViewStyleWhiteLarge 1

- (IBAction)setSpinnerType:(UISwitch *)sender
{
    // switch based on tag value
    // turn other two switches off
    switch (sender.tag) {
        case kUIActivityIndicatorViewStyleWhiteLarge:
            self.spinner.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
            [sender setOn:YES animated:YES];
            [self.whiteSwitch setOn:NO animated:YES];
            [self.graySwitch setOn:NO animated:YES];
            break;
        case kUIActivityIndicatorViewStyleWhite:
            self.spinner.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
            [self.whiteLargeSwitch setOn:NO animated:YES];
            [sender setOn:YES animated:YES];
            [self.graySwitch setOn:NO animated:YES];
            break;
        case kUIActivityIndicatorViewStyleGray:
            self.spinner.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
            [self.whiteLargeSwitch setOn:NO animated:YES];
            [self.whiteSwitch setOn:NO animated:YES];
            [sender setOn:YES animated:YES];
            break;
        default:
            break;
    }
}

如果您想按照自己的方式进行操作,那是可以的,但同样不太理想。但是,如果这样做,则代码:

UISwitch *switchObj = (UISwitch*)sender;
    if (switchObj == self.switchOne){

是多余的。相反,您可以写以下内容:

-(IBAction)toggleButtonPressed:(UISwitch *)sender{
    // UISwitch *switchObj = (UISwitch*)sender;
    if (sender == self.switchOne){
        // do stuff
    }

我希望有帮助。

我创建了一个应用程序来测试不同颜色背景上的 UIActivityIndi​​catorView 样式,它使用滑块和开关。如果有人想查看带有 IB 组件的应用程序,请访问以下网址:

https://github.com/asadquraishi/UIActivityIndi​​catorView -测试

@beryllium, your way does work but you might want to try something a little more elegant.

Instead, tag each switch and then instead of if-then use switch-case as you appear to have commented out.

Here's an example, note that kUIActivityIndicatorViewStyleWhiteLarge is actually a constant I created and assigned a value #define kUIActivityIndicatorViewStyleWhiteLarge 1

- (IBAction)setSpinnerType:(UISwitch *)sender
{
    // switch based on tag value
    // turn other two switches off
    switch (sender.tag) {
        case kUIActivityIndicatorViewStyleWhiteLarge:
            self.spinner.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
            [sender setOn:YES animated:YES];
            [self.whiteSwitch setOn:NO animated:YES];
            [self.graySwitch setOn:NO animated:YES];
            break;
        case kUIActivityIndicatorViewStyleWhite:
            self.spinner.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
            [self.whiteLargeSwitch setOn:NO animated:YES];
            [sender setOn:YES animated:YES];
            [self.graySwitch setOn:NO animated:YES];
            break;
        case kUIActivityIndicatorViewStyleGray:
            self.spinner.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
            [self.whiteLargeSwitch setOn:NO animated:YES];
            [self.whiteSwitch setOn:NO animated:YES];
            [sender setOn:YES animated:YES];
            break;
        default:
            break;
    }
}

If you want to do it your way, that's OK but again less optimal. However if you do, the code:

UISwitch *switchObj = (UISwitch*)sender;
    if (switchObj == self.switchOne){

is redundant. instead you can wrote the following:

-(IBAction)toggleButtonPressed:(UISwitch *)sender{
    // UISwitch *switchObj = (UISwitch*)sender;
    if (sender == self.switchOne){
        // do stuff
    }

I hope that helps.

I created an app to test UIActivityIndicatorView styles on different coloured backgrounds and it uses sliders and switches. Here the url if anyone wants to see the app with IB components:

https://github.com/asadquraishi/UIActivityIndicatorView-Test

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