事實上是辦的到的,就是利用sendActionsForControlEvents來達成。
假設現在有二個Button,button1 button2。
button1的事件是很簡單的印出Log,說我被按了。
而我們要靠button2來觸發button1的事件。
而因為我們必須要取得button1的實體,因此要設定IBOutlet。
最後只要透過button1的sendActionsForControlEvents,就可以完成了。
完整程式碼可以參考下面這個
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "ViewController.h" | |
@interface ViewController () | |
@property (weak, nonatomic) IBOutlet UIButton *button; | |
@end | |
@implementation ViewController | |
- (IBAction)buttonClick:(id)sender { | |
NSLog(@"我是button,我被按了。"); | |
} | |
- (IBAction)button2Click:(id)sender { | |
NSLog(@"我是button2,我被按了。"); | |
[self.button sendActionsForControlEvents:UIControlEventTouchUpInside]; | |
//靠程式碼去觸發button的TouchUpInside事件 | |
} | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
// Do any additional setup after loading the view, typically from a nib. | |
} | |
- (void)didReceiveMemoryWarning { | |
[super didReceiveMemoryWarning]; | |
// Dispose of any resources that can be recreated. | |
} | |
@end |
這個東西可以讓你測試一些UI事件。
沒有留言:
張貼留言