iPhone Dev-UIButtonを手動で作成
質問
iPhoneで開発する方法を学んでいます。BeginningiPhone 3 development Exploring the SDKという本を購入しました。噛んだ後、Interface Builderを捨てることにしました。私はまだすべてのビューをIBで設計していますが、それをすべてコードで記述し、nibファイルのみを使用してコントロールのフレームを取得します。
したがって、 UIButton
を作成する必要があり、ドキュメントは他のコントロールとは異なります。 initWithFrame:
を使用してみましたが、この他のメソッド buttonWithType:
が自動リリースされていると想定していますが、とにかく画面にボタンを表示できませんでした。誰かが変更可能なタイトルのボタンをローカルに作成するコードを少し書いてください
解決
次のようなものを試してみます:
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(20, 20, 200, 44); // position in the parent view and set the size of the button
[myButton setTitle:@"Click Me!" forState:UIControlStateNormal];
// add targets and actions
[myButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
// add to a view
[superView addSubview:myButton];
免責事項:これをここに入力するだけです。現在、Macにアクセスできないため、テストできません。
PS Interface Builderを使用しない特別な理由はありますか?好奇心が強い。
所属していません StackOverflow