2011-10-23 34 views
0

我有一個簡單的問題,但我不知道爲什麼它不起作用。 我以編程方式創建了一個按鈕,我想將方法​​「backLogin」添加到該操作。 奇怪的是,我沒有得到任何錯誤,但按鈕不會以任何方式迴應。我在互聯網上看到的任何地方,這段代碼都應該可以工作。 任何想法我做錯了什麼?自行創建的按鈕後面的操作不起作用。

-(void) loginAppear{ 

UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
myButton.frame = CGRectMake(20, 20, 75, 37); // position in the parent view and set the size of the button 
[myButton setTitle:@"Back" forState:UIControlStateNormal]; 
// add targets and actions 
[myButton addTarget:self action:@selector(backLogin) forControlEvents:UIControlEventTouchUpInside]; 
// add to a view 
[myButton setAlpha:1]; 
[loginImage addSubview:myButton];  
} 

後面 「backLogin」 的代碼已經先後在其他情況:不是添加到爲myButton的loginImage

-(void) backLogin{ 

CALayer *layer = loginImage.layer;  

//spin frame 
CABasicAnimation *anim1 = 
[CABasicAnimation animationWithKeyPath: @"transform" ]; 
anim1.toValue = [ NSValue valueWithCATransform3D: 
       CATransform3DMakeRotation(180 * M_PI, 0.0, 1.0, 0) ]; 
anim1.duration = .2; 
anim1.cumulative = YES; 
anim1.repeatCount = 1; 
[layer addAnimation: anim1 forKey: @"transformAnimation" ];  

} 
+0

你試過在你的backLogin方法中放一個NSLog,來確認是否調用該方法......也許這裏的代碼不工作 – Quirin

+0

什麼是loginImage?你有沒有看到按鈕被按下的視覺指示? –

+0

你好,loginImage是一個UIImageView實例。我現在會嘗試NSLog .. – Trace

回答

0

,嘗試將其添加到主視圖(如[self.view addSubview:爲myButton ])。您可能需要更改框架座標以正確定位按鈕。

+0

謝謝!現在,我得到我的按鈕的反應,但一個錯誤; [loginViewController backLogin:]:無法識別的選擇發送到實例0x1ad2c0雖然我沒有看到任何錯誤...? – Trace

+0

:[loginViewController backLogin:]似乎表明它期望backLogin是一個接受一個參數的函數。也許你需要: - (void)backLogin:(UIButton *)sender。對於addTarget調用,使用@selector(backLogin :)。 –

+0

更好的是,改回backLogin爲: - (void)backLogin:(id)sender –