现在的位置: 首页 > 综合 > 正文

objective-c 把所有类型为UIButton的子控件放在父视图中央 动态创建按钮

2013年04月03日 ⁄ 综合 ⁄ 共 1237字 ⁄ 字号 评论关闭
    for(UIView *view in [self.backgroundView subviews])
    {
        //[view removefromsuperview];
        if([[[view superclass] description] isEqualToString:@"UIButton"])
        {
            UIButton * btn=(UIButton*) view;
            btn.titleLabel.text=@"dynamic title";
            btn.center=btn.superview.center;
        }
    }

 

 

- (IBAction)addButton:(id)sender {  
    CGRect frame = CGRectMake(9020020060);  
    UIButton *someAddButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
    someAddButton.backgroundColor = [UIColor clearColor];  
    [someAddButton setTitle:@"动态添加一个按钮!" forState:UIControlStateNormal];  
    someAddButton.frame = frame;  
    [someAddButton addTarget:self action:@selector(someButtonClicked) forControlEvents:UIControlEventTouchUpInside];  
    [self.view addSubview:someAddButton];  

}  

 

 

 

 

-(void) someButtonClicked{    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"   
                                                    message:@"您点击了动态按钮!"     
                                                   delegate:self     
                                          cancelButtonTitle:@"确定"    
                                          otherButtonTitles:nil];    
    [alert show];  

}   

【上篇】
【下篇】

抱歉!评论已关闭.