【问题标题】:UISwitch won't show up in custom UIViewUISwitch 不会出现在自定义 UIView 中
【发布时间】:2012-02-04 08:05:04
【问题描述】:

我创建了一个自定义 UIView 类并尝试以编程方式在其中设置一个 UISwitch 控制器。 UISwitch 不会出现。邪恶在哪里?

UIView 类:

- (void)viewWillAppear:(BOOL)animated
{
    uiSwitch=[[[UISwitch alloc]initWithFrame:CGRectMake(5.00,200.00,79,27)] autorelease];
    [uiSwitch addTarget:self action:@selector(uiSwitchCurrentStatus) forControlEvents:UIControlEventValueChanged];
    [self addSubview:uiSwitch];
}

我已经检查了有关 UIswitch 的其他问题,但所有这些问题似乎都不是我的情况。提前谢谢你。

【问题讨论】:

    标签: ios cocoa-touch uiswitch


    【解决方案1】:

    (如果在 UIViewCotroller 中) 试试[self.view addSubview:uiSwitch];

    或者,如果这确实在 UIView 类中,则将整个方法移动到视图控制器。

    或者把代码放在UIViewlayoutSubviews中。

    在任何情况下,您都应该检查开关是否已经存在。否则可能会泄漏。

    另外如果放在视图控制器中- (void)viewDidLoad 可能是更好的地方。

    -(void)layoutSubviews{
        if (!uiSwitch){
            uiSwitch=[[[UISwitch alloc]initWithFrame:CGRectMake(5.00,200.00,79,27)] autorelease];
            [uiSwitch addTarget:self action:@selector(uiSwitchCurrentStatus) forControlEvents:UIControlEventValueChanged];
            [self addSubview:uiSwitch];
        }
    
    }
    

    【讨论】:

    • 我的课是@interface DrawItemDetails : UIView。它不包含参数 .view
    • viewWillAppear: 是 UIViewController 方法,而不是视图。如果放置在 UIView 子类中,它将永远不会被调用
    • 在界面 DrawItemDetails : UIView { ItemShow *theItem; UISwitch *uiSwitch; UIButton *setQualityButton; } At property (retain, nonatomic) IBOutlet UISwitch *uiSwitch;
    • 那么,创建 UISwitch 并绘制他的地方在哪里。
    • 正如我所说:要么在 viewController -viewDidLoad 要么在 view layoutSubviews
    猜你喜欢
    • 1970-01-01
    • 2012-08-09
    • 1970-01-01
    • 2016-01-27
    • 1970-01-01
    • 1970-01-01
    • 2020-08-06
    • 1970-01-01
    • 2010-11-22
    相关资源
    最近更新 更多