我有一個簡單的問題,但我不知道爲什麼它不起作用。 我以編程方式創建了一個按鈕,我想將方法「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" ];
}
你試過在你的backLogin方法中放一個NSLog,來確認是否調用該方法......也許這裏的代碼不工作 – Quirin
什麼是loginImage?你有沒有看到按鈕被按下的視覺指示? –
你好,loginImage是一個UIImageView實例。我現在會嘗試NSLog .. – Trace