0

在我正在开发的 iPhone 应用程序中,我需要在 UIScrollView 中放置按钮,但由于我需要放置这么多按钮来滚动浏览,我无法将它们放置在界面生成器中。因此,我必须通过我的代码将它们全部放在滚动视图中。如何在代码中创建一个按钮,然后使用它来触发一个动作。

4

2 回答 2

3

请参阅“如何以编程方式创建基本 UIButton ”。另请参阅目标/操作机制

于 2011-01-17T23:29:19.930 回答
3
  UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  //position button
  myButton.frame = CGRectMake(50, 50, 50, 50); 
  [myButton setTitle:@"Click" forState:UIControlStateNormal];
  // add targets and actions
  [myButton addTarget:self action:@selector(myButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
   // add to a view
   [self.view addSubview:myButton];

更多信息在这里

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIButton_Class/UIButton/UIButton.html

于 2011-01-17T23:30:27.317 回答