Objective-C 在运行时向我的 iPhone 应用程序添加 NSButton

标签 objective-c button runtime addition

我在这方面完全一无所知!逐步解释可赚取奖励积分。

最佳答案

在 iOS 中我们有 UIButtons(不是 NSButtons)

以下是如何以编程方式创建 UIButton:

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; //3
[button setFrame:CGRectMake(320, 0, 100,100 )];
[button setTitle:@"PressMe!" forState:UIControlStateNormal]; 
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];//2
[aView addSubview:button]; //1

和:

- (void)buttonPressed:(UIButton *)sender{

    //do something here ;)
    [sender setTitle:@"PressMe!. Again" forState:UIControlStateNormal];


}

1:如果您位于 UIViewController 中,则 aView 将是 self.view 如果您在其他地方,只需传递包含您的按钮的 View

2:最简单的情况就是这样,但您可以将任何对象设置为目标。该对象必须具有您在 @selector(...) 中传递的方法。否则,按下按钮时您的应用程序将崩溃。

关于Objective-C 在运行时向我的 iPhone 应用程序添加 NSButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4688826/

相关文章:

javascript - 上一个/下一个 Javascript 字符串处理?

ios - 用户转到主屏幕后,按钮不会更改单击时的图像

c++ - C++ 中的运行时接口(interface)和对象组合

java - 读取 java exec 的输出

ios - 使用 MKAnnotationView 委托(delegate)时引脚消失

ios - 更改 UIView 框架结束触摸事件

objective-c - swipeWithEvent 相当于 Lion

ios - 如何只在 ELCImagePickerController 中选择视频?

Java 防止按钮在图标更改时调整大小

Delphi - 在现有表格上分配 OnClose 表格