のiOSビュー階層の基礎のために、このリンクをチェック: Getting started with iOS Viewsと理解し、次の図(単位:techrepublic.com):プログラム
![Credits: http://www.techrepublic.com/](https://i.stack.imgur.com/w5lCb.png)
:
// Create your button wherever you wish (below is example button)
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.frame = CGRectMake(0.0, 0.0, 320, 450);
[myButton setTitle:@"Yay" forState:UIControlStateNormal];
[myButton addTarget:self action:@selector(didTouchUp:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:myButton];
// This method will be called when touch up is made on the button
- (void)didTouchUp:(UIButton *)sender {
NSLog(@"Button Pressed!");
}
説明:
[myButton addTarget:self action:@selector(didTouchUp:) forControlEvents:UIControlEventTouchUpInside];
- myButtonという - あなたのボタンビュー
- addTarget - あなたが呼び出したいセレクタ - メソッドが
- アクションに存在する対象
- forcontrolEvents - ユーザーがストーリーボードを使用して
このアクションをトリガーするために行いますコントロールイベント:
![enter image description here](https://i.stack.imgur.com/LkUXw.png)
- (A)絵コンテが正しいクラス
- (Bに対応していることを確認してくださいに)UIButtonをオブジェクトライブラリからストーリーボードにドラッグします
dをViewController.hファイルに追加します。
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIButton *myButton;
- (IBAction)didTouchUp:(id)sender;
@end
次に、ViewControllerに追加します。メートルのファイル:
- (IBAction)didTouchUp:(id)sender {
NSLog(@"Button Pressed!");
}
最後には、UIButtonとIBActionの間の接続を作成します。
![enter image description here](https://i.stack.imgur.com/Ww4df.png)
- (A)ボタンビュー
- (B)ドラッグtouch上で右クリックストーリーボード上のボタンビューへの操作を
- (C)didTouchUpを押してください:
これは、このような手順を行うには非常に基本的な流れである...あなたは以下を読むことによってあなたのスキルを消費することがあります:
プロパティができ
ビューコントローラ内のすべてのサブビューによってアクセスされます。ボタンイベントを処理するためのibactionコンセントを作成することも、独自のターゲットメソッドを追加することもできます。 –