IOS_Object-C 學習筆__ Button 按下後換 Button 文字 ( Convert button text )
改變按鈕上呈現的文字內容,例如我們要按下按鈕後改變 Button 物件文字內容。首先,要給這個按鈕拉 IBOutlet 傳遞要改變的內容和 IBAction 要執行的事情。
第二,透過設置布林值來判斷按鈕是否在被按下一次。
第三, setTitle 按下去後要呈現的文字。
//=======
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIButton *textButton;
@property (nonatomic, assign) BOOL buttonTouch;
@end
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[_textButton setTitle:@"按一下" forState:UIControlStateNormal];
}
- (IBAction)oi:(id)sender {
self.buttonTouch = !self.buttonTouch;
if (self.buttonTouch ) {
[_textButton setTitle:@"再按一下" forState:UIControlStateNormal];
}else{
[_textButton setTitle:@"按一下" forState:UIControlStateNormal];
}
}
//=======
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIButton *textButton;
@property (nonatomic, assign) BOOL buttonTouch;
@end
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[_textButton setTitle:@"按一下" forState:UIControlStateNormal];
}
- (IBAction)oi:(id)sender {
self.buttonTouch = !self.buttonTouch;
if (self.buttonTouch ) {
[_textButton setTitle:@"再按一下" forState:UIControlStateNormal];
}else{
[_textButton setTitle:@"按一下" forState:UIControlStateNormal];
}
}
留言
張貼留言