2010-03-24 46 views
0

我已经在视图上创建了一个UIButton,并在UIButton的[UIButton * button1_obj]上创建了一个按钮[UIButton * button2_obj]在同一视图上创建。在iPhone编程中未点击UIButton

我正面临着这两个问题...请指导我如何继续。

  1. button1_obj在我运行我的项目时出现,但没有被点击或突出显示,也没有任何异常被抛出。

  2. 在button1_obj的动作上,button2-obj必须出现在同一个视图上,在放置button2_obj之前如何清除视图。

规范的问题(1):

-(void)start 

{ 

NSLog(@"--------------------------------------------------------------------"); 

NSLog(@"Entered CView start"); 

    //define size and position of view 
    subMainView_G_obj = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 480, 320)]; //initilize the view 
    //subMainView_G_obj.autoresizesSubviews = YES;    //allow it to tweak size of elements in view 

    UIButton_G_obj = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];  
    UIButton_G_obj.frame = CGRectMake(100,70, 150, 50); 
    UIButton_G_obj.backgroundColor = [UIColor clearColor]; 
    //UIButton_G_obj.adjustsImageWhenHighlighted = YES; 
    [UIButton_G_obj setTitle:@"UI" forState:UIControlStateNormal]; 
    [UIButton_G_obj addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside]; 
    [subMainView_G_obj addSubview:UIButton_G_obj]; 

    UIButton_G_obj = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];  
    UIButton_G_obj.frame = CGRectMake(100,150, 150, 50); 
    UIButton_G_obj.backgroundColor = [UIColor clearColor]; 
    UIButton_G_obj.adjustsImageWhenHighlighted = YES; 
    [UIButton_G_obj setTitle:@"Configuration" forState:UIControlStateNormal]; 
    [UIButton_G_obj addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside]; 
    [subMainView_G_obj addSubview:UIButton_G_obj]; 

    NSLog(@"Leaving CView start"); 
    NSLog(@"--------------------------------------------------------------------"); 
} 

- (void)action:(id) sender 
{ 

    NSLog(@"Inside action method.. On click of First View"); 
    buttonUIObj = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain]; 
    buttonUIObj.frame = CGRectMake(100,160,100,50); 
    [buttonUIObj setTitle:@"Next View" forState:UIControlStateNormal]; 
    buttonUIObj.backgroundColor = [UIColor clearColor]; 
    [subMainView_G_obj addSubview:buttonUIObj]; 

} 

我宣布UIButton_G_obj,buttonUIObj,subMainView_G_obj GLOBALLY。

回答

0

很难知道button1_obj的问题是什么,没有看到代码。但是,对于(2),您可能希望通过调用[button1_obj removeFromSuperview]来移除button1_obj。

+0

嘿感谢解决方案(2)..它的工作! (1)的 ,我已更新上述问题。 – suse 2010-03-24 03:43:38

+0

我在解决方案(2)中遇到问题。如果我将代码作为[Button1_obj removeFromSuperview],那么它只会删除一个按钮。我希望整个观点得到澄清并提出新的看法。我应该怎么做? – suse 2010-03-24 04:18:09

0

关于问题1: 首先,您的两个按钮都命名相同。因此,你有一个内存泄漏,因为你正在为你的按钮分配两次空间。为您的按钮使用不同的名称。

关于问题2: 可以隐藏按钮2,直到按下button 1:在-(void)start

UIButton_G_obj_1.tag = 1; 
... 
UIButton_G_obj_2.hidden = YES; 
UIButton_G_obj_2.tag = 2; 

而且在操作方法可以取消隐藏按钮:

if (sender.tag == 1) { 
    UIButton_G_obj_2.hidden = NO; 
}