2010-03-24 52 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; 
}