2016-06-30 13 views
0

UIButtonを作成しますが、画面に表示されません。 .MファイルになぜUIButtonが画面に表示されないのですか?

@property (weak, nonatomic) IBOutlet UIButton * button; 
@property (weak, nonatomic) IBOutlet UIImage * Image1; 

::.hファイルで

_button = [UIButton buttonWithType:UIButtonTypeCustom]; 
[_button addTarget:self action:@selector(_button1:) forControlEvents:UIControlEventTouchUpInside]; 
[_button setTitle:@"Show View" forState:UIControlStateNormal]; 
_button.frame = CGRectMake(0, 0, 437, 765); 

_Image1 = [UIImage imageNamed:@"1.png"]; 
[_button setBackgroundImage:_Image1 forState:UIControlStateNormal]; 
[self.view addSubview: _button]; 

このボタンは動作しないのはなぜ?

しかし、このボタンが機能します。

UIButton *button1; 
button1 = [UIButton buttonWithType:UIButtonTypeCustom]; 
[button1 addTarget:self action:@selector(swipeleft1:) forControlEvents:UIControlEventTouchUpInside]; 
[button1 setTitle:@"Show View" forState:UIControlStateNormal]; 
button1.frame = CGRectMake(0, 0, 437, 765); 

UIImage *buttonImage1 = [UIImage imageNamed:@"1.png"]; 
[button1 setBackgroundImage:buttonImage1 forState:UIControlStateNormal]; 
[self.view addSubview:button1]; 
+2

「does not work」とはどういう意味ですか? – Proton

+0

ボタンが画面に表示されない – user214155

+0

'_button'、プロパティ、またはivarをどのように定義しますか? – Proton

答えて

1

この方法あなたの特性を定義します。

@property (strong, nonatomic) UIButton * button; 
@property (strong, nonatomic) UIImage * Image1; 

強いあなたのボタンや画像を使用すると、画面に追加する機会を得る前に、それらが割り当て解除された瞬間に、保持されることを意味します。

IBOutletsは、ビューで既に保持されているため弱い場合がありますが、インターフェイスビルダーを使用せずにボタンをプログラムで作成しています。

関連する問題