在iPhone编程中没有点击UIButton

时间:2010-03-24 03:17:28

标签: iphone objective-c uibutton

我在视图上创建了一个UIButton,并且在UIButton [UIButton * button1_obj]的操作上,在同一视图上创建了另一个按钮[UIButton * button2_obj]。

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

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

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

  3. 问题代码(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。

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

关于问题2: 您可以隐藏按钮2,直到按下按钮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;
}