1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

iOS - UIButtonTap時の処理を他クラスで行う

1
Last updated at Posted at 2016-08-18

課題

UIButtonTap時の処理を他クラスで行う
→ UIButtonをViewに書いているがTap時の処理はViewControllerに書きたい的な状況

解決方法

  1. UIButtonのaddTargetに対象のクラスを指定
  2. 対象のクラスにactionで指定したFunctionを記述

View.h : Buttonを持っている画面
ViewController.h : ButtonTap時の処理を記述

objective-c

View.h
# import "ViewController.h"

- (void)makeBtn:(ViewController *)ViewController;
View.m
- (void)makeBtn:(ViewController *)ViewController {
	UIButton* button = [[UIButton alloc] init];
	[button addTarget:ViewController action:@selector(tapButton:) forControlEvents:UIControlEventTouchUpInside];
}
ViewController.h
# import "View.h"

- (void)tapButton:(UIButton *)button;
ViewController.m
- (void)testFunc{
	View* view = [[View alloc] init];
	[view makeBtn:self];
}

- (void)tapButton:(UIButton *)button {
	NSLog(@"tapped!!!");
}

Swift

今度書く。

1
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?